Diff Equations

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

VECTORIAL DIFFERENTIAL EQUATIONS WITH SAGE AND

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

(b) kf(t, x)k `(t)(1 + kxk) (t, x) [t0 , t0 + T ] Rn


exists a x : [t0 , t0 + T ] Rn such that
(
x0 (t) = f(t, x(t)) t [t0 , t0 + T ]
x(t0 ) = p0
(3) Si f is continuous and exists a function ` L1 ([t0 , t0 + T ]) fulfilling
(f(t, x) f(t, y)|x y) `(t)(kx yk22 ) (t, x, y) [t0 , t0 + T ] Rn Rn
exists one and only one x : [t0 , t0 + T ] Rn such that
(
x0 (t) = f(t, x(t)) t [t0 , t0 + T ]
x(t0 ) = p0
(4) If f is continuous and exists a real number such that
kf(t, x) f(t, y)k (kx yk) (t, x, y) [t0 , t0 + T ] Rn Rn
exists one and only one x : [t0 , t0 + T ] Rn such that
(
x0 (t) = f(t, x(t)) t [t0 , t0 + T ]
x(t0 ) = p0
Before a technical problem we need two important steps to do: The first one is to
find the corresponding function f : [t0 , t0 + T ] Rn Rn which represents the
problem. The second step, more systematic, is to find the analytical solution for
a given initial condition if possible, or to obtain a numerical approximation to the
solution.
2. Linear equations with constant coefficients
One of the few cases when we can find an analytical solution for the problem is the
case of f(t, x) = Ax + u(t) being A Mn (R) and u : [t0 , t0 + T ] Rn a
continuous function.
The particular solution of initial condition p0 is given by the formula
Z t
(?)
x = eA(tt0) p0 +
eA(ts) u(s) ds .
t0

This kind of equations appear in the following examples:


2.1. RLC circuits.
A bipolar alternator of angular frequency generates an instantaneous voltage
V sin t. If it is the source of a series circuit with resistance R ohms, capacitance
C faradays and inductance L henries, the charge q(t) and the intensity i(t) verify
the linear system
(
dq(t)
dt = i(t)
di(t)
1
R
V
dt = CL q(t) L i(t) + L sin t
that we can write
 0 
0
q
=
1
i
LC

  

0
q
+ V
R
i
sin t
L
L
1

VECTORIAL DIFFERENTIAL EQUATIONS WITH SAGE AND MATLAB

In this way it appears a two dimensional equation x0 = Ax + u(t) where


 




q
0
1
0
x=
, A=
, u(t) = V
1
i
LC
R
L
L sin t
The function circuits gives us the solution:
def circuits(R,L,C,V,w,T,Q,I):
M=matrix([[0,1],[-1/(L*C), -R/L]])
S=exp(M*t)
Ss=exp(M*(t-s))
u=vector([0,(V/L)*sin(w*s)])
CI=vector([Q,I])
Xt=S*CI
B=Ss*u
assume(t>0)
Xe=integral(B,s,0,t)
X=Xt+Xe
return X
2.2. Missiles.
Shooting a missile, it is possible to deduce its plane trajectory x : [0, T ] R2 from
the Newtons law:
dx
d(mv)
being v =
.
F =
dt
dt
If we consider the mass of the missile a constant m, and we suppose the acting
forces to be the gravity g and the resistance of the air v being its viscosity, it
is clear that

F = mg x0 = mx00 and x00 = g x0 .


m
Thus we have the equivalent first order system

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

with the same initial condition


second problem for

0 0
0 0
f2 (t, X) =
0 0
0 0

X0 and, being X(t) its solution and X(T0 ) = X1 , a


1
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

We discard the non conservative option N . The trajectory of a particle of mass m


under the action of the field D : R3 R3 verify the equation
mx00 = Dx.
For a solution x to be an oscillation it should be a bounded curve, and so the
eigenvalues k1 , k2, k3 can not be positive.
Moreover, if the particle is in a medium with viscosity coefficient b and it acts on it,
during an interval of time [t0 , t0 +T ], an arbitrary external force u : [to , to +T ] R3 ,
the trajectory of the particle is given by the equation
mx00 = Dx bx0 + u(t).
In particular, taking u(t) = bx0 (t) being x(t) the solution of mx00 = Dx, we have
an external force which annulates the viscosity of the medium.

VECTORIAL DIFFERENTIAL EQUATIONS WITH SAGE AND MATLAB

Taking x0 = v we get the equivalent first order system:


 0 
    
I
x
O
x
0
m
=

+ 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

ready to be treated by the (?) formula. The function oscillator:


var(yes no)
def oscillator(m,b,T,K,V,F,CI):
N=matrix(3)
U=identity_matrix(3)
K=diagonal_matrix([K[0],K[1],K[2]])
A=block_matrix(QQ,[[N,U],[-K/m, -b*U/m]])
CI=vector(CI)
S=exp(A*t)
Xh=S*CI
Ss=S(t=t-s)
if V==yes:
A0=block_matrix(QQ,[[N,U],[-K/m, N]])
S0=exp(A0*t)
X=S0*CI
u0=b*vector([X[3],X[4],X[5]])
else:
u=t^0*vector([0,0,0,F[0],F[1],F[2]])/m
u=u(t=s)
B=Ss*u
XP=integral(B,s,0,t)
X=Xh+XP
u0=F
return [X,u0]
gives us a complex solution. Of course the exponential exp(A t) of the real matrix
A should be real but the imaginary part is negligible.
3. Numerical Methods
For any other kind of differential equations we will not try analytical solutions. We
prefer to implement one step iterative methods in SAGE to obtain numerical and
graphical approximations to the solutions. Given a Cauchy problem
(
x0 (t) = f(t, x(t)) t [t0 , t0 + T ]
x(t0 ) = p0
we make an equidistant partition in the interval [t0 , t0 + T ]
t0 < t1 < < tk < tk+1 < < tN = t0 + T

ALBERTO CASTEJON,
EUSEBIO CORBACHO, BRAN TRIGO, AND RICARDO VIDAL

with step h =

T
N.

The Barrows rule


Z tk+1
x(tk+1 ) x(tk ) =
f(t, x(t))dt

k = 0, , N

tk

suggests us to look for an N + 1-tuple (xk ) which approximates the N + 1-tuple


(x(tk )), following a recursive process
(
x0 = p0
xk+1 = xk + h(tk , xk ; h)
where h(tk , xk ; h) is an approximation of the former integral. Such numerical
methods are called one step methods because to obtain xk+1 only use the information in xk .
We will say that the method is an orden p method if there exists a constant K 0
such that
N1
X
kx(tk+1 ) x(tk ) h(tk , x(tk ); h)k Khp .
k=0

If we use the formula of rectangles for approximating the integral


Z tk+1
f(t, x(t))dt hf(tk , xk )
tk

i.e. if we make (tk , xk ; h) = f(tk , xk ), we obtain the Eulers method which is an


order 1 method when f C (1 .
If we make
Fk1 + 2Fk2 + 2Fk3 + Fk4
(tk , xk ; h) =
6
with

Fk1 = f(tk , xk )

h
h

Fk2 = f(tk + , xk + Fk1)


2
2

h
h

Fk3 = f(tk + , xk + Fk2)

2
2

Fk4 = f(tk+1 , xk + hFk3 )

we obtain the Runge-Kutta method which is an order 4 method when f C (1 .

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

VECTORIAL DIFFERENTIAL EQUATIONS WITH SAGE AND MATLAB

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

3.2. Ecosystem evolution.


In an ideal valley there coexists in each instant x(t) lambs and y(t) wolves and the
possibility of feeding for the lambs is unlimited. According to Lotka and Volterra
this ecosystem is governed by the differential equation
(
x0 = 0.25x 0.01 x y
y0 = 1 y + 0.01 x y
If in the initial moment in the valley there are 80 lambs and 20 wolves, which will
be the evolution in the next 20 years?
A differential equation x0 = f(x) where the function f doesnt depend explicitely of
t is called autonomous. If x0 annulates the function f, it is clear that the constant
curve x(t) = x0 is a solution and the point x0 is called stationary. If, instead the
given equation we consider its linear approximation
x0 f(x0 ) + Df(x0 )(x x0 )
we arrive to the equation x0 = Ax + c solvable with the (?) formula, being
A = Df(x0 ) and c = Df(x0 )(x0 ). The analytical solution of this approximated
problem can give us information about the original one.
The numerical solution has been programmed with Euler and Runge-Kutta methods to compare them:
var(t x1 x2)
X=vector([x1,x2])
f=vector([.25*x1-.01*x1*x2,-x2+.01*x1*x2])
CI=vector([90,25])
S=euler(f,X,CI,[0,13],200)
S1=rungekutta(f,X,CI,[0,13],200)
g=line2d(S,rgbcolor=(1,0,0),thickness=.5)
g1=line2d(S1,rgbcolor=(0,1,0),thickness=1)
show(g+g1)
3.3. Chase curves.
A spy plane flies around our position at 3000 m. height describing a circle of 2000
m. radius with speed 200 m/s. When it is in the point (0,2000,3000) we shoot a
disuasive intelligent missile which will pursued it with speed 180 m/s.
Which is the trajectory of the missile in the first 100 seconds?
This problem is a typical example of chase curves:
Let be a mobile with known trajectory c : [t0 , t0 + T ] R3 pursued by another
one. The trajectory x : [t0 , t0 + T ] R3 of the hunter will satisfy
x0 (t) =
If we know the relation

r(t) =

x0 (t) = f(t, x(t))

c(t) x(t)
kx0 (t)k.
kc(t) x(t)k

kx0 (t)k
kc0(t)k

we can conclude that

with f(t, x(t)) = r(t)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:

VECTORIAL DIFFERENTIAL EQUATIONS WITH SAGE AND MATLAB

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

L(t, x(t), x0 (t))dt

J(x) =

t1

and, so, it should verify the Euler-Lagrange equation:




L
d L

= o.
x
dt x0

This vectorial equation is equivalent to a system of 3 second order equations




L
d L

=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

dH = (x00 | y) + (x0 | y0 ) L x0 L x00 L = L x0 + (x0 | y0 ) L


dt
x
x0
t
x
t
and we deduce
H
L
H
H
L
=
,
= x0 ,
=
x
x
y
t
t
Then we can write the last system in the hamiltonian style

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)]

VECTORIAL DIFFERENTIAL EQUATIONS WITH SAGE AND MATLAB

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

For running planet we use the auxiliary programs norma:


function J=norma(X)
J=sqrt(X*transpose(X));
and rungekutta4:
if diff(f,t)==zeros(1,n)
for j=2:N+1
for i=1:n
K(1,i)=subs(f(i),X, transpose(Y(:,j-1)));
end
for i=1:n
K(2,i)=subs(f(i),X, transpose(Y(:,j-1))+(h/2)*K(1,:));
end
for i=1:n
K(3,i)=subs(f(i),X, transpose(Y(:,j-1))+(h/2)*K(2,:));
end
for i=1:n
K(4,i)=subs(f(i),X, transpose(Y(:,j-1))+h*K(3,:));
end
for i=1:n
Y(i,j)=double(Y(i,j-1)+h*[1/6 2/6 2/6 1/6]*K(:,i));
end
end
else
for j=2:N+1
for i=1:n
K(1,i)=subs(f(i),[t X],[P(j-1) transpose(Y(:,j-1))]);
end
for i=1:n
K(2,i)=subs(f(i),[t X],[P(j-1)+h/2 transpose(Y(:,j-1))+(h/2)*K(1,:)]);
end
for i=1:n
K(3,i)=subs(f(i),[t X],[P(j-1)+h/2 transpose(Y(:,j-1))+(h/2)*K(2,:)]);
end
for i=1:n
K(4,i)=subs(f(i),[t X],[P(j) transpose(Y(:,j-1))+h*K(3,:)]);
end
for i=1:n
Y(i,j)=double(Y(i,j-1)+h*[1/6 2/6 2/6 1/6]*K(:,i));
end
end
end
3.5. Traction curves.
A fire helicopter has a bar of 10 m. linked to a spherical articulation. In the end of
the bar it hangs a water tank of 2000 `. The helicopter fills the tank in the origin,
goes up vertically up to 100 m. and then it begins flying with constant height
following the positive x-axis with speed 20 m/s. If the viscosity of the air is .1 kg/s
and in the initial instant a gust of wind gives the tank of water a speed of 1 m/s in

VECTORIAL DIFFERENTIAL EQUATIONS WITH SAGE AND MATLAB

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 =

x(t) = c(t) + `u(t) with

ku(t)k = 1

and x0 (t) k u(t).

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) =

(c0 (t)|u(t))u(t) c0 (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);

VECTORIAL DIFFERENTIAL EQUATIONS WITH SAGE AND MATLAB

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]

You might also like