-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.m
More file actions
103 lines (91 loc) · 5.01 KB
/
Utilities.m
File metadata and controls
103 lines (91 loc) · 5.01 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
classdef Utilities
methods(Static)
function p = p_exact(dt, tend)
%% Calculate the analytical solution
% dt: time step.
% tend: last value of the time vector.
% p: vector containing the values of the analytical solution
t = 0:dt:tend; % time vector.
p = 10./(1+9.*exp(-t)); %Analytical Solution
end
function createTable(method, dt, error, error_reduction, error_approx)
%% Plot the Table with the delta ts, errors, factor of error reduction and the approximated error
% method: ODE solver method
% dt: time step
% error: vector of the calculated errors
% error_reduction: vector containing the factor of error reduction
% error_approx: vector of approximated errors
row_header = {'delta t'; 'error'; 'error red.'; 'error app.'};
if method == "eulerMethod"
matrix = [dt; error; error_reduction; error_approx];
figure()
uitable('rowname',row_header,'position',[20 300 420 100],'data',matrix);
txt_title = uicontrol('Style', 'text', 'Position', [20 400 500 20], 'String', 'explicit Euler method (q = 1)');
end
if method == "heunMethod"
matrix = [dt; error; error_reduction; error_approx];
figure()
uitable('rowname',row_header,'position',[20 300 420 100],'data',matrix);
txt_title = uicontrol('Style', 'text', 'Position', [20 400 500 20], 'String', 'method of Heun (q = 2)');
end
if method == "rungeKuttaMethod"
matrix = [dt; error; error_reduction; error_approx];
figure()
uitable('rowname',row_header,'position',[20 300 420 100],'data',matrix);
txt_title = uicontrol('Style', 'text', 'Position', [20 400 500 20], 'String', 'Runge-Kutta method (q = 4)');
end
end
function plotter(method, p0, F, dt, tend)
%% Plot the analytical and numerical solutions for the corresponding method
% method: ODE solver method
% p0: initial conditon
% F: function handle of the ODE
% dt: time step vector
% tend: last value of the time vector
t = 0:dt(end):tend; % time vector
p = Utilities.p_exact(dt(end),tend); % calculate the analytical solution
figure(); %Adding new figure
plot(t,p); %Plotting analytical solution
hold on
%% Solving and plotting for explicit euler's method
if method == "eulerMethod"
for i=1:length(dt)
t_at_specific_dt= 0 : dt(i) :tend; % Time-vector
p_explicit_Euler = explicitEuler(F,p0, dt(i), tend); %Calculating solution via Euler Method
plot(t_at_specific_dt, p_explicit_Euler); %Plotting Euler function
end
legend('Analytical Solution', 'Numerical Solution dt=1', 'Numerical Solution dt=1/2', 'Numerical Solution dt=1/4', 'Numerical Solution dt=1/8', 'Location', 'northwest')
title('Numerical solutions vs Analytical solution via Euler Method')
xlabel('t')
ylabel('p')
grid on
end
%% Solving and plotting for explicit Heun's method
if method == "heunMethod"
for i=1:length(dt)
t_at_specific_dt= 0 : dt(i) :tend; % Time-vector
p_heun = heun(F,p0, dt(i), tend); %Calculating solution via Heun's Method
plot(t_at_specific_dt, p_heun); %Plotting Heuns Method function
end
legend('Analytical Solution', 'Numerical Solution dt=1', 'Numerical Solution dt=1/2', 'Numerical Solution dt=1/4', 'Numerical Solution dt=1/8', 'Location', 'northwest')
title('Numerical solutions vs Analytical solution via Heun Method')
xlabel('t')
ylabel('p')
grid on
end
%% Solving and plotting for explicit Runge Kutta method
if method == "rungeKuttaMethod"
for i=1:length(dt)
t_at_specific_dt= 0 : dt(i) :tend; % Time-vector
p_rungeKutta = rungeKutta(F,p0, dt(i), tend); %Calculating solution via Runge Kutta Method
plot(t_at_specific_dt, p_rungeKutta); %Plotting Runge Kutta function
end
legend('Analytical Solution', 'Numerical Solution dt=1', 'Numerical Solution dt=1/2', 'Numerical Solution dt=1/4', 'Numerical Solution dt=1/8', 'Location', 'northwest')
title('Numerical solutions vs Analytical solution via Runge Kutta Method')
xlabel('t')
ylabel('p')
grid on
end
end
end
end