aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZihan Liu <zl6114@ic.ac.uk>2017-03-12 14:12:03 +0000
committerGitHub <noreply@github.com>2017-03-12 14:12:03 +0000
commitb9dbb11c8acdbe20c56f5f22342c62d050bbe146 (patch)
tree722c672e1ad4d656e62d4fa404f4efd0fc49aa6d
parent22bb6a946266a0f62d85a442194beaeb6797175f (diff)
downloadNumericalAnalysis-b9dbb11c8acdbe20c56f5f22342c62d050bbe146.tar.gz
NumericalAnalysis-b9dbb11c8acdbe20c56f5f22342c62d050bbe146.zip
Add files via upload
-rw-r--r--Part 1/Huen_script.m41
-rw-r--r--Part 1/heun.m9
2 files changed, 50 insertions, 0 deletions
diff --git a/Part 1/Huen_script.m b/Part 1/Huen_script.m
new file mode 100644
index 0000000..f9f4561
--- /dev/null
+++ b/Part 1/Huen_script.m
@@ -0,0 +1,41 @@
+function Huen_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)5*heaviside(t);
+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
+
+plot(Vout);
+xlabel('T/s');
+ylabel('Vout/V');
+end \ No newline at end of file
diff --git a/Part 1/heun.m b/Part 1/heun.m
new file mode 100644
index 0000000..3b21804
--- /dev/null
+++ b/Part 1/heun.m
@@ -0,0 +1,9 @@
+function [x,y] = heun(func, xa, ya, h)
+
+x = xa + h;
+gradient1 = feval(func, xa, ya); %calculate the gradient at t
+ypredictor=ya+h*gradient1; %calculate predictor for the next value of y
+gradient2=feval(func, x, ypredictor); %calculate the gradient at t + h
+y = ya + h/2*(gradient1 + gradient2);
+
+end \ No newline at end of file