aboutsummaryrefslogtreecommitdiffstats
path: root/Part 1/error_script_heun.m
blob: 8c0f49fdd5465cf15a8cd4a098f7e419125286c3 (plain)
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
function error_script_heun (tf)

ts = 0;     % set initial value of x_0
is = 0;
h = 0.0001;  % set step-size
R = 0.5;
L = 0.0015;


A = 6;
T = 0.00015;

vin = @(t) A * cos(2*pi*t/T);
func = @(t, iout) (vin(t) - iout*R) / L;      % define func
[t, iout ] = heun(func, ts, is, h);

%numerical solution
vout = vin(t) - iout * R;

%obtaining the exact solution with favorite method
i_{Exact} = (V_{initial_in}/L)*((2*pi)/T * sin((2*pi)/T*t) + (R/L)*cos((2*pi)/T*t) - (R/L)*exp(-(R/L)*t))/((2*pi)/T^2 + (R/L)^2);
exact = vin(t) - R*i_{Exact};
%error as a function of t
error= exact - vout; 

figure(1);
plot(t,error); %for heun
loglog(error);

end