필터 지우기
필터 지우기

Error in using ode45 to solve a second-order ODE

조회 수: 1 (최근 30일)
Chinmay Sridhar
Chinmay Sridhar 2018년 8월 23일
댓글: Chinmay Sridhar 2018년 8월 23일
I am trying to solve a fairly simple second-order ODE using the ode45 command. I've read the documentation and created a file containing the function to be solved, having reduced it to state space form. The file is called function1dof.m
m = 12.319*0.25*0.036;
g = 386.1; % inch/s^2
L = 8.6; Lc = L/2;
I = 0.653*2;
u = 0.5; % Torque input in lb-in
function func = F(t,x)
func = zeros(2,1);
func(1) = x(2);
func(2) = (1/(Lc^2 + I))*(u - m*g*Lc*sin(x(1)));
end
Then, I created a second .m file, called solver1dof.m, to execute the ode45 command and (hopefully) solve the equation.
clear all
time_period = [0 2];
initial = [0, 0];
[t,x] = ode45(@F, time_period, initial);
However, running solver1dof.m gives an error claiming the function 'F' is undefined:
>> solver1dof
Undefined function or variable 'F'.
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in solver1dof (line 4)
[t,x] = ode45(@F, time_period, initial);
I am unsure on how to proceed, as it seems the function is perfectly well defined, as far as I can tell (obviously, it isn't).

채택된 답변

Steven Lord
Steven Lord 2018년 8월 23일
As stated in this documentation page, "Local functions are only visible within the file where they are defined, both to the script code and other local functions within the file. They are not visible to functions in other files, and cannot be called from the command line." The local function F defined inside the script file function1dof is not visible to script file solver1dof.
If you created a function handle to F inside function1dof, executed function1dof, and used that function handle variable inside solver1dof then the ode45 call inside solver1dof could start. But the variables defined in the script file function1dof are not visible inside the function F, so you will receive an error about an undefined function or variable Lc when ode45 tries to call F using the function handle.
Either move the definitions of those variables into F itself (at which point you might as well just make function1dof a function file rather than a script file) or define them in solver1dof and pass them into F as parameters.
  댓글 수: 3
Steven Lord
Steven Lord 2018년 8월 23일
Now that you've made function1dof into a function file (its first executable line starts with the function keyword) you don't need to execute it first. However, know that MATLAB will know that function by the name function1dof (its file name) as opposed to F (the name used in the function declaration line.) When they differ, the file name trumps the function declaration line.
So you're going to need to use @function1dof in your ode45 call.
Chinmay Sridhar
Chinmay Sridhar 2018년 8월 23일
It works now! Thank you for being patient, I realize these are very simple problems.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by