63 次查看(过去 30 天)
显示 更早的评论
Anil 2024-6-26,2:46
评论: Anil about 17 hours 前
采纳的回答: Namnendra
I know a bit about using optimization tool box for a symbolic function. But I want to use it for numercal values, if possible. For example, the stucture of my code is following:
fid = fopen(sprintf( 'test.dat'),'w' )
for a=0:1:10
for b=0:1:10
x=[];
x0=[];
for c=0:1:10%optimizing this one
A=[....];matrix
B=[.....]; %matrix
V=Lyap(A,B);
v1=(V(1,1)+V(2,2)-1)/2;%somthing from V
.
.
v4=....
x0=[x0; c, v1, v2, v3...]% storing
end %loop c
if size(x0~0)
x=[x0;x];
m1=x(:,4);
[p, q]=max(a); %finding max
c0=x(q,1);%correspoding value of c for that max
v10=x(q,2);correspoding value of v1 for that max
.
.
fprintf(fid,'%f %f %f %f'...., a, b, c0, v01..... )%writing to a file
end %loop if
end %loop b
end %loop a
fclose(fid); %close file
Now I want to use optimization tool as follow, if possible
function [ v1, v2, v3.....vn ]=myfn[a, b, c, V]
v1=(V(1,1)+V(2,2)-1)/2;
v2=....
v3=....
v4=....
end
then calling this function for optimization in main code.
Which optimization tool shall I use for such a numerical optimization?
Thanks a lot.
0 个评论 显示 -2更早的评论隐藏 -2更早的评论
显示 -2更早的评论隐藏 -2更早的评论
请先登录,再进行评论。
请先登录,再回答此问题。
采纳的回答
Namnendra 2024-6-26,8:27
Hello Anil,
I understand that you want to optimize multiple variables for a quantity extracted from a covariance matrix (using the Lyapunov function) numerically.
Yes, you can use MATLAB's Optimization Toolbox to perform this operation. Given your scenario, you can use `fmincon` or `fminunc` for numerical optimization. Here is a structured approach to modify your code to use an optimization function like `fmincon`:
Step 1: Define the Objective Function
Create a function that computes the value you want to optimize based on the covariance matrix `V`.
Step 2: Use the Optimization Function
Use `fmincon` or `fminunc` to perform the optimization within your loops for `a` and `b`.
Below is an example code snippet based on your structure:
function main()
fid = fopen('test.dat', 'w');
for a = 0:1:10
for b = 0:1:10
% Define bounds for c if any
lb = 0;
ub = 10;
c0 = 5; % Initial guess for c
% Optimization options
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
% Perform optimization
[c_opt, fval] = fmincon(@(c) objectiveFunction(a, b, c), c0, [], [], [], [], lb, ub, [], options);
% Compute the corresponding values of v1, v2, v3, v4
A = [...]; % Define your matrix A here
B = [...]; % Define your matrix B here
V = lyap(A, B);
[v1, v2, v3, v4] = myfn(a, b, c_opt, V);
% Write the results to file
fprintf(fid, '%f %f %f %f %f %f %f\n', a, b, c_opt, v1, v2, v3, v4);
end
end
fclose(fid); % Close file
end
function [v1, v2, v3, v4] = myfn(a, b, c, V)
v1 = (V(1,1) + V(2,2) - 1) / 2;
% Compute v2, v3, v4 based on V
v2 = ...;
v3 = ...;
v4 = ...;
end
function obj = objectiveFunction(a, b, c)
A = [...]; % Define your matrix A here
B = [...]; % Define your matrix B here
V = lyap(A, B);
[v1, v2, v3, v4] = myfn(a, b, c, V);
% Define your objective function, for example, minimize v1
obj = v1;
end
Explanation:
1. Main Function: This function iterates over `a` and `b`, and uses `fmincon` to optimize `c` for each combination of `a` and `b`. The optimized value of `c` along with the corresponding values of `v1`, `v2`, `v3`, and `v4` are written to a file.
2. Objective Function: This function computes the value you want to optimize (for example, `v1`). It uses the Lyapunov function to get the covariance matrix `V`, and then computes `v1`, `v2`, `v3`, and `v4` using the `myfn` function.
3. Optimization Options: `fmincon` options are set to use the Sequential Quadratic Programming (SQP) algorithm and display iteration information.
You can adjust the objective function and constraints as needed for your specific problem. This approach allows you to numerically optimize multiple variables for a quantity extracted from a covariance matrix using the Lyapunov function.
I hope the above information helps you with your approach.
Thank you.
2 个评论 显示 无隐藏 无
显示 无隐藏 无
Anil 2024-6-26,8:59
此评论的直接链接
https://ww2.mathworks.cn/matlabcentral/answers/2132001-can-i-optimize-using-optimization-tool-box-few-variables-for-a-quantity-extracted-from-a-covarianc#comment_3196141
@Namnendra Amazing!, that looks interesting. Thanks a lot. I will check it, but can I use @(c) for two or three variables such as @(c,d,e) for their initial conition c0, d0 and e0 etc. Thanks.
Anil about 17 hours 前
此评论的直接链接
https://ww2.mathworks.cn/matlabcentral/answers/2132001-can-i-optimize-using-optimization-tool-box-few-variables-for-a-quantity-extracted-from-a-covarianc#comment_3196701
@Namnendra it looks like objective function has some issues "Output argument "obj" (and possibly others) not assigned a value in the execution with "test>objectiveFunction" function." I thought it's v2, v3 and v4 (say vi's) which are not being used there. If I just use v1 in both the functions, though i want optimal vi's too and the correspoding values of vi's when v1 is min, the error remians the same. Thanks.
请先登录,再进行评论。
更多回答(0 个)
请先登录,再回答此问题。
另请参阅
类别
Control SystemsControl System ToolboxMatrix Computations
在 Help Center 和 File Exchange 中查找有关 Matrix Computations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 简体中文Chinese
- English
- 日本Japanese (日本語)
- 한국Korean (한국어)
联系您当地的办事处