필터 지우기
필터 지우기

variable mentioned but still getting unrecognized variable error

조회 수: 3 (최근 30일)
Ayush Ranjan
Ayush Ranjan 2023년 7월 17일
편집: Voss 2023년 7월 17일
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
Garv Agarwal
Garv Agarwal 2023년 7월 17일
yexpected=table2array(Book65);
Where is the declararation of Book65?
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
Satwik Samayamantry 2023년 7월 17일
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