aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsupercalvinchan <cc6815@ic.ac.uk>2017-03-13 15:11:47 +0000
committerGitHub <noreply@github.com>2017-03-13 15:11:47 +0000
commit33a4d7de6f7db00012c5e66425ef8d1ffb86d77e (patch)
treee91fbf6f17ccafd9d711a5a7af32f4b48c71bf89
parent55fa9d66cd8993b4e76122d3df4ef42685285770 (diff)
downloadNumericalAnalysis-33a4d7de6f7db00012c5e66425ef8d1ffb86d77e.tar.gz
NumericalAnalysis-33a4d7de6f7db00012c5e66425ef8d1ffb86d77e.zip
Update error_script.m
-rw-r--r--Part 1/error_script.m83
1 files changed, 49 insertions, 34 deletions
diff --git a/Part 1/error_script.m b/Part 1/error_script.m
index 5c2514e..f9f8a5d 100644
--- a/Part 1/error_script.m
+++ b/Part 1/error_script.m
@@ -1,40 +1,43 @@
%-----------------------------------------------------------------
-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
-exact= @(t) 0.07553*cos( 41883.7*(t) ) + 0.94901*sin( 41883.7*(t) ); % works for arrays
-%exact = Vin0 - R*((Vin0/R) * (1 - exp(-(R/L)*ta))); %Vin0 is the initial value of Vin, ta is the value of t
-
+% 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;
-%error as a function of t
-error = @(t) exact(t) -vout;
-
-figure(2);
-plot(t,error); %for midpoint
-loglog(error);
%--------------------------------------------------------------------
clear;
ts = 0; % set initial value of x_0
is = 0;
-h = 0.0001; % set step-size
+h = 0.0000005; % set step-size
tf = 0.03; % stop here
R = 0.5;
L = 0.0015;
@@ -46,21 +49,33 @@ 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
+%numerical solution
vout = vin(t) - iout * R;
%obtaining the exact solution with favorite method
-exact= @(t) 0.07553*cos( 41883.7*(t) ) + 0.94901*sin( 41884.7*(t) ); % works for arrays
+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 = @(t) exact(t) -vout;
+error(t) = abs(exact(t) - vout(t));
figure(3);
plot(t,error); %for ralston
-loglog(error);
-
+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