aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMK2020 <mh4115@ic.ac.uk>2017-03-12 16:35:17 +0000
committerGitHub <noreply@github.com>2017-03-12 16:35:17 +0000
commitdcb5625f789a156f02260b8a3eec16b9cc30c2a8 (patch)
tree49b2637fc92400a78f6a88f3bca0b6b9a02f6890
parentd774e219f020321e89318e7473166b030ff15b80 (diff)
downloadNumericalAnalysis-dcb5625f789a156f02260b8a3eec16b9cc30c2a8.tar.gz
NumericalAnalysis-dcb5625f789a156f02260b8a3eec16b9cc30c2a8.zip
Add files via upload
-rw-r--r--Part 1/error_script.m64
-rw-r--r--Part 1/error_script_heun.m30
-rw-r--r--Part 1/heun_script.m55
-rw-r--r--Part 1/midpoint_script.m104
-rw-r--r--Part 1/ralston_script.m104
5 files changed, 311 insertions, 46 deletions
diff --git a/Part 1/error_script.m b/Part 1/error_script.m
new file mode 100644
index 0000000..43f8dda
--- /dev/null
+++ b/Part 1/error_script.m
@@ -0,0 +1,64 @@
+%-----------------------------------------------------------------
+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
+
+%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
+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
+exact= @(t) 0.07553*cos( 41883.7*(t) ) + 0.94901*sin( 41884.7*(t) ); % works for arrays
+
+%error as a function of t
+error = @(t) exact(t) -vout;
+
+figure(3);
+plot(t,error); %for ralston
+loglog(error);
+
+
+
+
+
+
diff --git a/Part 1/error_script_heun.m b/Part 1/error_script_heun.m
new file mode 100644
index 0000000..bb511bd
--- /dev/null
+++ b/Part 1/error_script_heun.m
@@ -0,0 +1,30 @@
+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
+exact= @(t) 0.07553*cos( 41883.7*(t) ) + 0.94901*sin( 41883.7*(t) ); % works for arrays
+
+%error as a function of t
+error=exact-vout;
+
+figure(1);
+plot(t,error); %for heun
+loglog(error);
+
+end \ No newline at end of file
diff --git a/Part 1/heun_script.m b/Part 1/heun_script.m
index 264942f..fdf9a62 100644
--- a/Part 1/heun_script.m
+++ b/Part 1/heun_script.m
@@ -42,18 +42,9 @@ title('(Heaviside) V_{out} versus time');
%---------------------------------------------------------------------------------------------
-%initailise the circuits
-R = 0.5;
-L = 1.5*10^(-3);
-h = 0.00001; %step size
-
-
-%initailise the container
+%initailise the circuits at the top
-N = round(tf/h); %number of iterations
-t = zeros(1, N);
-Vout = zeros(1, N);
-current = zeros(1,N);
+%initailise the container at the top
%input voltage
tau = 0.000150;
@@ -86,18 +77,9 @@ title('(Exponential #1) V_{out} versus time');
%-----------------------------------------------------------------------------------------------
-%initailise the circuits
-R = 0.5;
-L = 1.5*10^(-3);
-h = 0.00001; %step size
+%initailise the circuits at the top
-
-%initailise the container
-
-N = round(tf/h); %number of iterations
-t = zeros(1, N);
-Vout = zeros(1, N);
-current = zeros(1,N);
+%initailise the container at the top
%input voltage
tau = 0.000150;
@@ -132,13 +114,10 @@ title('#(Exponential #2) V_{out} versus time');
%-------------------------------------------------------------------------------------------
-%initailise the circuits
-R = 0.5;
-L = 1.5*10^(-3);
-h = 0.00001; %step size
+%initailise the circuits at the top
-%initailise the container
+%initailise the container at the top
N = round(tf/h); %number of iterations
t = zeros(1, N);
@@ -174,18 +153,11 @@ title('(Sine wave) V_{out} versus time ');
%-----------------------------------------------------------------------------------------
-%initailise the circuits
-R = 0.5;
-L = 1.5*10^(-3);
-h = 0.00001; %step size
+%initailise the circuits at the top
%initailise the container
-N = round(tf/h); %number of iterations
-t = zeros(1, N);
-Vout = zeros(1, N);
-current = zeros(1,N);
%input voltage
% step function of 5 volt
@@ -218,18 +190,9 @@ ylabel({'V_{out}', '(volt)'});
title('(Square wave) V_{out} versus time');
%--------------------------------------------------------------------------------------
-%initailise the circuits
-R = 0.5;
-L = 1.5*10^(-3);
-h = 0.00001; %step size
-
+%initailise the circuits at the top
-%initailise the container
-
-N = round(tf/h); %number of iterations
-t = zeros(1, N);
-Vout = zeros(1, N);
-current = zeros(1,N);
+%initailise the container at the top
%input voltage
% step function of 5 volt
diff --git a/Part 1/midpoint_script.m b/Part 1/midpoint_script.m
new file mode 100644
index 0000000..35ace43
--- /dev/null
+++ b/Part 1/midpoint_script.m
@@ -0,0 +1,104 @@
+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;
+
+vin = @(t) 3.5;
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+[t, iout ] = midpoint(func, ts, tf, is, h);
+
+vout = vin(t) - iout * R;
+plot(t,vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('V_{out} versus time (original function)');
+%____________________________________________________________________
+h = 0.0001;
+tf = 0.03;
+figure;
+A = 3.5;
+tau = 0.000150;
+
+vin = @(t) A * exp(-t.^2/tau);
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+[t, iout ] = midpoint(func, ts, tf, is, h);
+
+vout = vin(t) - iout * R;
+plot(t,vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('V_{out} versus time (exponential square funtion)');
+%____________________________________________________________________
+h = 0.0001;
+tf = 0.03;
+figure;
+A = 3.5;
+tau = 0.000150;
+
+vin = @(t) A * exp(-t/tau);
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+[t, iout ] = midpoint(func, ts, tf, is, h);
+
+vout = vin(t) - iout * R;
+plot(t,vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('V_{out} versus time (exponential function)');
+
+%____________________________________________________________________
+h = 0.0001;
+tf = 0.03;
+figure;
+A = 4;
+T = 0.0015;
+
+vin = @(t) A * sin(2*pi*t/T);
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+[t, iout ] = midpoint(func, ts, tf, is, h);
+
+vout = vin(t) - iout * R;
+plot(t,vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('V_{out} versus time (sine function)');
+%____________________________________________________________________
+h = 0.0001;
+tf = 0.03;
+figure;
+A = 4;
+T = 0.0015;
+
+vin = @(t) A * square(2*pi*t/T);
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+[t, iout ] = midpoint(func, ts, tf, is, h);
+
+vout = vin(t) - iout * R;
+plot(t,vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('V_{out} versus time (square function)');
+%____________________________________________________________________
+h = 0.0001;
+tf = 0.03;
+figure;
+A = 4;
+T = 0.0015;
+
+vin = @(t) A * sawtooth(2*pi*t/T);
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+[t, iout ] = midpoint(func, ts, tf, is, h);
+
+vout = vin(t) - iout * R;
+plot(t,vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('V_{out} versus time (sawtooth function)');
+
+
+
+
+
+
diff --git a/Part 1/ralston_script.m b/Part 1/ralston_script.m
new file mode 100644
index 0000000..91da2a9
--- /dev/null
+++ b/Part 1/ralston_script.m
@@ -0,0 +1,104 @@
+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;
+
+vin = @(t) 3.5;
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+[t, iout ] = ralston(func, ts, tf, is, h);
+
+vout = vin(t) - iout * R;
+plot(t,vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('V_{out} versus time (original function)');
+%____________________________________________________________________
+h = 0.0001;
+tf = 0.03;
+figure;
+A = 3.5;
+tau = 0.000150;
+
+vin = @(t) A * exp(-t.^2/tau);
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+[t, iout ] = ralston(func, ts, tf, is, h);
+
+vout = vin(t) - iout * R;
+plot(t,vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('V_{out} versus time (exponential square funtion)');
+%____________________________________________________________________
+h = 0.0001;
+tf = 0.03;
+figure;
+A = 3.5;
+tau = 0.000150;
+
+vin = @(t) A * exp(-t/tau);
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+[t, iout ] = ralston(func, ts, tf, is, h);
+
+vout = vin(t) - iout * R;
+plot(t,vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('V_{out} versus time (exponential function)');
+
+%____________________________________________________________________
+h = 0.0001;
+tf = 0.03;
+figure;
+A = 4;
+T = 0.0015;
+
+vin = @(t) A * sin(2*pi*t/T);
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+[t, iout ] = ralston(func, ts, tf, is, h);
+
+vout = vin(t) - iout * R;
+plot(t,vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('V_{out} versus time (sine function)');
+%____________________________________________________________________
+h = 0.0001;
+tf = 0.03;
+figure;
+A = 4;
+T = 0.0015;
+
+vin = @(t) A * square(2*pi*t/T);
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+[t, iout ] = ralston(func, ts, tf, is, h);
+
+vout = vin(t) - iout * R;
+plot(t,vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('V_{out} versus time (square function)');
+%____________________________________________________________________
+h = 0.0001;
+tf = 0.03;
+figure;
+A = 4;
+T = 0.0015;
+
+vin = @(t) A * sawtooth(2*pi*t/T);
+func = @(t, iout) (vin(t) - iout*R) / L; % define func
+[t, iout ] = ralston(func, ts, tf, is, h);
+
+vout = vin(t) - iout * R;
+plot(t,vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('V_{out} versus time (sawtooth function)');
+
+
+
+
+
+