-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorCalculator.m
More file actions
35 lines (34 loc) · 1.32 KB
/
errorCalculator.m
File metadata and controls
35 lines (34 loc) · 1.32 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
function error = errorCalculator(method, dt, tend, F, p0)
%% Calculate the error
% method: ODE solver method
% dt: time step
% tend: last value of the time vector
% F: function handle of the ODE
% p0: initial conditon
% error: vector of all exact errors, corresponding to the analytical
% solution
%% Error for euler method
if method == "eulerMethod"
error = zeros(1, length(dt)); %Initializing error vector for euler error vs exact solution
for i=1:length(dt)
%calculating the error for each dt
error(i) = sqrt(dt(i)/tend*sum((explicitEuler(F,p0, dt(i),tend)-Utilities.p_exact(dt(i),tend)).^2));
end
end
%% Error for heun's method
if method == "heunMethod"
error = zeros(1, length(dt)); %Initializing error vector for heun error vs exact solution
for i=1:length(dt)
%calculating the error for each dt
error(i) = sqrt(dt(i)/tend*sum((heun(F,p0, dt(i),tend)-Utilities.p_exact(dt(i),tend)).^2));
end
end
%% Error for Runge Kutta method
if method == "rungeKuttaMethod"
error = zeros(1, length(dt)); %Initializing error vector for runge Kutta error vs exact solution
for i=1:length(dt)
%calculating the error for each dt
error(i) = sqrt(dt(i)/tend*sum((rungeKutta(F,p0, dt(i),tend)-Utilities.p_exact(dt(i),tend)).^2));
end
end
end