Effacer les filtres
Effacer les filtres

Can I optimize (using optimization tool box) few variables for a quantity extracted from a covariance matrix (Lyap function) numerically?

87 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Namnendra
Namnendra le 26 Juin 2024 à 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.
  3 commentaires
Anil
Anil le 27 Juin 2024 à 2:40
@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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Computations dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by