How to optimize the Parameters in my code
Show older comments
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
------------------------------
Accepted Answer
More Answers (1)
Teja Muppirala
on 2 May 2011
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 Comments
Mathematics
on 2 May 2011
yem
on 15 Dec 2014
hi i have a system with 2 delays i want to identify these delays with optimisations tools(Newton algorithm)
Categories
Find more on Surrogate Optimization 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!