please someone solve this error ''Undefined function or variable 'x''.

3 ビュー (過去 30 日間)
Meenakshi Tripathi
Meenakshi Tripathi 2021 年 7 月 22 日
編集済み: Yongjian Feng 2021 年 7 月 22 日
function ross
L = [-1.0828 -1.6242;1.2513 1.8770;-0.0020 -0.0029];
master = [-x(2)-x(3);x(1)+0.5*x(2);2+x(3)*x(1)-4*x(3)];
slave = [-x(5)-x(6);x(4)+x(7)*x(5);2+x(6)*x(4)-4*x(6)]-L*y;
thetahat = 2*x(5)*[-0.2308 0.1538]*y;
y =[12*x(1)+2*x(2)+3*x(3)-12*x(4)-x(5)-3*x(6)-2*x(5)*thetahat;7*x(1)+3.5*x(2)+2*x(3)-7*x(4)-2*x(5)-2*x(6)-3*x(5)*thetahat]
x0 = [1 1 1 -5 -5 -5 0];
tspan= [0:0.001:70];
options = odeset('RelTol',1e-5,'AbsTol',1e-5);
[t,x]=ode45(@ross,tspan,x0,options)
function dxdt = ross(t,x)
dxdt = [master;slave;thetahat]
end
end
error is-
Undefined function or variable 'x'.
Error in ross (line 4)
master = [-x(2)-x(3);x(1)+0.5*x(2);2+x(3)*x(1)-4*x(3)];
  3 件のコメント
Meenakshi Tripathi
Meenakshi Tripathi 2021 年 7 月 22 日
sorry! I am a beginner so could you please tell me how to define x?
Rik
Rik 2021 年 7 月 22 日
If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).

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

採用された回答

Yongjian Feng
Yongjian Feng 2021 年 7 月 22 日
編集済み: Yongjian Feng 2021 年 7 月 22 日
It seems like lines are all messed up. Do it make more sense to re-arrange them like this:
x0 = [1 1 1 -5 -5 -5 0];
tspan= [0:0.001:70];
options = odeset('RelTol',1e-5,'AbsTol',1e-5);
[t,x]=ode45(@ross,tspan,x0,options);
% This looks more like the function. Now x and t are input parameters, and
% they can be used inside the function
function dxdt = ross(t,x)
%function ross
L = [-1.0828 -1.6242;1.2513 1.8770;-0.0020 -0.0029];
master = [-x(2)-x(3);x(1)+0.5*x(2);2+x(3)*x(1)-4*x(3)];
y =[12*x(1)+2*x(2)+3*x(3)-12*x(4)-x(5)-3*x(6)-2*x(5)*thetahat;7*x(1)+3.5*x(2)+2*x(3)-7*x(4)-2*x(5)-2*x(6)-3*x(5)*thetahat];
slave = [-x(5)-x(6);x(4)+x(7)*x(5);2+x(6)*x(4)-4*x(6)]-L*y;
thetahat = 2*x(5)*[-0.2308 0.1538]*y;
%y =[12*x(1)+2*x(2)+3*x(3)-12*x(4)-x(5)-3*x(6)-2*x(5)*thetahat;7*x(1)+3.5*x(2)+2*x(3)-7*x(4)-2*x(5)-2*x(6)-3*x(5)*thetahat]
% The following lines moved up.
%x0 = [1 1 1 -5 -5 -5 0];
%tspan= [0:0.001:70];
%options = odeset('RelTol',1e-5,'AbsTol',1e-5);
%[t,x]=ode45(@ross,tspan,x0,options)
% function dxdt = ross(t,x)
dxdt = [master;slave;thetahat]
%end
end
  2 件のコメント
Rik
Rik 2021 年 7 月 22 日
Actually the relevant defnition of x is in the function signature:
function dxdt = ross(t,x)
% ^ here
Yongjian Feng
Yongjian Feng 2021 年 7 月 22 日
編集済み: Yongjian Feng 2021 年 7 月 22 日
You are right! Modified again.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by