How to optimize the Parameters in my code
조회 수: 12 (최근 30일)
이전 댓글 표시
Dear All,
I have a problem regarding the optimization of parameters such that the error should be minimized. As given in the code below, I want to find the best values of parameters (UP1 and UP2) in the given range such that my error minimizes. Can anybody help me in this regard. If there is any example similar to my problem, please let me know. I will be very thankful to you.
Best Regards
------------------------------
clear,clc
global XX
tend = 40;
tspan=1:1:40;
cp1 = 5; cp2 = 3; A1 = 2; rst = 2;
UP1=.2; %%%parameter to be optimized range = [.01 1] %%%
UP2=.7; %%%parameter to be optimized range = [0.5 3] %%%
M =[cp1 0 0 0; 0 cp1 0 0; 0 0 cp2 0; 0 0 0 cp2];
XX=[-rst-cp1*UP1 0 rst 0;
UP1*cp1 0 0 0;
rst 0 -rst-UP2*cp2 0;
0 0 UP2*cp2 0];
options = odeset('AbsTol',1e-10,'RelTol',1e-10,'InitialStep',1e-2, ...
'Mass',M,'Jacobian',XX);
u0 = zeros(4,1);
u0(1) = 1;
[t,u] = ode15s(@call, tspan, u0,options);
n1=[u(3,2) u(7,2) u(end,2)];
n2=[u(3,4) u(7,4) u(end,4)];
e1=[0.3 0.8 1];
e2=[0.5 2 5];
%%%% Objective is to minimize the following error %%%%
error = abs((n1(1)-e1(1))^2+(n1(3)-e1(2))^2+(n1(3)-e1(3))^2+...
(n2(1)-e2(1))^2+(n2(2)-e2(2))^2+(n2(3)-e2(3))^2)
------------------------------
function du = call( t,u )
global XX
du=XX*u;
end
------------------------------
댓글 수: 0
채택된 답변
Teja Muppirala
2011년 5월 2일
People often ask how to do this. Basically you need to pass your ODE solver to your optimization solver. This is an example of how to do it:
댓글 수: 2
Teja Muppirala
2011년 5월 2일
If that example doesn't make sense, or you don't know how to apply that to your problem, feel free to ask for more clarification.
추가 답변 (1개)
Teja Muppirala
2011년 5월 2일
Your answer is actually close, but maybe it wasn't entirely straightforward to match it to the other example without some more experience with seeing these types of problems.
This is (just one) correct implementation of your problem. Make sure you understand why it works. You can see that the answer occurs at a corner, [0.01 3.0].
function [x,f] = dooptim
tspan=1:1:40;
cp1 = 5; cp2 = 3; A1 = 2; rst = 2;
lb = [0.01 0.5]; %Upper bounds on the variables
ub = [1 3]; %Lower bounds on the variables
x0 = [0.2 0.7] % Some initial condition
mycostfun = @(x) dosolve(x,cp1,cp2,A1,rst,tspan);
[x,f] = fmincon(mycostfun,x0,[],[],[],[],lb,ub,[])
function error = dosolve(x,cp1,cp2,A1,rst,tspan)
UP1=x(1); %%%parameter to be optimized range = [.01 1] %%%
UP2=x(2); %%%parameter to be optimized range = [0.5 3] %%%
M =[cp1 0 0 0; 0 cp1 0 0; 0 0 cp2 0; 0 0 0 cp2];
XX=[-rst-cp1*UP1 0 rst 0;
UP1*cp1 0 0 0;
rst 0 -rst-UP2*cp2 0;
0 0 UP2*cp2 0];
options = odeset('AbsTol',1e-10,'RelTol',1e-10,'InitialStep',1e-2, ...
'Mass',M,'Jacobian',XX);u0 = zeros(4,1);
u0(1) = 1;
call = @(t,u) XX*u;
[t,u] = ode15s(call, tspan, u0,options);
n1=[u(3,2) u(7,2) u(end,2)];
n2=[u(3,4) u(7,4) u(end,4)];
e1=[0.3 0.8 1];
e2=[0.5 2 5];
%%%%Objective is to minimize the following error %%%%
error = abs((n1(1)-e1(1))^2+(n1(3)-e1(2))^2+(n1(3)-e1(3))^2+...
(n2(1)-e2(1))^2+(n2(2)-e2(2))^2+(n2(3)-e2(3))^2);
댓글 수: 2
yem
2014년 12월 15일
hi i have a system with 2 delays i want to identify these delays with optimisations tools(Newton algorithm)
참고 항목
카테고리
Help Center 및 File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!