diff options
author | MK2020 <mh4115@ic.ac.uk> | 2017-03-14 17:30:34 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-14 17:30:34 +0000 |
commit | 487047b7896d1d0814267cb44b4e66a1b1c5aff9 (patch) | |
tree | a21aa5de0e18216dab20de4c415dd76cb0834aff | |
parent | 50a968d642ae703e5205ab283c1c230e5f0e2c59 (diff) | |
download | NumericalAnalysis-487047b7896d1d0814267cb44b4e66a1b1c5aff9.tar.gz NumericalAnalysis-487047b7896d1d0814267cb44b4e66a1b1c5aff9.zip |
Add files via upload
-rw-r--r-- | Part 1/error_script.m | 132 | ||||
-rw-r--r-- | Part 1/error_script_heun.m | 22 | ||||
-rw-r--r-- | Part 1/exact_solution.m | 9 | ||||
-rw-r--r-- | Part 1/heun_temp.m | 13 | ||||
-rw-r--r-- | Part 1/midpoint.m | 6 | ||||
-rw-r--r-- | Part 1/ralston.m | 18 | ||||
-rw-r--r-- | Part 1/zihan_error.m | 37 | ||||
-rw-r--r-- | Part 1/zihan_error2.m | 40 |
8 files changed, 206 insertions, 71 deletions
diff --git a/Part 1/error_script.m b/Part 1/error_script.m index 97cc501..8e24418 100644 --- a/Part 1/error_script.m +++ b/Part 1/error_script.m @@ -1,43 +1,8 @@ %-----------------------------------------------------------------
-% 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
+h = 0.0001; % set step-size
tf = 0.03; % stop here
R = 0.5;
L = 0.0015;
@@ -47,35 +12,90 @@ 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);
+[t, iout ] = midpoint(func, ts, tf, is, h);
-%numerical solution
+%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);
+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 = abs(exact - vout);
-
-figure(3);
-plot(t,error); %for ralston
+error = exact - vout;
+figure(1);
+plot(t,error); %for midpoint
-max_error = max(error);
+figure(2);
-
-figure(5);
-for k = 1:5
-
- [t, iout] = ralston(func, ts, tf, is, h);
- exact = vin(t) - R*i_Exact(t);
+for ind=3:16 % choose these carefully
+ h1=2^(-ind); % set stepsize
+
+
+ [t, iout] = midpoint(func, ts, tf, is, h1);
+ exact = vin(t) - R*i_Exact(t) ;
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;
+ error = max(abs(exact - vout));
+
+ plot(log(h1),log(error),'b*'); % log/log plot stepsize vs error
+ xlabel({'Time', '(seconds)'});
+ ylabel({'Error'});
+ title('MIDPOINT');
+ hold on; % for next value to be plotted
end
+
+
+%
+% %--------------------------------------------------------------------
+%
+% 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
+%
+%
+%
+% figure(4);
+%
+% for ind=3:16 % choose these carefully
+% h1=2^(-ind); % set stepsize
+%
+%
+% [t, iout] = ralston(func, ts, tf, is, h1);
+% exact = vin(t) - R*i_Exact(t) ;
+% vout = vin(t) - R*iout;
+% error = max(abs(exact - vout));
+%
+% plot(log(h1),log(error)); % log/log plot stepsize vs error
+% xlabel({'Time', '(seconds)'});
+% ylabel({'Error'});
+% title('V_{out} versus time (original function)');
+% hold on; % for next value to be plotted
+% end
+
+
diff --git a/Part 1/error_script_heun.m b/Part 1/error_script_heun.m index 8c0f49f..8e2dff3 100644 --- a/Part 1/error_script_heun.m +++ b/Part 1/error_script_heun.m @@ -18,14 +18,28 @@ func = @(t, iout) (vin(t) - iout*R) / L; % define func 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};
+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= exact - vout;
+error = abs(exact - vout);
figure(1);
plot(t,error); %for heun
-loglog(error);
+%loglog(error);
+
+figure(5);
+for ind=3:16 % choose these carefully
+ h1=2^(-ind); % set stepsize
+
+
+ [t, iout] = heun(func, ts, is, h1);
+ exact = vin(t) - R*i_Exact(t) ;
+ vout = vin(t) - R*iout;
+ error = max(abs(exact - vout));
+
+ plot(log(h1),log(error),'b*'); % log/log plot stepsize vs error
+ hold on; % for next value to be plotted
+end
end
diff --git a/Part 1/exact_solution.m b/Part 1/exact_solution.m new file mode 100644 index 0000000..4cd8902 --- /dev/null +++ b/Part 1/exact_solution.m @@ -0,0 +1,9 @@ +function [ t, i_Exact ] = exact_solution(R,L,T,tfinal, h)
+
+N = round((tfinal) / h);
+t = zeros(1,N);
+i_Exact = zeros(1,N);
+for i = 1 : N - 1
+ t(i+1) = t(i) + h;
+ i_Exact(i+1) = (6/L)*((2*pi)/T * sin((2*pi)/T*t(i)) + (R/L)*cos((2*pi)/T*t(i)) - (R/L)*exp(-(R/L)*t(i)))/((2*pi)/T^2 + (R/L)^2);
+end
diff --git a/Part 1/heun_temp.m b/Part 1/heun_temp.m new file mode 100644 index 0000000..9a49d4a --- /dev/null +++ b/Part 1/heun_temp.m @@ -0,0 +1,13 @@ +function [y, t] = heun_temp(f,a,b,ya,n)
+h = (b - a) / n;
+halfh = h / 2;
+y(1,:) = ya;
+t(1) = a;
+for i = 1 : n
+ t(i+1) = t(i) + h;
+ g = f(t(i),y(i,:));
+ z = y(i,:) + h * g;
+ y(i+1,:) = y(i,:) + halfh * ( g + f(t(i+1),z) );
+end;
+
+%func,t0,tf,i0,step_size
\ No newline at end of file diff --git a/Part 1/midpoint.m b/Part 1/midpoint.m index 7c3ed5c..9c8c180 100644 --- a/Part 1/midpoint.m +++ b/Part 1/midpoint.m @@ -1,8 +1,10 @@ function [ x, y ] = midpoint( f, t0, tfinal, y0, h)
N = round((tfinal - t0) / h);
-ya = zeros(1,N); ta = zeros(1,N);
-ya(1) = y0; ta(1) = t0;
+ya = zeros(1,N);
+ta = zeros(1,N);
+ya(1) = y0;
+ta(1) = t0;
for i = 1 : N - 1
ta(i+1) = ta(i) + h;
diff --git a/Part 1/ralston.m b/Part 1/ralston.m index 6041374..ac134c3 100644 --- a/Part 1/ralston.m +++ b/Part 1/ralston.m @@ -1,15 +1,18 @@ -function [t,vout] = ralston(func, t0, tf,i0 , h)
+function [xa,ya] = ralston(func, t0, tf,i0 , h)
-n = ((tf - t0) / h);
+N = round((tf) / h);
+xa = zeros(1,N);
+ya = zeros(1,N);
+xa(1)=t0;
+ya(1)=i0;
+
+%param
a1=1/3;
a2=2/3;
p1=3/4;
q11=3/4;
-xa(1)=t0;
-ya(1)=i0;
-
-for i=1:1:n
+for i=1: N - 1
xtemp=xa(i);
ytemp=ya(i);
@@ -20,6 +23,3 @@ k2=func(xtemp+p1*h,ytemp+q11*k1*h); ya(i+1)=ytemp+(a1*k1+a2*k2)*h;
xa(i+1)=xtemp+h;
end
-
-t = xa;
-vout = ya;
\ No newline at end of file diff --git a/Part 1/zihan_error.m b/Part 1/zihan_error.m new file mode 100644 index 0000000..cbabc50 --- /dev/null +++ b/Part 1/zihan_error.m @@ -0,0 +1,37 @@ +clear;
+%inital
+ts = 0; % set initial value of x_0
+is = 0;
+
+%parameter
+tf = 0.03; % stop here
+R = 0.5;
+L = 0.0015;
+
+
+
+%vin and the function
+A = 6;
+T = 0.00015;
+vin = @(t) A * cos(2*pi*t/T);
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+
+
+for ind=3:16 % choose these carefully
+ h1=2^(-ind); % set stepsize
+
+ [t, iout, exact] = midpoint(func, ts, tf, is, h1);
+
+ figure;
+ plot(exact)
+
+% vout = vin - R*iout;
+%
+% error = max(abs(exact - vout));
+%
+% plot(log(h1),log(error),'b*'); % log/log plot stepsize vs error
+% xlabel({'Time', '(seconds)'});
+% ylabel({'Error'});
+% title('MIDPOINT');
+% hold on; % for next value to be plotted
+end
diff --git a/Part 1/zihan_error2.m b/Part 1/zihan_error2.m new file mode 100644 index 0000000..5f9d643 --- /dev/null +++ b/Part 1/zihan_error2.m @@ -0,0 +1,40 @@ +%-----------------------------------------------------------------
+clear;
+ts = 0; % set initial value of x_0
+is = 0;
+tf = 0.03; % stop here
+R = 0.5;
+L = 0.00015;
+
+A = 6;
+T = 0.00015;
+
+vin = @(t) A * cos(2*pi*t/T);
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+
+%obtaining the exact solution with favorite method
+
+%error as a function of t
+%error = exact - vout;
+
+figure;
+
+for ind=16:50 % choose these carefully
+
+ h1=2^(-ind); % set stepsize
+ [t, i_Exact] = exact_solution(R,L,T,tf,h1);
+ exact = vin(t) - R*i_Exact;
+
+ [t, iout] = ralston(func, ts, tf, is, h1);
+ vout = vin(t) - R*iout;
+
+ error = exact - vout;
+ error = max(abs(error));
+
+ plot(log(h1),log(error)); % log/log plot stepsize vs error
+ xlabel({'Step size', '(h)'});
+ ylabel({'Error'});
+ title('MIDPOINT');
+ hold on; % for next value to be plotted
+
+end
|