- #1
stvoffutt
- 15
- 0
Homework Statement
Solve the following system for [tex]0<t<5[/tex]
[tex]u^\prime = u-e^{-2t} v, u(0) = 1 [/tex]
[tex]v^\prime = u+3v, v(0) = -2 [/tex]
using Forward Euler method and implement the numerical scheme into a MATLAB code.
Homework Equations
Forward Euler : [tex]\vec x^(\prime)_{n+1} = \vec F(t,\vec x) [/tex]
[tex] \frac{\vec x_{n+1} - \vec x_n}{k} = \vec F(t,\vec x) [/tex]
[tex] \vec x_{n+1} = \vec x_n + k\vec F(t,\vec x) [/tex]
The Attempt at a Solution
This seems simple enough but I am having trouble within MATLAB code. First,
[tex] \vec F(t,\vec x) =
\left(\begin{array}{cc}
u-e^{-2t} v\\
u+3v
\end{array}
\right)
[/tex]
So, I implemented this into Matlab and got that both u(t) and v(t) diverge to +inf and -inf respectively. Below is my Matlab code.
Code:
function result = hw_2_2(a,b,k)
T = b-a;
N = T/k;
t = [a:k:T];
y_forward = zeros(2,N+1);
y_forward(:,1) = [1;-2];
% y_backward(:,1) = [1;-2];
for n = 1:N
y_forward(:,n+1) = fun_FE(y_forward(:,n),t(n),k);
% y_backward(:,n+1) = fun_BE(y_backward(:,n),t(n+1),k);
end
u = y_forward(1,:);
v = y_forward(2,:);
figure;
plot(t,u,'-r')
title('forward euler')
figure';
plot(t,v,'-b')
% hold off;
% hold on;
% title('backward euler')
% w= y_backward(1,:)
% z = y_backward(2,:)
% plot(t,w,'-r')
% plot(t,z,'-b')
function solFE = fun_FE(u0,t,k)
A = [1 -exp(-2*t);1 3];
solFE = u0 + k*A*u0;
function solBE = fun_BE(u0,t,k)
solBE = inv(1-k*[1,-exp(-2*t);1,3])*u0;
Eventually I will need to use this code to do the same thing for Backward Euler but I wanted to make sure that this code was OK for Forward Euler method first before I proceeded. The function call that I used is
Code:
hw_2_2(0,5,0.005)