필터 지우기
필터 지우기

Error using ode45

조회 수: 1 (최근 30일)
Javier Negron
Javier Negron 2021년 5월 7일
댓글: Jan 2021년 5월 8일
Trying to solve this functions but it give me this error.
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);
  댓글 수: 1
James Tursa
James Tursa 2021년 5월 7일
편집: James Tursa 2021년 5월 7일
The beginning of your funcionMAT.m file should look like this:
function dy = funcionMAT(t,y)
And then you should use a function handle when calling ode45:
[t,x] = ode45(@funcionMAT,tspan,x0);

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2021년 5월 7일
The error message means, that you need a function, because the integrator uses input and output arguments for the function to be integrated:
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(@(t,y) funcionMAT(t,y, m,g,r,I,ks), tspan, x0);
% And your functionMAT file:
function dy = funcionMAT(t,y, m,g,r,I,ks)
dy = [y(1) * m + g; ... % Insert your function here
y(2)]
end
The @(t,y) functionMAT(t,y,...) method is used to provide the parameters to the function. ODE45 would call the function the inputs t and y only. This is much smarter than using global variables.
  댓글 수: 2
Javier Negron
Javier Negron 2021년 5월 7일
I actualy have a function file
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
But I don't know if is not well called or some script error
Thanks in advance!
Jan
Jan 2021년 5월 8일
If you use global variables to provide parameters, the global statement must appear in each function, which uses the global variables.
function xdot = Analisis_de_movimiento2(~,x)
global m g r I ks % Inside
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
Global variables impede the debugging massively and therefore avoiding them is a good programming practice. Use an anonymous function instead as shown in my answer. See also:

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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