matlab出现错误怎么解决function particle % y(1)=xp, y(2)=upx, y(3)=yp, y(4)=upy, y(5)=zp, y(6)=upzclear allclcy0=[0; 0; 0; 0; 0; 0];t= [0 40];[t y] = ode23(@f,[0 40],y0);plot3(y(:, 1), y(:, 3), y(:, 5));axis([-50 50 -50 50 -50 0])grid on xla

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/26 16:59:40
matlab出现错误怎么解决functionparticle%y(1)=xp,y(2)=upx,y(3)=yp,y(4)=upy,y(5)=zp,y(6)=upzclearallclcy0=[0;0;

matlab出现错误怎么解决function particle % y(1)=xp, y(2)=upx, y(3)=yp, y(4)=upy, y(5)=zp, y(6)=upzclear allclcy0=[0; 0; 0; 0; 0; 0];t= [0 40];[t y] = ode23(@f,[0 40],y0);plot3(y(:, 1), y(:, 3), y(:, 5));axis([-50 50 -50 50 -50 0])grid on xla
matlab出现错误怎么解决
function particle
% y(1)=xp, y(2)=upx, y(3)=yp, y(4)=upy, y(5)=zp, y(6)=upz
clear all
clc
y0=[0; 0; 0; 0; 0; 0];
t= [0 40];
[t y] = ode23(@f,[0 40],y0);
plot3(y(:, 1), y(:, 3), y(:, 5));
axis([-50 50 -50 50 -50 0])
grid
on
xlabel('Xp')
ylabel('Yp')
zlabel('Zp')
title('Trajectory of the Particle')
%--------------------------------------------------

function dydt=f(t,y)
ST=70;
gamma=1;
A=6.5;
dydt=[
y(2)
ST(sin(2*pi*y(3))-y(2))+gamma*2*pi(sin(2*pi*y(1))*cos(2*pi*y(3))+(cos(2*pi*y(3)))^2)
y(4)
ST(sin(2*pi*y(1))+cos(2*pi*y(3))-y(4))+gamma*2*pi(sin(2*pi*y(3))*cos(2*pi*y(3))-sin(2*pi*y(3))*sin(2*pi*y(1))-sin(2*pi*y(3))*cos(2*pi*y(3)))
y(6)
A+ST(cos(2*pi*y(1))-y(6))-gamma*2*pi*sin(2*pi*y(3))*sin(2*pi*y(1))];
出现下面的错误
? Subscript indices must either be real positive integers or logicals.
Error in ==> Untitled>f at 28
dydt=[
Error in ==> odearguments at 110
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode23 at 172
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in ==> Untitled at 10
[t y] = ode23(@f,[0 40],y0);

matlab出现错误怎么解决function particle % y(1)=xp, y(2)=upx, y(3)=yp, y(4)=upy, y(5)=zp, y(6)=upzclear allclcy0=[0; 0; 0; 0; 0; 0];t= [0 40];[t y] = ode23(@f,[0 40],y0);plot3(y(:, 1), y(:, 3), y(:, 5));axis([-50 50 -50 50 -50 0])grid on xla
原因:
前面赋值ST是一个标量,后面dydt表达式里有ST(...),这种写法会被理解成ST是一个数组,而括号里面的内容则是数组的下标.在调用的时候,括号里的值为0,MATLAB的数组下标从1开始,所以就会导致报这样的错误.
分析:
你的表达式ST(...)是不是想要表示ST*(...
一共有三处ST需要修改,另外还有两处pi存在类似的问题.
除此之外,还有一个小问题:grid on 被你写在两行里了.
改后的代码如下:
function particle
% y(1)=xp,y(2)=upx,y(3)=yp,y(4)=upy,y(5)=zp,y(6)=upz
clear all
clc
y0=[0; 0; 0; 0; 0; 0];
t= [0 40];
[t y] = ode23(@f,[0 40],y0);
plot3(y(:,1),y(:,3),y(:,5));
axis([-50 50 -50 50 -50 0])
grid on
xlabel('Xp')
ylabel('Yp')
zlabel('Zp')
title('Trajectory of the Particle')
%--------------------------------------------------
function dydt=f(t,y)
ST=70;
gamma=1;
A=6.5;
dydt=[
y(2)
ST*(sin(2*pi*y(3))-y(2))+gamma*2*pi*(sin(2*pi*y(1))*cos(2*pi*y(3))+(cos(2*pi*y(3)))^2)
y(4)
ST*(sin(2*pi*y(1))+cos(2*pi*y(3))-y(4))+gamma*2*pi*(sin(2*pi*y(3))*cos(2*pi*y(3))-sin(2*pi*y(3))*sin(2*pi*y(1))-sin(2*pi*y(3))*cos(2*pi*y(3)))
y(6)
A+ST*(cos(2*pi*y(1))-y(6))-gamma*2*pi*sin(2*pi*y(3))*sin(2*pi*y(1))];