error in running my optimization code

1 次查看(过去 30 天)
My code is throwing the following error in conversion which i cannot understand.
Code-
yexpected=table2array(Book65);
tspan=0:48;
yo=[0.2 0.05 0.539];
p=optimvar('p',7,"LowerBound",0,"UpperBound",30);
sol=ode45(@(t,y)kumar2004optimizer(t,y,p),tspan,yo);
myfcn = fcn2optimexpr(@kumar2004optimizer,p,tspan,y0);
obj = sum(sum((myfcn - yexpected).^2));
prob = optimproblem("Objective",obj);
p0.p = [3 25 25 12 1 0.5 1];
[psol,sumsq] = solve(prob,p0);
function dydt=kumar2004optimizer(t,y,p)
dydt=zeros(3,1);
dydt(1)=p(1)*y(1)*(1-y(1))-p(2)*y(1)*y(2);
dydt(2)=(p(3)*y(1)+y(3))*y(2)*(1-y(2))-y(2);
dydyt(3)=p(4)*(1+tanh((y(2)-p(5))/p(6)))-p(7)*y(3);
end
error-Unable to perform assignment because value of type
'optim.problemdef.OptimizationExpression' is not convertible to 'double'.

回答(1 个)

Torsten
Torsten 2023-7-19
I assume that "yexpected" is a matrix of size 49x3.
yexpected=table2array(Book65);
p0 = [3 25 25 12 1 0.5 1];
sol = lsqnonlin(@(p)optimizer(p,yexpected),p0)
function res = optimizer(p,yexpected)
tspan=0:48;
y0=[0.2 0.05 0.539];
sol=ode45(@(t,y)kumar2004optimizer(t,y,p),tspan,y0);
res = sol - yexpected;
res = res(:);
end
function dydt=kumar2004optimizer(t,y,p)
dydt=zeros(3,1);
dydt(1)=p(1)*y(1)*(1-y(1))-p(2)*y(1)*y(2);
dydt(2)=(p(3)*y(1)+y(3))*y(2)*(1-y(2))-y(2);
dydt(3)=p(4)*(1+tanh((y(2)-p(5))/p(6)))-p(7)*y(3);
end
  7 个评论
Torsten
Torsten 2023-7-20
编辑:Torsten 2023-7-20
What are the times where these 26x3 measurements were taken ? You have to specifiy them in "tspan" in place of the 0:48 you use at the moment.
And (I made a mistake here) use
[T,Y]=ode45(@(t,y)kumar2004optimizer(t,y,p),tspan,y0);
res = Y - yexpected;
instead of
sol=ode45(@(t,y)kumar2004optimizer(t,y,p),tspan,y0);
res = sol - yexpected;

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by