variable mentioned but still getting unrecognized variable error

2 次查看(过去 30 天)
I am unrecognized variable where I have mentioned and declared variable,
p=zeros(7,1);
kpm = 10; kp =3;kmp = 14;klm = 15;kl = 1;theta=1;w=0.5;
p(1)=kpm;
p(2)=kp;
p(3)=kmp;
p(4)=klm;
p(5)=kl;
p(6)=theta;
p(7)=w;
tspan = 0:25;
yo=[0.2 0.05 0.539];
[t,y]=ode45(@(t,y)kumaretal2004new(t,y,p),tspan,yo);
for i=1:3
figure(i)
plot(t,y(:,i))
hold on
end
yexpected=zeros(26,3);
yexpected=table2array(Book65);
lb=zeros(7,1);
ub(1)=30;
ub(2)=30;
ub(3)=30;
ub(4)=30;
ub(5)=30;
ub(6)=30;
ub(7)=30;
A = [];
b = [];
Aeq = [];
beq = [];
parameters=fmincon(objectivefunction,p,A,b,Aeq,beq,lb,ub);
function dydt=kumaretal2004new(t,y,p)
dydt=zeros(3,1);
function qz=f(m)
qz=1+tanh((m-p(6))/p(7));
end
dydt(1)=(p(2))*y(1)*(1-y(1))-p(1)*y(1)*y(2);
dydt(2)=(p(3)*y(1)+y(3))*y(2)*(1-y(2))-y(2);
dydt(3)=p(4)*f(y(2))-p(5)*y(3);
end
function difference=objectivefunction(J)
tspan = 0:25;
N=size(tspan,2);
J=0;
for j=1:N
difference(j,1)=yexpected(j,1)-y(j,1);
difference(j,2)=yexpected(j,2)-y(j,2);
difference(j,3)=yexpected(j,3)-y(j,3);
J1=difference(j,1)^2;
J2=difference(j,2)^2;
J3=difference(j,3)^2;
J=J+J1+J2+J3;
end
end
  2 个评论
Stephen23
Stephen23 2023-7-17
This line of code:
parameters=fmincon(objectivefunction,p,A,b,Aeq,beq,lb,ub);
% ^^^^^^^^^^^^^^^^^
calls OBJECTIVEFUNCTION without any input arguments. It does NOT provide FMINCON with a function handle, as the FMINCON documentation states the 1st input must be. Solution: provide the 1st input as a function handle:
Note that you will need to parameterize the function to pass the variable YEXPECTED (and possibly others), just as the FMINCON documentation states near the top of the page:

请先登录,再进行评论。

回答(2 个)

Satwik Samayamantry
Hi Ayush,
Your code will throw the following error
Unrecognized function or variable 'Book65'.
Error in file1 (line 19)
yexpected=table2array(Book65);
This clearly indicates that you didn't declare the variable Book65. Hence, declare the variable before using it.
  3 个评论
Dyuman Joshi
Dyuman Joshi 2023-7-17
Where? How?
What is the error you get? Copy and paste the full error message i.e. all of the red text.
Ayush Ranjan
Ayush Ranjan 2023-7-17
I get the following error.
Unrecognized function or variable 'yexpected'.
Error in optimizertrial>objectivefunction (line 49)
difference(j,1)=yexpected(j,1)-y(j,1);
Error in optimizertrial (line 33)
parameters=fmincon(objectivefunction,p,A,b,Aeq,beq,lb,ub);
>>

请先登录,再进行评论。


Voss
Voss 2023-7-17
编辑:Voss 2023-7-17
yexpected is defined in the main script (i.e., the base workspace), but not in the function objectivefunction, where it is used.

类别

Help CenterFile Exchange 中查找有关 Parallel Computing Fundamentals 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by