Error in 1st line of code

조회 수: 2 (최근 30일)
Manali Kunte
Manali Kunte 2020년 11월 28일
댓글: Walter Roberson 2025년 3월 16일
Hello. I am new to MATLAB and keep on getting this error every single time:
Error in MSDSOLV (line 1)
[T,Y]= ode45(@MSD,[0 10],[0.3,-0.1]);
This is my MSD code for the differential equations:
function dx=MSD (t,x);
dx=zeroes(2,1);
A=[0 1;-1 -2]
B=[0;1]
J=[-1+i -1-i]
K=acker(A,B,J)
u=-K*[x(1);x(2)]
dx(1)=x(2);
dx(2)=-x(1)-2*x(2)+u;
and this is my solver code for teh differential equations:
[T,Y]= ode45(@MSD,[0:0.1:10],[0.3,-0.1]);
figure(1)
plot(T,Y(:,1),'b')
grid
xlabel('time (s)')
ylabel ('x_1')
Thanks in advance

답변 (2개)

Anudeep Kumar
Anudeep Kumar 2025년 3월 14일
Hey Manali,
The issue seems to be in the first line of the definition of “MSD” function.
You have used “zeroes” instead of “zeros” which is the actual defined function in MATLAB. You can make the following change to line 1 of your function definition:
>>dx=zeros(2,1);
Here is the link to the documentation for the function "zeros()" :
Hope that helps!

VBBV
VBBV 2025년 3월 15일
@Manali Kunte, use the limits for time span instead of vector, Besides, there is typo error for function zeroes. The valid Matlab function is zeros
[T,Y]= ode45(@MSD,[0 10],[0.3,-0.1]);
% use the limits for time span instead of vector
figure(1)
plot(T,Y(:,1),'b')
grid
xlabel('time (s)')
ylabel ('x_1')
function dx=MSD (t,x);
dx=zeros(2,1); % use zeros instead of zeroes
A=[0 1;-1 -2];
B=[0;1];
J=[-1+i -1-i];
K=acker(A,B,J);
u=-K*[x(1);x(2)];
dx(1)=x(2);
dx(2)=-x(1)-2*x(2)+u;
end
  댓글 수: 8
Walter Roberson
Walter Roberson 2025년 3월 16일
Back in R2012a
>> [T,Y]= ode45(@MSD,0,[0.3,-0.1])
Error using odearguments (line 22)
When the first argument to ode45 is a function handle, the tspan
argument must have at least two elements.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs,
odeFcn, ...
Walter Roberson
Walter Roberson 2025년 3월 16일
I went through a lot of trouble to install Windows XP and R14. Unfortunately R14 needs PLP, and that needs assistance from Mathworks Support to deal with these days. So I installed R2008a (after notable drama).
>> [T,Y]= ode45(@MSD,0,[0.3,-0.1])
??? Error using ==> odearguments at 24
When the first argument to ode45 is a function handle, the tspan
argument must have at least two elements.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs,
odeFcn, ...
So if there is an ode45 version that has a problem with tspan that does not have two elements, then it must be before R2008a.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by