Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: simple harmonic oscillator using ode45 function
Replies: 9   Last Post: May 28, 2013 9:04 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Shane

Posts: 5
Registered: 12/14/12
Re: simple harmonic oscillator using ode45 function
Posted: Dec 14, 2012 11:37 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Torsten" wrote in message <kafd1n$ah8$1@newscl01ah.mathworks.com>...
> "Shane" wrote in message <kaf8sj$q32$1@newscl01ah.mathworks.com>...
> > Hi,
> > new to matlab and struggling to grasp the language. My problem is I have been given the equation:
> >
> > x" +ax' + bx = f(t)
> >
> > solved using the ode45 function
> >
> > first for f(t) = 0 and then when f(t)= 1
> >
> > the point is to study the influence of the variables a and b, I'm sure i will be comfortable enough to evaluate these myself once I have the code.
> >
> > Any and all help would be appreciated,
> > Thanks Shane

>
> a=2;
> b=3;
> f=0;
> y1_0 = 0.0; % set x at t=0
> y2_0 = 1.0; % set x' at t=0
> options = odeset('RelTol',1e-5,'AbsTol',1e-5);
> [T,Y] = ode45(@(t,y)oscillator(t,y,a,b,f),[0 12],[y1_0 y2_0],options);
>
> function dy = oscillator(t,y,a,b,f)
> dy = zeros(2,1); % a column vector
> dy(1) = y(2) ;
> dy(2) = -a*y(2)-b*y(1)+f;
>
> But note that for this simple ode, the easiest way to analyze the influence of the input parameters is to solve the ODE analytically.
> Look up the "dsolve" command in MATLAB.
>
> Best wishes
> Torsten

-------------------------------------------------------------------
Thanks for the help Torsten
I've been struggling with it since I posted the first message and I've gotten somewhere at least, I solved the problem first by creating two first order equations and tried creating a function :
----------code----------------
function xp = f1(t,x)
a=1;
b=1;
xp= zeros(2,1);
xp(1)=x(2);
xp(2)= -a*x(2) - b*x(1);
------------------------------------------
although when I try to run the function to evaluate the problem I always get an error message:
---------------code---------
tspan=[0,50];
init=[-2;0];
a= 1;
b=2;
[t,x]=ode45(@1,tspan,init);
{Error using <a href="matlab:helpUtils.errorDocCallback('feval')" style="font-weight:bold">feval</a>
Error: <a href="matlab: opentoline('Q:\MATLAB\shm0.m',1,29)">File: shm0.m Line: 1 Column: 29</a>
Unbalanced or unexpected parenthesis or bracket.

Error in <a href="matlab:helpUtils.errorDocCallback('odearguments', 'C:\Program Files\matlab\R2012a\toolbox\matlab\funfun\private\odearguments.m', 88)" style="font-weight:bold">odearguments</a> (<a href="matlab: opentoline('C:\Program Files\matlab\R2012a\toolbox\matlab\funfun\private\odearguments.m',88,0)">line 88</a>)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in <a href="matlab:helpUtils.errorDocCallback('ode45', 'C:\Program Files\matlab\R2012a\toolbox\matlab\funfun\ode45.m', 114)" style="font-weight:bold">ode45</a> (<a href="matlab: opentoline('C:\Program Files\matlab\R2012a\toolbox\matlab\funfun\ode45.m',114,0)">line 114</a>)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
}

---------------------------

sorry its a bit of a mess but again any help would be greatly appreciated
Shane



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.