필터 지우기
필터 지우기

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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by