ODE_Maple.mw

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
 

`+`(diff(diff(theta(t), t), t), `*`(gamma, `*`(diff(theta(t), t))), `*`(`^`(omega, 2), `*`(sin(theta(t))))) = 0 (1)
 

> ICs := theta(0)=Theta, D(theta)(0)=Omega; # Initial conditions
 

theta(0) = Theta, (D(theta))(0) = Omega (2)
 

> dsolve({ODE, ICs}, theta(t)); # Try to compute closed-form 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
 

`+`(diff(diff(theta(t), t), t), `*`(gamma, `*`(diff(theta(t), t))), `*`(`^`(omega, 2), `*`(theta(t)))) = 0 (3)
 

> solution:=simplify(dsolve({LinearODE, ICs}, theta(t)));
 

theta(t) = `+`(`/`(`*`(`/`(1, 2), `*`(`+`(`*`(exp(`+`(`-`(`*`(`/`(1, 2), `*`(`+`(gamma, `-`(`*`(`^`(`+`(`*`(`^`(gamma, 2)), `-`(`*`(4, `*`(`^`(omega, 2))))), `/`(1, 2))))), `*`(t)))))), `*`(Theta, `*`...
theta(t) = `+`(`/`(`*`(`/`(1, 2), `*`(`+`(`*`(exp(`+`(`-`(`*`(`/`(1, 2), `*`(`+`(gamma, `-`(`*`(`^`(`+`(`*`(`^`(gamma, 2)), `-`(`*`(4, `*`(`^`(omega, 2))))), `/`(1, 2))))), `*`(t)))))), `*`(Theta, `*`...
theta(t) = `+`(`/`(`*`(`/`(1, 2), `*`(`+`(`*`(exp(`+`(`-`(`*`(`/`(1, 2), `*`(`+`(gamma, `-`(`*`(`^`(`+`(`*`(`^`(gamma, 2)), `-`(`*`(4, `*`(`^`(omega, 2))))), `/`(1, 2))))), `*`(t)))))), `*`(Theta, `*`...
(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};
 

{Omega = 2, Theta = `+`(`*`(`/`(1, 4), `*`(Pi))), gamma = 1, omega = `+`(`*`(`/`(1, 2), `*`(`^`(5, `/`(1, 2)))))} (5)
 

Plug these numbers into the solution: 

> eval(solution,numbers);
 

theta(t) = `+`(`-`(`*`(`/`(1, 8), `*`(`+`(`*`(`/`(1, 4), `*`(exp(`+`(`-`(`*`(`/`(1, 2), `*`(`+`(1, `-`(`*`(`^`(-4, `/`(1, 2))))), `*`(t)))))), `*`(Pi, `*`(`^`(-4, `/`(1, 2)))))), `*`(4, `*`(exp(`+`(`-...
theta(t) = `+`(`-`(`*`(`/`(1, 8), `*`(`+`(`*`(`/`(1, 4), `*`(exp(`+`(`-`(`*`(`/`(1, 2), `*`(`+`(1, `-`(`*`(`^`(-4, `/`(1, 2))))), `*`(t)))))), `*`(Pi, `*`(`^`(-4, `/`(1, 2)))))), `*`(4, `*`(exp(`+`(`-...
(6)
 

By default Maple does not perform complex number simplifications, we need to use the evalc (evaluate using complex arithmetic) function, and then simplify: 

> approx_solution:=simplify(evalc(eval(solution,numbers)));
 

theta(t) = `+`(`*`(`/`(1, 8), `*`(exp(`+`(`-`(`*`(`/`(1, 2), `*`(t))))), `*`(`+`(`*`(2, `*`(cos(t), `*`(Pi))), `*`(16, `*`(sin(t))), `*`(Pi, `*`(sin(t)))))))) (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);
 

Plot_2d
 

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)
 

`+`(`*`(`/`(1, 8), `*`(exp(`+`(`-`(`*`(`/`(1, 2), `*`(t))))), `*`(`+`(`*`(2, `*`(cos(t), `*`(Pi))), `*`(16, `*`(sin(t))), `*`(Pi, `*`(sin(t)))))))) (8)
 

> y2:=simplify(eval(diff(theta(t),t), approx_solution)); # Velocity of pendulum theta'(t)
 

`+`(`-`(`*`(`/`(1, 16), `*`(exp(`+`(`-`(`*`(`/`(1, 2), `*`(t))))), `*`(`+`(`*`(16, `*`(sin(t))), `*`(5, `*`(Pi, `*`(sin(t)))), `-`(`*`(32, `*`(cos(t)))))))))) (9)
 

> P1:=plot([y1, y2, t=0..15], color='red', labels=["theta(t)","theta'(t)"]):
 

> display(P1);
 

Plot_2d
 

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}, numbers), theta(t), type='numeric', range=0..15);
 

proc (x_rkf45) local _res, _dat, _vars, _solnproc, _xout, _ndsol, _pars, _n, _i; option `Copyright (c) 2000 by Waterloo Maple Inc. All rights reserved.`; if `<`(1, nargs) then error (10)
 

> p2:=odeplot(num_solution, color='blue', style='point'):
 

Let's compare the numerical solution to the approximate (linearized) solution: 

> display(p1,p2);
 

Plot_2d
 

And let's also compare the true phase plot to the approximate one: 

> P2:=odeplot(num_solution, [theta(t), diff(theta(t), t)], color='blue', style='point'):
 

> display(P1,P2);
 

Plot_2d
 

>