ode45 not working
2 次查看(过去 30 天)
显示 更早的评论
Trying to solve this functions but it give me this error. Can some one find the error I have?
Im new to ode45
global m g r I ks
m = 5;
g = 9.81;
r = 0.470;
I = 0.37;
ks = 0.012;
dt = 0.01;
tspan = (0:dt:5);
x0 = [0,0];
[t,x] = ode45(funcionMAT,tspan,x0);
plot(t,x(:,1));
plot(t,x(:,2));
a = x(2)-x(1)/dt;
plot(t,a);
Error
Attempt to execute SCRIPT funcionMAT as a function:
D:\Users\Javier E. Negron\Documents\Trabajos de universidad (ene-may 2021)\Capstone\funcionMAT.m
Error in Analisis_de_movimiento2 (line 13)
[t,x] = ode45(funcionMAT,tspan,x0);
My function is
global m g r I ks
function xdot = Analisis_de_movimiento2(~,x)
A = [0,1;(-ks*x(1)-(m*g*r/2)*cos(x(1))+(2*pi/3)*ks)*x(1)/(I + m*r^2),0];
xdot = A * [x(1);x(2)];
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/611125/image.png)
0 个评论
回答(2 个)
Carly McKean
2023-11-15
I think you need to make these changes:
[t,x] = ode45(Analisis_de_movimiento2,tspan,x0);
and rename your file functionMAT to Analisis_de_movimiento2
0 个评论
Steven Lord
2023-11-15
The way you've written your funcionMAT file, it is a script file (because the first line does not start with either function or classdef.) Then you try to call it as though it were a function, but that doesn't work because it's a script. Get rid of the global line entirely (by parameterizing your function) and it becomes a function file.
In addition, the way you're calling ode45 attempts to call the funcionMAT function with 0 inputs and pass whatever that returns into ode45 as the first input argument. Instead you want to pass a function handle to funcionMAT into ode45. With the function handle, ode45 will be able to call funcionMAT with inputs of its choosing as needed.
[t,x] = ode45(@funcionMAT,tspan,x0); % Note the @ symbol
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!