Error in 1st line of code

2 visualizaciones (últimos 30 días)
Manali Kunte
Manali Kunte el 28 de Nov. de 2020
Comentada: Walter Roberson el 16 de Mzo. de 2025
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

Respuestas (2)

Anudeep Kumar
Anudeep Kumar el 14 de Mzo. de 2025
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 el 15 de Mzo. de 2025
@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 comentarios
Walter Roberson
Walter Roberson el 16 de Mzo. de 2025
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 el 16 de Mzo. de 2025
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by