ode45 not working

13 Ansichten (letzte 30 Tage)
Javier Negron
Javier Negron am 7 Mai 2021
Beantwortet: Steven Lord am 15 Nov. 2023
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

Antworten (2)

Carly McKean
Carly McKean am 15 Nov. 2023
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 am 15 Nov. 2023
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

Tags

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by