69 views (last 30 days)
Show older comments
Anil on 26 Jun 2024 at 2:46
-
-
Link
Direct link to this question
https://jmaab.mathworks.com/matlabcentral/answers/2132001-can-i-optimize-using-optimization-tool-box-few-variables-for-a-quantity-extracted-from-a-covarianc
Edited: Anil on 28 Jun 2024 at 22:56
Accepted Answer: Namnendra
Open in MATLAB Online
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
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=[..]% storing
end %loop c
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 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 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Namnendra on 26 Jun 2024 at 8:27
Open in MATLAB Online
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 Comments Show NoneHide None
Show NoneHide None
Anil on 26 Jun 2024 at 8:59
Direct link to this comment
https://jmaab.mathworks.com/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 on 27 Jun 2024 at 2:40
Direct link to this comment
https://jmaab.mathworks.com/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.
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
Control SystemsControl System ToolboxMatrix Computations
Find more on Matrix Computations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- 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
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office