Azzera filtri
Azzera filtri

error in running my optimization code

1 visualizzazione (ultimi 30 giorni)
Ayush Ranjan
Ayush Ranjan il 19 Lug 2023
Commentato: Ayush Ranjan il 20 Lug 2023
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'.

Risposte (1)

Torsten
Torsten il 19 Lug 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 Commenti
Torsten
Torsten il 20 Lug 2023
Modificato: Torsten il 20 Lug 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
Ayush Ranjan il 20 Lug 2023
it worked thank you

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by