Date: Jan 24, 2013 9:44 AM
Author: Steven Lord
Subject: Re: simple harmonic oscillator using ode45 function
"Shane " <email@example.com> wrote in message
> "Steven_Lord" <firstname.lastname@example.org> wrote in message
> Hi Steve back after the winter break seen your help this is the code you
> asked for:
>>> dbtype 1:3 shm0.m
> 1 tspan=[0 50];
> 2 init=[-2 0];
> 3 [t,x]= ode45(shm0,tspan,init)
There are three problems here.
The first is that ODE45 expects its first input argument to be a function
handle[*] and you are calling it with whatever the shm0 function returns. If
shm0 returned a function handle, that would be okay. But this ties into the
second and third problems.
The second problem would be that before MATLAB can call ODE45 it needs to
call shm0 to determine what to pass into ODE45 as the first input. Doing
this requires shm0 to call shm0 to determine what to pass into ODE45 as the
first input. Doing this requires shm0 to call shm0 to determine what to pass
into ODE45 as the first input. ... We're going around in circles. You should
not call the ODE solvers and ask them to use the function in which you're
calling them as the ODE function first input. But actually we don't get this
far due to the third problem.
The third problem is that shm0 is a script not a function. That means that
[t, x] = ode45(shm0, tspan, init) % Which would require shm0 to return a
value to be used as ODE45's first input, but scripts don't return values.
[t, x] = ode45(@shm0, tspan, init) % Which would require shm0 to accept
input arguments and return an output argument when ODE45 calls it, neither
of which scripts do.
will work. If you want to keep shm0 a script, modify your ODE45 call to use
a function handle to a SEPARATE function to evaluate the right-hand side of
your system of ODEs. Personally, so that I could use a subfunction for that
purpose and not have to create a separate file, I'd turn shm0 into a
% begin shm0.m
function [t, x] = shm0
tspan = [0 50];
init = [-2 0];
[t, x] = ode45(@myODEfun, tspan, init);
function yprime = myODEfun(t, y)
% Evaluate the RHS of your ODE system here
% end shm0.m
To contact Technical Support use the Contact Us link on
[*] Yes, I know ODE45 _can_ accept things other than function handles as its
first input, but I recommend that you don't call the ODE solvers with
anything but some flavor ("named" or anonymous) of function handle.