Error in 1st line of code

2 次查看(过去 30 天)
Manali Kunte
Manali Kunte 2020-11-28
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