ODE Fall 2012, A. Donev, CIMS numerically

advertisement
ODE Fall 2012, A. Donev, CIMS
Using the symbolic algebra package Maple to solve ODEs analytically and
numerically
restart:
with(plots):
ODE := diff(theta(t),t,t) + gamma*diff(theta(t),t) + omega^2*sin
(theta(t))=0; # Equations
(1)
I C s : = theta(0)=Theta, D(theta)(0)=Omega; # Initial conditions
(2)
dsolve({ODE, I C s} , t h e t a ( t ) ) ; # T r y t o c o m p u t e c l o s e d - f o r m
solution (no answer is returned)
1. Linearized ODE
The linearized ODE in which sin(theta) is replaced by theta can be solved analytically using the
methods we discussed in class.
Maple knows all of the recipies we discussed and can do the calculations without making mistakes:
LinearODE := diff(theta(t),t,t) + gamma*diff(theta(t),t) +
omega^2*theta(t)=0; # Linearization for small theta
(3)
s o l u t i o n : = s i m p l i f y ( d s o l v e ( { L i n e a r O D E , I C s} , t h e t a ( t ) ) ) ;
(4)
Now let's plug in some specific numbers for which complex numbers will be required:
numbers:={gamma=1, omega=sqrt(5/4), Theta=Pi/4, Omega=2};
(5)
Plug these numbers into the solution:
eval(solution,numbers);
(6)
By default Maple does not perform complex number simplifications, we need to use the evalc (evaluate
using complex arithmetic) function, and then simplify:
a p p r o x _ s o l u t i o n : = s i m p l i f y (e v a l c (e v a l ( s o l u t i o n , n u m b e r s ) ) ) ;
(7)
(7)
Let us now plot the approximate (linearized) solution theta(t):
p1:=plot(eval(theta(t),approx_solution), t=0..15, color=red):
display(p1);
0
5
10
15
t
2. Phase Plot
Let us now plot the trajectory in phase space, where the coordinates are [theta(t), theta'(t)]
y1:=eval(theta(t),approx_solution); # Position (angle) of
pendulum theta(t)
(8)
y2:=simplify(eval(diff(theta(t),t), approx_solution)); #
Velocity of pendulum theta'(t)
(9)
P 1 : = p l o t ( [ y 1, y2, t = 0 . . 1 5] , c o l o r = ' r e d ' , l a b e l s = [ " t h e t a ( t ) " ,
" t h e t a ' ( t ) " ]) :
display(P1);
2
theta'(t)
1
0
theta(t)
3. Numerical Solution
Finally, let's solve the true nonlinear ODE numerically using Maple's default numerical method (called
"RK4")
num_solution:=dsolve(eval({ODE, ICs} , n u m b e r s ) , t h e t a ( t ) , t y p e =
'numeric', range=0..15);
(10)
p2:=odeplot(num_solution, color='blue', style='point'):
Let's compare the numerical solution to the approximate (linearized) solution:
display(p1,p2);
0
5
10
15
t
And let's also compare the true phase plot to the approximate one:
P 2 : = o d e p l o t ( n u m _ s o l u t i o n , [ t h e t a ( t ) , d i f f ( t h e t a ( t ) , t ) ] , color=
' b l u e ' , s t y l e = ' p o i n t ') :
display(P1,P2);
2
theta'(t)
1
0
theta(t)
Download