error in running my optimization code
Mostrar comentarios más antiguos
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'.
Respuestas (1)
Torsten
el 19 de Jul. de 2023
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 comentarios
Ayush Ranjan
el 19 de Jul. de 2023
Ayush Ranjan
el 20 de Jul. de 2023
Torsten
el 20 de Jul. de 2023
Before you pass "yexpected" to "lsqnonlin" by the line
sol = lsqnonlin(@(p)optimizer(p,yexpected),p0)
add the commands
class(yexpected)
size(yexpected)
and show us the output.
Ayush Ranjan
el 20 de Jul. de 2023
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;
Ayush Ranjan
el 20 de Jul. de 2023
Categorías
Más información sobre Get Started with Optimization Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!