フィルターのクリア

error in running my optimization code

1 回表示 (過去 30 日間)
Ayush Ranjan
Ayush Ranjan 2023 年 7 月 19 日
コメント済み: Ayush Ranjan 2023 年 7 月 20 日
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;
Ayush Ranjan
Ayush Ranjan 2023 年 7 月 20 日
it worked thank you

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by