Search All of the Math Forum:

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

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

 Messages: [ Previous | Next ]
 Torsten Posts: 1,717 Registered: 11/8/10
Re: simple harmonic oscillator using ode45 function
Posted: Dec 14, 2012 9:28 AM

"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.

Date Subject Author
12/14/12 Shane
12/14/12 Torsten
12/14/12 Shane
12/14/12 Steven Lord
1/24/13 Shane
1/24/13 Steven Lord
1/24/13 Shane
12/14/12 Steven Lord
4/16/13 Benjimon
5/28/13 Shane