aboutsummaryrefslogtreecommitdiffstats
path: root/Part 1/error_script.m
blob: 98ca943515e71eda6720e8bb63fe081827dfa803 (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
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
%-----------------------------------------------------------------
% clear;
% ts = 0;     % set initial value of x_0
% is = 0;
% h = 0.0001;  % set step-size
% tf = 0.03;     % stop here
% 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 ] = midpoint(func, ts, tf, is, h);
% 
% %numerical solution
% vout = vin(t) - iout * R;
% 
% %obtaining the exact solution with favorite method
% i_Exact = (6/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(2);
% plot(t,error); %for midpoint
% figure(3);
% t = logspace(-10 ,1);
% loglog(t,error);
% grid on;


%--------------------------------------------------------------------

clear;
ts = 0;     % set initial value of x_0
is = 0;
h = 0.0000005;  % set step-size
tf = 0.03;     % stop here
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 ] = ralston(func, ts, tf, is, h);

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

%obtaining the exact solution with favorite method
i_Exact = @(t) (6/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(t);

%error as a function of t
error = abs(exact - vout); 

figure(3);
plot(t,error); %for ralston


max_error = max(error);


figure(5);
for k = 1:5
    
    [t, iout] = ralston(func, ts, tf, is, h);
    exact = vin(t) - R*i_Exact;
    vout = vin(t) - R*iout;
    error = abs(exact - vout); 
    max_error = max(error);
    plot(log(t), log(max_error),'b*');
    hold on;
    h = 2*h;
end