Professional Documents
Culture Documents
List No. 04 Problems Proposed in The Course of Differential Equations Environmental Engineering M-File
List No. 04 Problems Proposed in The Course of Differential Equations Environmental Engineering M-File
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
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)
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.
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).
solucion numerica
1.1
Euler
1.05 Exacta
0.95
Program:
0.9 euler2.m
0.85
y(t)
Solucion Numerica
1
euler
exacta
0.95
0.9
% 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
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
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’)
RK4
3.5
Exacta
3
desplazamiento y
% 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)
Solucion Numerica
10
RK4
5 exacta
−5
−10
y(t)
−15
−20
−25
−30
−35
−40
0 1 2 3 4 5
tiempo(t)