Error in 1st line of code

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

Answers (2)

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!
@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 Comments

Using a sorted vector (ascending or descending) of more than two elements for the time span is completely valid.
Ok. May there were some updates for the syntax used in function ode45. I did encounter issues about the number of elements for time span.
As far as I can remember, ode45 has supported a vector with more than 2 elements as the tspan input for at least a couple decades. If you specify a 2 element vector, ode45 will "decide" at which intermediate times it will return the solution of the system of ODEs. If you specify a > 2 element vector, ode45 will return the solution at those times (though it reserves the right to use different times internally and interpolate to the specified times to create the output.)
If you use a two-element vector, then with the default options, it outputs every accepted point, and also outputs another three points interpolated between the last accepted point and the current accepted point. This is controlled by the Refine option; https://www.mathworks.com/help/matlab/ref/odeset.html#bu2m9z6-Refine

Ok. @steven lord, you can see the error message line in which, the default time span limits allows two element vector only. However, that is no longer an issue now. :)

Here is the error message
[T,Y]= ode45(@MSD,0,[0.3,-0.1]);
Error using odearguments (line 21)
When the first argument to ode45 is a function handle, the tspan argument must have at least two elements.

Error in ode45 (line 104)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
It says the tspan must have at least two elements.
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, ...
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.

Sign in to comment.

Tags

Asked:

on 28 Nov 2020

Commented:

on 16 Mar 2025

Community Treasure Hunt

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

Start Hunting!