ode45 not working

4 次查看(过去 30 天)
Javier Negron
Javier Negron 2021-5-7
回答: Steven Lord 2023-11-15
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

回答(2 个)

Carly McKean
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

Steven Lord
Steven Lord 2023-11-15
Don't use global variables. Parameterize your function instead.
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

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by