Diff Equations
Diff Equations
Diff Equations
MATLAB
ALBERTO CASTEJON,
EUSEBIO CORBACHO, BRAN TRIGO, AND RICARDO VIDAL
Abstract. Since the scientific revolution by Newton and Leibnitz in the XVIIth century, a number of technical problems are solved finding for a function
f : [t0 , t0 + T ] Rn Rn a curve x : [t0 , t0 + T ] Rn such that
(
x0 (t) = f (t, x(t)) t [t0 , t0 + T ]
.
x(t0 ) = p0
For a choose collection of problems in RLC circuits, ballistics in the plane and
trajectories of particles in the space under the action of a linear oscillator we
find the correspondent function f (t, x) which is of the form Ax + u(t) with
A Mn (R). In this case we can present interact programs in SAGE which
provides an analytical solution of the problem.
For other type of problems like periodical trajectories, ecosystem evolution,
chase curves, hamiltonian mechanics and traction curves, we obtain functions
f (t, x) which are not linear with constant coefficients. For these cases we
have programmed in SAGE the Euler and Runge-Kutta numerical methods by
direct translation from the theory and we present the corresponding numerical
solutions when it is possible. When it is not possible we present analogs
programs in MATLAB which works in the direction predicted by the theory.
1. Introduction
Since the Newton-Leibnitz scientific revolution in the XVII-th century, a number
of technical problems are solved finding, for a function f : [t0 , t0 + T ] Rn Rn ,
a curve x : [t0 , t0 + T ] Rn such that x0 (t) = f(t, x(t)) t [t0 , t0 + T ].
In very general setting for the function f we know that, fixed a p0 Rn , there
exists just one curve x : [t0 , t0 + T ] Rn such that
(
x0 (t) = f(t, x(t)) t [t0 , t0 + T ]
x(t0 ) = p0
called the solution of this Cauchy problem with initial condition p0 . In the following
result we remember some of these conditions:
Teorema 1.1.
(1) If f is continuous in a neighbourhood of (t0 , p0 ), exists a 0 < T 0 T and
a function x : [t0 , t0 + T 0 ] Rn such that
(
x0 (t) = f(t, x(t)) t [t0 , t0 + T ]
x(t0 ) = p0
(2) If f is continuous and exists a function ` L1 ([t0 , t0 + T ]) fulfilling one
ofthe following conditions
(a) (f(t, x)|x) `(t)(1 + kxk22 ) (t, x) [t0 , t0 + T ] Rn
1
ALBERTO CASTEJON,
EUSEBIO CORBACHO, BRAN TRIGO, AND RICARDO VIDAL
0
q
+ V
R
i
sin t
L
L
1
x0 = v
v 0 = g v
m
Writing X = xv , we get the four dimensional equation
0 0
1
0
0
0 0
0
1
X + 0 .
X0 =
0 0
0
0
m
9.8
0 0
0
m
Applying
the (?) formula, we can solve this equation for any initial condition
x0
X0 =
being x0 the initial position and v0 the initial speed.
v0
If we suppose that in an interval of time [0, T0] [0, T ] it acts on the missile a
propulsing force : [0, T0] R2 , we need solve a first problem for
0 0
1
0
0
0 0
0
1
0
X +
in [0, T0 ]
f1 (t, X) =
0 0
0
1 (t)
m
9.8 + 2 (t)
0 0
0
m
ALBERTO CASTEJON,
EUSEBIO CORBACHO, BRAN TRIGO, AND RICARDO VIDAL
0 0
0 0
f2 (t, X) =
0 0
0 0
m
0
0
0
1
X + 0
0
0
9.8
m
in
[T0 , T ]
with initial condition X1 . Adding both we get the global solution in [0, T ]. The
function missile gives us the solution:
def missile(m,b,T,F,T0,CI):
N=matrix(2)
U=identity_matrix(2)
A=block_matrix([[N,U],[N,-(b/m)*U]])
S=exp(A*t)
Ss=S(t=t-s)
u=vector([0,0,F[0]/m,-9.8+F[1]/m])
B=Ss*u
Xh=S*transpose(CI)
XP=integral(B,s,0,t)
X=Xh+transpose(XP)
CI1=X(t=T0)
u0=vector([0,0,0,-9.8])
S0=S(t=t-T0)
Xh0=S0*CI1
B0=Ss*u0
XP0=integral(B0,s,T0,t)
X0=Xh0+transpose(XP0)
return [X,X0]
2.3. Linear oscillators.
Let L : R3 R3 be a linear force field. Taking an adequate basis of eigenvectors
we have essentially two possibilities for the matrix representation of L:
k1 0 0
k1
0
0
D = 0 k2 0 or N = 0
k2 k3
0 0 k3
0 k3 k2
+ u
D
I
v0
v
b
m
m
m
3
being O and I the
zero and the identity in M3 (R) and 0 the zero of R .
x
Writing X = v , we find the six dimensional equation
I
O
0
0
m
X = AX + U with A = D
and U = u
I
b
m
m
m
ALBERTO CASTEJON,
EUSEBIO CORBACHO, BRAN TRIGO, AND RICARDO VIDAL
with step h =
T
N.
k = 0, , N
tk
Fk1 = f(tk , xk )
h
h
h
h
2
2
For our interest these two methods are sufficient. For its implementation in SAGE
we have defined the functions substitution, euler and rungekutta:
def substitution(f,E,F):
n=len(E)
f0=copy(f)
for k in [0..n-1]:
a=E[k]
S=str(f0).replace(str(a),str(F[k]))
f0=S
f0=vector(sage_eval(S,locals={t:t}))
return f0
def euler(f,X,CI,I,N):
h=(I[1]-I[0])/N
A=CI
B=[CI]
for n in [0..N-1]:
tn=I[0]+n*h
f0=f(t=tn)
A=A+h*substitution(f0,X,A).n()
B.append(A)
return B
def rungekutta(f,X,CI,I,N):
h=(I[1]-I[0])/N
A=CI
B=[CI]
for n in [0..N-1]:
tn=I[0]+n*h
f0=f(t=tn)
Fn1=substitution(f0,X,A).n()
f1=f(t=tn+h/2)
Fn2=substitution(f1,X,A+h*Fn1/2).n()
Fn3=substitution(f1,X,A+h*Fn2/2).n()
f2=f(t=tn+h)
Fn4=substitution(f2,X,A+h*Fn3).n()
A=A+h*(Fn1+2*Fn2+2*Fn3+Fn4)/6
B.append(A)
return B
We will use these functions in several examples:
3.1. Periodical trajectories.
The trajectory of a mobile is governed by the differential equation
cos( 2t ) sin( 2t )
x0 (t) =
.x(t)
sin( 2t ) cos( 2t )
If in the initial moment the mobile is in the point (1,1), which is the trajectory in
a period [0, 4]?
The differential equation is linear but without constant coefficients and the (?)
formula doesnt work. The numerical solution has been programmed as follows:
var(t x1 x2)
X=vector([x1,x2])
f=vector([x1*cos(t/2)-x2*sin(t/2),x1*sin(t/2)+x2*cos(t/2)])
CI=vector([1,1])
S=rungekutta(f,X,CI,[0,4*pi.n()],200)
g=line2d(S,rgbcolor=(0,1,0),thickness=1)
movil=[]
for k in srange(0,200,4):
movil.append(g+point(S[k],color=(1,0,0),pointsize=30))
a=animate(movil)
a.show(delay=10)
ALBERTO CASTEJON,
EUSEBIO CORBACHO, BRAN TRIGO, AND RICARDO VIDAL
r(t) =
c(t) x(t)
kx0 (t)k.
kc(t) x(t)k
kx0 (t)k
kc0(t)k
c(t) x(t)
kc(t) x(t)k
and, for every initial position of the pursuer x(t0 ) = x0 , we will have just one curve
as its solution. In our case the program of the numerical solution is:
var(t x1 x2 x3)
C=vector([2000*cos(t/10),2000*sin(t/10),3000])
CI=vector([0,0,0])
I=[0,100]
N=100
r=1
X=vector([x1,x2,x3])
VP=diff(C,t)
f=.9*((C-X)/norma(C-X))*200
S=rungekutta(f,X,CI,I,N)
g=line3d(S,rgbcolor=(0,1,0),thickness=5)
a=parametric_plot3d(C,(t,0,I[1]),rgbcolor=(1,0,0),thickness=5)
show(g+a)
3.4. Hamiltonian Mechanics.
The hamiltonian function of a particle of mass 1 in R3 is
1
1
H(x, v) = (v|v)
.
2
kxk
Calculate its trajectory in the interval of time [0, 118], being
x(0) = (10, 0, 3) and v(0) = (0.1, 0.2, 0).
If the kinetic energy of a system in R3 is T (t, x, x0 ) and the potential is V(t, x), its
lagrangian is
L(t, x, x0 ) = T (t, x, x0 ) V(t, x).
The trajectory is given by a function xo : [t1 , t2 ] R3 which is an extremal point
of the functional
Z
t2
J(x) =
t1
= o.
x
dt x0
=0
i = 1, 2, 3
xi
dt x0i
which should be solved to obtain the 3 components of xo (t) = (xo1 (t), xo2 (t), xo3 (t)).
With the change of variables
L
yi =
i = 1, 2, 3
x0i
we obtain the 6 first order equation system:
yi = x0
i
.
0
L
yi =
xi
Hamilton had the good idea of considering the function
H = (x0 | y) L(t, x, x0 )
10
ALBERTO CASTEJON,
EUSEBIO CORBACHO, BRAN TRIGO, AND RICARDO VIDAL
and it is not difficult to prove that we can express H in terms of the generalized
coordinates (t, x, y), i.e. H = H(t, x, y) .
We can get the derivative of H with respect to t by two different ways:
dH
H 0 H 0 H
dt = x x + y y + t
x0 =
y
.
y0 = H
x
x
If X =
we get a vectorial differential equation of our type X 0 = f(t, X).
y
The most general expression of the kinetic energy is
T = (Ax0 | x0 ) + (b| x0 ) + c
where A M3 (R), b R3 and c R. Then,
L
T
y=
=
= 2Ax0 + b
0
x
x0
and, so,
H = 2(Ax0 | x0 ) + (b| x0 ) T + V = (Ax0 | x0 ) c + V.
Frequently we have b = o and c = 0 and , in such case,
H = T + V = total energy.
A particle of mass 1 under the action of a newtonian field of constant K
N : R3 \ {0}
R3
x
x
7 K
kxk3
1
K
has T = (v|v), V =
and, then, its hamiltonian function is
2
kxk
1
K
H = (v|v)
2
kxk
which is precisely the hamiltonian function of the proposed problem in the beginning
of the section with K = 1. So, we already know that the trajectory of our particle
should be a conic. We can program the numerical solution in the form:
var(t x1 x2 x3 x4 x5 x6)
X=vector([x1,x2,x3,x4,x5,x6])
x=X[0:3]
v=X[3:6]
H=(v*v)/2-1/norma(x)
Hv=[diff(H,x4),diff(H,x5),diff(H,x6)]
Hx=[-diff(H,x1),-diff(H,x2),-diff(H,x3)]
11
f=vector(Hv+Hx)
I=[0,120]
T=I[1]-I[0]
N=60
h=T/N
CI=vector([10,0,3,.1,.2,0])
P=rungekutta(f,X,CI,I,N)
P1=[P[k][0:3] for k in [0..N]]
g=line3d(P1,rgbcolor=(0,1,0),thickness=5)
a=point3d([0,0,0],color=(1,0,0),pointsize=200)
show(g+a)
Unexpectedly the trajectory which seemed to follow an ellipse changes uncontrolledly and we check that the vectors (P [k][0 : 3]) have complex components
for k > 38. This mistake doesnt occur in the solution obtained with the very
similar MATLAB program planet.
clear;
syms t x1 x2 x3 x4 x5 x6
X=[x1 x2 x3 x4 x5 x6];
n=size(X,2);
X=X(1:n);
q=X(1:n/2);p=X(1+n/2:n);
H=p*transpose(p)/2-1/norma(q);
Hq=jacobian(H,q);Hp=jacobian(H,p);
f=[Hp -Hq];
I=[0 118]; to=I(1); T=I(2)-I(1);
CI=[10 0 3 .1 .2 0];
N=60;
h=T/N;
P=to:h:to+T;
Y(:,1)=CI;
rungekutta4;
Y
plot3(0,0,0,*)
hold on
plot3(Y(1,:), Y(2,:), Y(3,:),g)
title(particle trajectory in a period)
h=plot3(Y(1,:), Y(2,:), Y(3,:),ro)
axis(equal)
grid off
nframes=N+1;
for k=1:nframes
x=Y(1,k);
y=Y(2,k);
z=Y(3,k);
set(h,XData,x,YData,y,Zdata,z)
M(k)=getframe;
end
movie(M,3)
12
ALBERTO CASTEJON,
EUSEBIO CORBACHO, BRAN TRIGO, AND RICARDO VIDAL
13
the direction of the positive y-axis, calculate the trajectory of the tank during the
first 10 seconds of the flight.
This problem is a typical example of traction curves:
Assume that a mobile, whose trajectory c : [t0 , t0 + T ] R3 is known, is joined by
means of a spherical articulation to a bar of length ` with a ball of mass m in the
end. If, moreover, on the ball it is acting an external force : [t0 , t0 + T ] R3 and
a resistance proportional to the speed, the trajectory x(t) of the ball will satisfy
x(t) = c(t) + `u(t)
with ku(t)k = 1 ,
its speed
x0 (t) = c0 (t) + `u0 (t) with (u(t)|u0(t)) = 0
and, according to Newtons laws, its celerity will fulfil
(
mx00 (t) = m(c00 (t) + `u00 (t)) = k(t)u(t) (c0 (t) + `u0 (t)) + (t)
with (u(t)|u0 (t)) = 0 and (u(t)|u00(t)) = (u0 (t)|u0 (t))
where k(t) is a scalar function and the viscosity coefficient of the air.
Multiplying by u(t) we will have
k(t) = m(c00 (t)|u(t)) m`(u0 (t)|u0 (t)) + (c0 (t)|u(t)) ((t)|u(t))
and so,
mc00 + (m(c00 |u) m`(u0 |u0 ) + (c0 |u) (|u)) u c0 `u0 +
m`
This second order equation is equivalent to the system
u 0 = v
00
00
0
0
v0 = mc + (m(c |u) m`(v|v) + (c |u) (|u)) u c `v +
m`
u
and putting U =
we obtain the first order vectorial equation U 0 = f(t, U ).
v
u0
For each initial condition U0 =
with ku0 k = 1 and v0 u0 , we will have a
v0
solution U (t) whose three first components u(t) will allow us to write the trajectory
of the ball
x(t) = c(t) + `u(t).
If the function c : [t0 , t0 +T ] R3 is identically zero, we obtain the general equation
of the pendulum:
u 0 = v
.
v0 = (m`(v|v) (|u)) u `v +
m`
If the viscosity coefficient is very big, we can suppose that, independently of any
external force , the trajectory is
u00 =
ku(t)k = 1
Then, it will exist a scalar function h(t) such that c0 (t) + `u0 (t) = h(t)u(t) and
multiplying by u(t) we get the first order equation
u0 (t) =
14
ALBERTO CASTEJON,
EUSEBIO CORBACHO, BRAN TRIGO, AND RICARDO VIDAL
To obtain the numerical solution of the fire helicopter problem we present the
following program:
var(t x1 x2 x3 x4 x5 x6)
C=vector([20*t,0,100])
fe=vector([0,0,-9.8])
CI=vector([0,0,-1,0,1,0])
d=10
b=0.1
M=2000
I=[0,20]
N=40
h=(I[1]-I[0])/N
u=[x1,x2,x3]
U=vector(u)
v=[x4,x5,x6]
V=vector(v)
x=u+v
X=vector(x)
D=diff(C,t)
DD=diff(D,t)
K=DD*U-d*(V*V)+(b/M)*(D*U)-(fe*U)
f=vector(v+list((-DD+K*U-(b/M)*D-(b/M)*d*V+fe)/d))
H=rungekutta(f,X,CI,I,N)
H1=[C(t=k*h)+d*H[k][0:3] for k in [0..N]]
g=line3d(H1,rgbcolor=(0,1,0),thickness=5)
a=parametric_plot3d(C,(t,I[0],I[1]),rgbcolor=(1,0,0),thickness=5)
show(g+a)
Unfortunately the numerical solutions for the vector u dont preserve the norm 1.
This mistake doesnt occur in the solution obtained with the very similar MATLAB
program helicopter:
clear
syms t x1 x2 x3 x4 x5 x6
X=[x1 x2 x3 x4 x5 x6];
n=size(X,2);
d=10;
b=.1;
M=2000;
I=[0 20];
N=60;
to=I(1);T=I(2)-I(1); h=T/N;
U=X(1:n/2);V=X(1+n/2:n);
C=[20*t,0,100];
fe=[0, 0, -9.8];
CI=[0,0,-1];
VI=[-1,1,0];
CI=[CI VI];
D=diff(C,t);
DD=diff(D,t);
15
K=DD*transpose(U)-d*V*transpose(V)+(b/M)*D*transpose(U)-fe*transpose(U);
f=[V (-DD+K*U-(b/M)*D-(b/M)*d*V+fe)/d];
P=to:h:to+T;
Y(:,1)=CI;
rungekutta4;
[q p]=size(P);
U=Y(1:3,:);
Cd=[subs(C(1),t,P).*ones(1,p); subs(C(2),t,P).*ones(1,p); subs(C(3),t,P).*ones(1,p)];
Z=Cd+d*U;
plot3(Cd(1,:),Cd(2,:),Cd(3,:),b)
axis equal
hold on
plot3(Z(1,:),Z(2,:),Z(3,:),r)
axis equal
H=plot3(Cd(1,:),Cd(2,:),Cd(3,:),bo);
G=plot3(Z(1,:),Z(2,:),Z(3,:),ro);
grid off
nframes=N+1;
for k=1:nframes
x=Cd(1,k);x1=Z(1,k);
y=Cd(2,k);y1=Z(2,k);
z=Cd(3,k);z1=Z(3,k);
set(H,XData,x,YData,y,ZData,z)
set(G,XData,x1,YData,y1,ZData,z1)
F(k)=getframe;
end
which needs also the auxiliary program rungekutta4.
4. Acknowledgements
The third author was partyally supported by a collaboration scholarship 2011-2012
of Ministerio de Educaci
on, Cultura y Deporte de Espa
na.
References
[1] Botana, F. ; PPW 2.0: pr
acticas de matem
aticas pola web
[2] Cohen, N. ; Sage in Graph Theory, http://www.steinertriples.fr/ncohen/tut/Graphs/.
[3] Castej
on, A.; Corbacho, E.; Vidal, R. Apuntes de M
etodos Matem
aticos. Curso 2011-2012,
http://www.faitic/M
etodos Matem
aticos/312210421
[4] Crouzeix, M., Mignot, A.L. Analyse num
erique des
equations diff
erentielles. Paris, Masson,
1984.
[5] Stein, W. ; Sage for Power Users, wstein.org/books/sagebook/sagebook.pdf
Departamento de Matematica Aplicada I, Universidad de Vigo, Spain.
E-mail address : [email protected]
Departamento de Matematica Aplicada I, Universidad de Vigo, Spain.
E-mail address : [email protected]
Departamento de Matematica Aplicada I, Universidad de Vigo, Spain.
E-mail address : [email protected]
Departamento de Matematica Aplicada I, Universidad de Vigo, Spain.
E-mail address : [email protected]