필터 지우기
필터 지우기

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