Shane
Posts:
5
Registered:
12/14/12


Re: simple harmonic oscillator using ode45 function
Posted:
Jan 24, 2013 10:03 AM


"Steven_Lord" <slord@mathworks.com> wrote in message <kdrhc6$o8r$1@newscl01ah.mathworks.com>... > > > "Shane " <shane_t@hotmail.co.uk> wrote in message > news:kdrf88$fp6$1@newscl01ah.mathworks.com... > > "Steven_Lord" <slord@mathworks.com> wrote in message > > <kafm1a$c49$1@newscl01ah.mathworks.com>... > > *snip* > > > 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 > neither this: > > [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. > > nor this: > > [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 righthand 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 > function. > > > % 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 > > >  > Steve Lord > slord@mathworks.com > To contact Technical Support use the Contact Us link on > http://www.mathworks.com > > [*] 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.
 I think i understand Steve the function is calling itself with no value, so if i use the text you suggested then follow it with: > > a=1; > > b=1; > > xprime= zeros(2,1); > > xprime(1)=x(2); > > xprime(2)= a*x(2)  b*x(1)
then it should run fine if I just typed in shm0 as this would call the function with the values given? Thanks again Shane

