Clear Filters
Clear Filters

variable mentioned but still getting unrecognized variable error

2 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 17 Jul 2023
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:

Sign in to comment.

Answers (2)

Satwik Samayamantry
Satwik Samayamantry on 17 Jul 2023
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 Comments
Dyuman Joshi
Dyuman Joshi on 17 Jul 2023
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 on 17 Jul 2023
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);
>>

Sign in to comment.


Voss
Voss on 17 Jul 2023
Edited: Voss on 17 Jul 2023
yexpected is defined in the main script (i.e., the base workspace), but not in the function objectivefunction, where it is used.

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!