- Anonymous functions - https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
- ode45 - https://www.mathworks.com/help/matlab/ref/ode45.html
My program prompts the following error, how can I solve it
    4 次查看(过去 30 天)
  
       显示 更早的评论
    
clc;
clear;
y0=[0.2 0.6 0.8 1.1 1.5 1.9 2.5 2.7 3 4];
e0=[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1];
tspan=[0 10];
[t,y]=ode45(@shuru,tspan,y0,e0);
figure(1)
plot(t,y)
function dy=shuru(t,y,e)
dy=zeros(10,1);
dy(1)=sin(t)-y(1)+cos(t)-100*sat(t*(y(1)-y(2)))+cos(t)*(4-sin(t)*e(1))-e(1);
dy(2)=sin(2*t)-y(2)+cos(2*t)/2-100*(sat(t*(y(2)-y(3)))+sat(t*(y(2)-y(8))))+cos(2*t)*(4-sin(2*t/2)*e(2))-e(2);
dy(3)=sin(3*t)-y(3)+cos(3*t)/3-100*(sat(t*(y(3)-y(4)))+sat(t*(y(3)-y(9))))+cos(3*t)*(4-sin(3*t/3)*e(3))-e(3);
dy(4)=sin(4*t)-y(4)+cos(4*t)/4-100*(sat(t*(y(4)-y(5)))+sat(t*(y(4)-y(8))))+cos(4*t)*(4-sin(4*t/4)*e(4))-e(4);
dy(5)=sin(5*t)-y(5)+cos(5*t)/5-100*(sat(t*(y(5)-y(6)))+sat(t*(y(5)-y(7))))+cos(5*t)*(4-sin(5*t/5)*e(5))-e(5);
dy(6)=sin(6*t)-y(6)+cos(6*t)/6-100*sat(t*(y(6)-y(7)))+cos(6*t)*(4-sin(6*t/6)*e(6))-e(6);
dy(7)=sin(7*t)-y(7)+cos(7*t)/6-100*sat(t*(y(7)-y(8)))+cos(7*t)*(4-sin(7*t/7)*e(7))-e(7);
dy(8)=sin(8*t)-y(8)+cos(8*t)/6-100*sat(t*(y(8)-y(9)))+cos(8*t)*(4-sin(8*t/8)*e(8))-e(8);
dy(9)=sin(9*t)-y(9)+cos(9*t)/6-100*sat(t*(y(9)-y(10)))+cos(9*t)*(4-sin(9*t/9)*e(9))-e(9);
dy(10)=sin(10*t)-y(10)+cos(10*t)/6-100*sat(t*(y(10)-y(1)))+cos(10*t)*(4-sin(10*t/10)*e(10))-e(10);
end
function de=wucha(t,e)
de(n)=-1-cos(n*t)*(4-sin(n*t)*e(n));
end
function M=sat(x)
k=0.01;
d=abs(x/k);
if d<=1
    M=x/k;
else 
    M=sign(x/k);
end
error shuru (line 3)
dy(1)=sin(t)-y(1)+cos(t)-100*sat(t*(y(1)-y(2)))+cos(t)*(4-sin(t)*e(1))-e(1);
error odearguments (line 90)
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.
error ode45 (line 115)
  odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
error DAT (line 7)
[t,y]=ode45(@shuru,tspan,y0,e0);
0 个评论
回答(1 个)
  ag
      
 2025-4-13
        Hi JlAxin,
The problem that you are encountering is because the "ode45" function expects the ODE function to have the signature f(t, y), where "t" is the independent variable and "y" is the dependent variable. The extra parameter "e" in the function "shuru" is not supported directly by "ode45". 
The below code snippet demonstrates how to resolve this issue:
% Define an anonymous function to include the additional parameter e0
odefun = @(t, y) shuru(t, y, e0);
[t, y] = ode45(odefun, tspan, y0);
For more details, please refer to the following MathWorks documentations:
Hope this helps!
0 个评论
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
			
	产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

