Can I optimize (using optimization tool box) few variables for a qu... (2024)

63 次查看(过去 30 天)

显示 更早的评论

Anil 2024-6-26,2:46

  • 链接

    此问题的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/2132001-can-i-optimize-using-optimization-tool-box-few-variables-for-a-quantity-extracted-from-a-covarianc

  • 链接

    此问题的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/2132001-can-i-optimize-using-optimization-tool-box-few-variables-for-a-quantity-extracted-from-a-covarianc

评论: 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

.

.

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更早的评论

请先登录,再进行评论。

请先登录,再回答此问题。

采纳的回答

Namnendra 2024-6-26,8:27

  • 链接

    此回答的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/2132001-can-i-optimize-using-optimization-tool-box-few-variables-for-a-quantity-extracted-from-a-covarianc#answer_1477286

  • 链接

    此回答的直接链接

    https://ww2.mathworks.cn/matlabcentral/answers/2132001-can-i-optimize-using-optimization-tool-box-few-variables-for-a-quantity-extracted-from-a-covarianc#answer_1477286

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

% 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

  • 链接

    此评论的直接链接

    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

  • 链接

    此评论的直接链接

    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 CenterFile Exchange 中查找有关 Matrix Computations 的更多信息

标签

  • optimization
  • lyap
  • linear equations
  • functions

产品

  • MATLAB

版本

R2023b

Community Treasure Hunt

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

Start Hunting!

发生错误

由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。


Translated by Can I optimize (using optimization tool box) few variables for a qu... (5)

Can I optimize (using optimization tool box) few variables for a qu... (6)

选择网站

选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:

您也可以从以下列表中选择网站:

美洲

欧洲

亚太

联系您当地的办事处

Can I optimize (using optimization tool box) few variables for a qu... (2024)
Top Articles
Latest Posts
Article information

Author: Fr. Dewey Fisher

Last Updated:

Views: 5705

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Fr. Dewey Fisher

Birthday: 1993-03-26

Address: 917 Hyun Views, Rogahnmouth, KY 91013-8827

Phone: +5938540192553

Job: Administration Developer

Hobby: Embroidery, Horseback riding, Juggling, Urban exploration, Skiing, Cycling, Handball

Introduction: My name is Fr. Dewey Fisher, I am a powerful, open, faithful, combative, spotless, faithful, fair person who loves writing and wants to share my knowledge and understanding with you.