You are on page 1of 9

List No.

04
Problems proposed in the course of differential equations
Environmental Engineering

1. M-file. Develop an M-file to solve a ODE with Euler method. Use Euler’s method ot obtain a
four decimal approximation of the indicated value. Carry out the recursion of

yi+1 = yi + h(xi , yi ), xi = x0 + ih, i = 0, 1, 2, ...

by hand, first using h = 0.1 and then using h = 0.05



a) y = 2x − 3y, y(1) = 5, y(1.2)

b) y = x + y 2 , y(0) = 0, y(0.2)

2. M-file. Develop an M-file to solve a ODE with Euler method. Use Euler’s method to obtain
a four decimal approximation of the indicated value. Firs use h = 0.1 and then use h = 0.05.
Find an explicit solution for each initial-value problem and then construct tables of data.

a) y = y, y(0) = 1, y(1)

b) y = 2xy, y(1) = 1, y(1.5)

c) y = e−y , y(0) = 0, y(0.5)

d ) y = x2 + y 2 , y(0) = 1, y(0.5)

e) y = xy 2 − xy , y(1) = 1, y(1.5)

3. M-file. Develop an M-file to solve a ODE with Modified Euler method.Use the Modified Euler
method to approximate the solutions of the following initial value problem, and compare the
results to the actual values.
′ 1 −2t
a) y = te3t −2y, 0 ≤ t ≤ 1, y(0) = 0, with h = 0.5; actual solution y(t) = 51 te3t − 25
1 3t
e + 25 e

b) y = cos 2t + sin 3t, 0 ≤ t ≤ 1, y(0) = 1, with h = 0.25; actual solution y(t) = 1
2
sin 2t −
1
3
cos 3t + 34 .

4. M-file . Develop an M-file to solve a ODE with RK4 method. Use RK4 method to obtain a
four decimal approximation of the indicated value. First use h = 0.1 and then use h = 0.05. Is
possible, use a different color for each curve.

a) y = 2x − 3y + 1, y(1) = 5, y(1.5)

b) y = 1 + y 2 , y(0) = 0, y(0.5)

c) y = x2 + y 2 , y(0) = 1, y(0.5)
′ √
d ) y = xy + y, y(0) = 1, y(0.5)

Luis Lara Romero. ⃝Copyright


c 1
All rights reserved
5. Damped spring-mass system. The motion of a damped spring-mass system is described by
the following ordinary differential equation
′′ ′
mx + cx + kx = 0

where x displacement from equilibrium position (m), t time (s), m = 20 kg mass, and c the
damping coefficient (N.s/m). The damping coefficient c takes on three values of 5 (underdam-
ped), 40 (critically damped), and 200 (over damped). The spring constant k = 20 N/m. The
initial velocity is zero, and the initial displacement x = 1 m. Solve this equation using a nume-
rical method over the time period 0 ≤ t ≤ 15 s. Plot the displacement versus time for each of
the three values of the damping coefficient on the same plot.

6. Temperature. In the investigation of a homicide or accidental death, it is often important to


estimate the time of death. From the experimental observations, it is known that the surface
temperature of an object changes at a rate proportional to the difference between the tempe-
rature of the object and that of the surrounding environment or ambient temperature. This is
known as Newton’s law of cooling. Thus, if T (t) is the temperature of the object at time t, and
Ta is the constant ambient temperature
dT
= −K(T − Ta )
dt
where K > 0 is a constant of proportionality. Suppose that at time t = 0 a corpse is discovered
and its temperature is measured to be T0 . We assume that at time of death, the body tempe-
rature Td was at the normal value of 37o C. Suppose that the temperature of the corpse when
it was discovered was 29,5o C, and that two hours latex, it is 23,5o C. The ambient temperature
is 20o C. (a) determine K and the time of death. (b) Solve the ODE numerically and plot the
results.

7. Fall of a body. If air resistance is proportional to the square of the instantaneous velocity,then
the velocity v of a mass m dropped from a given height is determined from
dv
m = mg − kv 2
dt
Let v(0) = 0, k = 0.125, m = 5 slugs, and g = 32 f t/s2 (a) Use a numerical solver to graph
the solution of initial value problem on the interval of time [0, 6]. (b) Use the fourth order RK4
and fifth order RK5 Runge-Kutta method to find an approximate solution by velocity v(5).
(c) Use separation of variables to solve the initial value problem and find value v(5).

Luis Lara Romero. ⃝Copyright


c 2
All rights reserved
8. Mathematical model. A mathematical model for the area A cm2 that a colony of bacteria
occupies is given by
dA
= A(2.128 − 0.0432A)
dt
Suppose that the initial area is 0.24 cm2 . (a) Use a numerical solver to graph the solution of
the initial value problem. Approximate the value A(4) (t days). (b) Use separation of variables
to solve the initial value problem and compute A(4).

The absolute error is defined

|actual value − approximation|

The relative error and percentage relative error are,


absolute error
actual value
and
absolute error
× 100
actual value
Program: euler1.m

% Programa mètodo de Euler explicito


% Diferencial equation
% y’ = x - y
% suject to
% y(0) = 1, x in [0,1]
% solucion : y = x - 1 + 2e^(-x)
clc
t_steps = 10; % numero de subdivisiciones
t_b = 0; % valor inicial tiempo
t_f = 1; % valor final de tiempo
h = (t_f - t_b)/t_steps; % paso del tiempo
x = t_b: h :t_f;
y = zeros(1,t_steps +1); % vector inicial
y(1) = 1; % condiciòn inicial
% metodo de euler explicito
for n=1:t_steps
y(n+1) = y(n) + h*(x(n) - y(n)) ;
end
% ploteando (t,y)
plot(x,y,’--*’, ’LineWidth’, 2)
grid on
xlabel(’tiempo(t)’)
ylabel(’y(t)’)
title(’ solucion numerica ’)
axis([t_b t_f min(y)-0.1 max(y)+0.1])
coord_sol = [x’ y’] % print solucion
% solucion exacta
hold on
ye = x - 1+ 2*exp(-x);

Luis Lara Romero. ⃝Copyright


c 3
All rights reserved
plot(x,ye,’r’,’LineWidth’, 2)
legend(’Euler’, ’Exacta’)

solucion numerica
1.1
Euler
1.05 Exacta

0.95

Program:
0.9 euler2.m
0.85
y(t)

% Programa mètodo de Euler explicito


% Diferencial
0.8 equation
% y’ = t - y
0.75
% suject to
% 0.7y(0) = 1, t in [0,1]
% solucion
0.65
: y = t - 1 + 2e^(-t)
clc,clear all
steps =0.6 16; % numero de subdivisiciones
ta = 0; % valor inicial tiempo
tb = 1; 0 0.2 0.4
tiempo(t)
%0.6valor final
0.8 1
de tiempo
h = (tb - ta)/steps; % paso del tiempo
y = zeros(1,steps + 1); % vector de zeros
y(1) = 1; % condiciòn inicial y0 = 1
t(1) = 1; % condicion inicial t0 = 1
t = ta:h:tb; % intervalo de tiempo
f=@(t,y) t-y;
% metodo de euler explicito
for n=1:steps
y(n+1) = y(n) + h*feval(f, t(n), y(n));
end
% ploteando ...

Luis Lara Romero. ⃝Copyright


c 4
All rights reserved
plot(t,y,’--*’, ’LineWidth’, 2)
grid on
coord_xy = [t’ y’]
% ploteando solucion exacta
hold on
ye = t - 1 +2*exp(-t);
plot(t,ye,’r’, ’LineWidth’, 2)
legend(’euler’, ’exacta’)
xlabel(’ tiempo(t)’)
ylabel(’y(t)’)
title(’ Solucion Numerica ’)

Solucion Numerica
1
euler
exacta
0.95

0.9

Program: euler mejorado.m


0.85
% Programa mètodo de Euler Mejorado
y(t)

% Diferencial equation
0.8
% y’ = t - y
% suject to
% 0.75y(0) = 1, t in [0,1]
% solucion : y = t - 1 + 2e^(-t)
clc,clear
0.7 all
steps = 16; % numero de subdivisiciones
ta = 0;
0.65
% valor inicial tiempo
tb = 1; 0 0.2 0.4 %0.6valor final
0.8 1
de tiempo
tiempo(t)
h = (tb - ta)/steps; % paso del tiempo
y = zeros(1,steps + 1); % vector de zeros
y(1) = 1; % condiciòn inicial y0 = 1

Luis Lara Romero. ⃝Copyright


c 5
All rights reserved
t(1) = 0; % condicion inicial t0 = 1
t = ta:h:tb; % intervalo de tiempo
f=@(t,y) t-y;
% metodo de euler mejorado

for n=1:steps
k1 = feval(f, t(n),y(n));
k2 = feval(f,t(n+1), y(n)+ h*k1);
y(n+1) = y(n) + 0.5*h*(k1 +k2);
end
% ploteando ...
plot(t,y,’--*’, ’LineWidth’, 2)
grid on
coord_xy = [t’ y’]
% ploteando solucion exacta
hold on
ye = t - 1 +2*exp(-t);
plot(t,ye,’r’, ’LineWidth’, 2)
legend(’euler’, ’exacta’)
xlabel(’ tiempo(t)’)
ylabel(’y(t)’)
title(’ Solucion Numerica ’)

Solucion Numerica
1
euler
exacta
0.95

0.9

Program: rk4 1.m


0.85
% Programa
y(t)

% Diferential equation : y’ = 2xy


0.8

Luis Lara Romero. ⃝Copyright


c 6
All rights reserved
0.75

0.7
% suject to : y(1) = 1; y(1.5) = ....
clc
disp(’ Solución numérica RK4 ’)
tb = 1; % tiempo inicial
tf = 1.5; % tiempo final
num = 11;
tspan =linspace(tb,tf,num);

y0 = 1; % condicion inicial
f=@(x,y) 2*x*y;
[x,y] = ode45(f,tspan,y0);

coord_xy = [x,y]
csvwrite(’xy.m’,[x y]); % save coordenadas (x,y)
plot(x,y,’r*’, ’LineWidth’, 2)
axis([tb tf, min(y)-0.1 max(y)+0.1])
grid on
xlabel(’ espacio x’)
ylabel(’ desplazamiento y ’)
title(’ Diferential Equation ’)
% ploteando solución exacta
hold on
syms x y
y = dsolve(’Dy = 2*x*y’, ’y(1)=1’, ’x’);
g = ezplot(y,[tb tf]);
set(g, ’color’, ’b’, ’LineWidth’, 2)
% simplificamos solucion
y(x) = simplify(y)
% evaluando en punto
eval_y = double(subs(y,{x}, {tf}))
% legenda
legend(’ RK4’, ’Exacta’)

Luis Lara Romero. ⃝Copyright


c 7
All rights reserved
exp(x2) exp(−1)

RK4
3.5
Exacta

3
desplazamiento y

Program: rk4 2.m


2.5

% Programa Runge-Kutta 4
% Diferencial equation
2
% y’ = y - t^2 + 1
% suject to
% 1.5y(0) = 0.5, t in [0,5]
% solucion : y = t^2 +2t + 1 -0.5e^t
clc,clear all
steps = 1 10; % numero de subdivisiciones
ta = 0; % valor inicial tiempo
1 1.1 1.2
tb = 5; x
%1.3valor final
1.4 1.5
de tiempo
h = (tb - ta)/steps; % paso del tiempo
y = zeros(1,steps + 1); % vector de zeros
y(1) = 0.5; % condiciòn inicial y0 = 0.5
t(1) = 0; % condicion inicial t0 = 0
t = ta:h:tb; % intervalo de tiempo
f=@(t,y) y - t^2 +1;
% RK4

for n=1:steps
k1 = feval(f, t(n),y(n));
k2 = feval(f, t(n) +0.5*h, y(n) + 0.5*h*k1);
k3 = feval(f, t(n)+0.5*h, y(n) + 0.5*h*k2)
k4 = feval(f, t(n+1), y(n) + h*k3)

y(n+1) = y(n) + (1/6)*h*(k1 +2*k2+2*k3+k4);

Luis Lara Romero. ⃝Copyright


c 8
All rights reserved
end
% ploteando ...
plot(t,y,’--*’, ’LineWidth’, 2)
grid on
coord_xy = [t’ y’]
% ploteando solucion exacta
hold on
ye = t.^2 + 2*t+1-0.5*exp(t);
plot(t,ye,’r’, ’LineWidth’, 2)
legend(’RK4’, ’exacta’)
xlabel(’ tiempo(t)’)
ylabel(’y(t)’)
title(’ Solucion Numerica ’)

Solucion Numerica
10
RK4
5 exacta

−5

−10
y(t)

−15

−20

−25

−30

−35

−40
0 1 2 3 4 5
tiempo(t)

Luis Lara Romero. ⃝Copyright


c 9
All rights reserved

You might also like