forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler.m
46 lines (34 loc) · 984 Bytes
/
euler.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
clc;clear;close all
%==========================================================================
% Define Function
f =@(x) -3*x;
% Define Initial and Final time
tInit=0;
tLast=5;
% Define number of points
N=1e2;
% Set Initial Conditions
yInit=1;
%==========================================================================
dt=(tLast-tInit)/(N-1); % Calculate dt
t=[tInit:dt:tLast]; % Preallocate time array
y=zeros(1,length(t)); % Preallocate solution array
y(1)=yInit; % Impose Initial Conditions
% Loop over time
for i=1:length(t)-1
t(i+1) = t(i) + dt; % Calculate next time
y(i+1) = y(i) + f( y(i) )*dt; % Update solution
end
% Plot numerical solution
plot(t,y)
% Create analytical solution
g=@(x) exp(-3*x);
z=g(t);
% Plot analytical solution on the same graph
hold on
plot(t,z,'--')
% Set axis, title and legend
xlabel('t');ylabel('y(t)');
title('Analytical VS Numerical Solution')
grid
legend('Numerical','Analytical')