aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMK2020 <mh4115@ic.ac.uk>2017-03-12 14:58:17 +0000
committerGitHub <noreply@github.com>2017-03-12 14:58:17 +0000
commit199d2797b945f13818c116521e1a6aab2d14fab1 (patch)
tree689c29c3d1645557d639a2ba96a6bbed62e25943
parent43c261581bba603aae9713f293e0686ceb74e5e6 (diff)
downloadNumericalAnalysis-199d2797b945f13818c116521e1a6aab2d14fab1.tar.gz
NumericalAnalysis-199d2797b945f13818c116521e1a6aab2d14fab1.zip
Add files via upload
-rw-r--r--Part 1/general_script.m104
-rw-r--r--Part 1/heun_script.m264
-rw-r--r--Part 1/midpoint.m18
-rw-r--r--Part 1/ralston.m25
4 files changed, 411 insertions, 0 deletions
diff --git a/Part 1/general_script.m b/Part 1/general_script.m
new file mode 100644
index 0000000..91da2a9
--- /dev/null
+++ b/Part 1/general_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)');
+
+
+
+
+
+
diff --git a/Part 1/heun_script.m b/Part 1/heun_script.m
new file mode 100644
index 0000000..264942f
--- /dev/null
+++ b/Part 1/heun_script.m
@@ -0,0 +1,264 @@
+function heun_script (tf) %tf is the end time
+
+%initailise the circuits
+R = 0.5;
+L = 1.5*10^(-3);
+h = 0.00001; %step size
+
+
+%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
+Vin = @(t)3.5*heaviside(t);
+
+%the initial condition
+t(1) = 0;
+current(1) = 0;
+
+
+%the equation
+func = @(t,current) (Vin(t)-R*(current))/L; %Function input for difference method
+
+
+%Huen
+for j = 1 : N-1
+ [t(j + 1),current(j + 1)] = heun(func, t(j), current(j), h);
+ Vout(j + 1) = Vin(t(j)) - R*current(j); %Create Vout array from Iout, R and Vin
+end
+
+%plot
+
+figure(1);
+plot(Vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+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
+
+N = round(tf/h); %number of iterations
+t = zeros(1, N);
+Vout = zeros(1, N);
+current = zeros(1,N);
+
+%input voltage
+tau = 0.000150;
+A = 3.5;
+
+Vin = @(t) A * exp(-t.^2/tau);
+
+%the initial condition
+t(1) = 0;
+current(1) = 0;
+
+
+%the equation
+func = @(t,current) (Vin(t)-R*(current))/L; %Function input for difference method
+
+
+%Huen
+for j = 1 : N-1
+ [t(j + 1),current(j + 1)] = heun(func, t(j), current(j), h);
+ Vout(j + 1) = Vin(t(j)) - R*current(j); %Create Vout array from Iout, R and Vin
+end
+
+%plot
+
+figure(2);
+plot(Vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+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 container
+
+N = round(tf/h); %number of iterations
+t = zeros(1, N);
+Vout = zeros(1, N);
+current = zeros(1,N);
+
+%input voltage
+tau = 0.000150;
+A = 3.5;
+
+Vin = @(t) A * exp(-t/tau);
+
+%the initial condition
+t(1) = 0;
+current(1) = 0;
+
+
+%the equation
+func = @(t,current) (Vin(t)-R*(current))/L; %Function input for difference method
+
+
+%Huen
+for j = 1 : N-1
+ [t(j + 1),current(j + 1)] = heun(func, t(j), current(j), h);
+ Vout(j + 1) = Vin(t(j)) - R*current(j); %Create Vout array from Iout, R and Vin
+end
+
+%plot
+
+figure(3);
+plot(Vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+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 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
+Vin = @(t)4*sin(2*pi*6000*t);
+
+%the initial condition
+t(1) = 0;
+current(1) = 0;
+
+
+%the equation
+func = @(t,current) (Vin(t)-R*(current))/L; %Function input for difference method
+
+
+%Huen
+for j = 1 : N-1
+ [t(j + 1),current(j + 1)] = heun(func, t(j), current(j), h);
+ Vout(j + 1) = Vin(t(j)) - R*current(j); %Create Vout array from Iout, R and Vin
+end
+
+%plot
+
+figure(4);
+plot(Vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+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 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
+A = 4;
+T = 0.0015;
+
+Vin = @(t) A * square(2*pi*t/T);
+
+%the initial condition
+t(1) = 0;
+current(1) = 0;
+
+
+%the equation
+func = @(t,current) (Vin(t)-R*(current))/L; %Function input for difference method
+
+
+%Huen
+for j = 1 : N-1
+ [t(j + 1),current(j + 1)] = heun(func, t(j), current(j), h);
+ Vout(j + 1) = Vin(t(j)) - R*current(j); %Create Vout array from Iout, R and Vin
+end
+
+%plot
+
+figure(5);
+plot(Vout);
+xlabel({'Time', '(seconds)'});
+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 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
+A = 4;
+T = 0.0015;
+
+Vin = @(t) A * sawtooth(2*pi*t/T);
+
+%the initial condition
+t(1) = 0;
+current(1) = 0;
+
+
+%the equation
+func = @(t,current) (Vin(t)-R*(current))/L; %Function input for difference method
+
+
+%Huen
+for j = 1 : N-1
+ [t(j + 1),current(j + 1)] = heun(func, t(j), current(j), h);
+ Vout(j + 1) = Vin(t(j)) - R*current(j); %Create Vout array from Iout, R and Vin
+end
+
+%plot
+
+figure(6);
+plot(Vout);
+xlabel({'Time', '(seconds)'});
+ylabel({'V_{out}', '(volt)'});
+title('(Sawtooth wave) V_{out} versus time');
+
+end \ No newline at end of file
diff --git a/Part 1/midpoint.m b/Part 1/midpoint.m
new file mode 100644
index 0000000..7c3ed5c
--- /dev/null
+++ b/Part 1/midpoint.m
@@ -0,0 +1,18 @@
+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;
+
+for i = 1 : N - 1
+ ta(i+1) = ta(i) + h;
+ halfstep = ta(i) + 1 * h / 2;
+ gradient1 = f(ta(i), ya(i));
+ ypredict = ya(i) + 0.5 * h * gradient1;
+ gradient2 = f(halfstep, ypredict);
+ ya(i+1) = ya(i) + h * gradient2;
+end
+x = ta;
+y = ya;
+
+
diff --git a/Part 1/ralston.m b/Part 1/ralston.m
new file mode 100644
index 0000000..6041374
--- /dev/null
+++ b/Part 1/ralston.m
@@ -0,0 +1,25 @@
+function [t,vout] = ralston(func, t0, tf,i0 , h)
+
+n = ((tf - t0) / h);
+a1=1/3;
+a2=2/3;
+p1=3/4;
+q11=3/4;
+
+xa(1)=t0;
+ya(1)=i0;
+
+for i=1:1:n
+ xtemp=xa(i);
+ ytemp=ya(i);
+
+
+k1=func(xtemp,ytemp);
+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