aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZihan Liu <zl6114@ic.ac.uk>2017-03-11 15:00:56 +0000
committerGitHub <noreply@github.com>2017-03-11 15:00:56 +0000
commit13f9142d9a0fd4ea76a127d304ce53b00d35a85c (patch)
treef76f796c99d979ab1d80a1dcb6447c2a07a0b574
parent6479c2f9d2238ea3ea464265aa644049d45f840d (diff)
downloadNumericalAnalysis-13f9142d9a0fd4ea76a127d304ce53b00d35a85c.tar.gz
NumericalAnalysis-13f9142d9a0fd4ea76a127d304ce53b00d35a85c.zip
Matlab files ready
still need more plots and the main report
-rw-r--r--Part 2/RK4second.m11
-rw-r--r--Part 2/RLC_script.m38
2 files changed, 49 insertions, 0 deletions
diff --git a/Part 2/RK4second.m b/Part 2/RK4second.m
new file mode 100644
index 0000000..c5403f5
--- /dev/null
+++ b/Part 2/RK4second.m
@@ -0,0 +1,11 @@
+function [qc_next,qc_dash_next] = RK4second(t,qc,qc_dash,h,func1,func2)
+ %source from http://www.mymathlib.com/diffeq/runge-kutta/runge_kutta_3_8.html
+ k1 = func2(t,qc,qc_dash);
+ k2 = func2(t,qc + h/3, qc_dash + h*k1/3);
+ k3 = func2(t,qc + 2*h/3, qc_dash - h*k1/3 + h*k2);
+ k4 = func2(t,qc + h,qc_dash + h*k1 - h*k2 + h*k3);
+ %yi+1 = yi + 1/8 ( k1 + 3 k2 + 3 k3 + k4 )
+ %xi = x0 + i h
+ qc_next = qc + h*func1(t, qc, qc_dash);
+ qc_dash_next = qc_dash + h/8*(k1 + 3*k2 + 3*k3 + k4);
+end \ No newline at end of file
diff --git a/Part 2/RLC_script.m b/Part 2/RLC_script.m
new file mode 100644
index 0000000..73977bc
--- /dev/null
+++ b/Part 2/RLC_script.m
@@ -0,0 +1,38 @@
+function RLC_script(f,h)
+%initailise the circuits
+R = 280;
+L = 600*10^(-3);
+C = 3*60^(-6);
+tf = 1/f;
+N = round(tf/h,0);
+
+qc_dash = zeros(1,N);
+qc = zeros(1,N);
+t = zeros(1,N);
+Vout = zeros(1,N);
+
+%input voltage
+Vin = @(t)5;
+
+%the coupled equation
+func1 = @(t,qc,qc_dash)qc_dash;
+func2 = @(t,qc,qc_dash)(Vin(t) - qc/C - R*qc_dash) / L;
+
+%the initial condition
+t(1) = 0;
+qc(1) = 500*10^(-9);
+qc_dash(1) = 0;
+
+%Rouge Kutta
+for i = 1 : N -1
+ [qc(i+1),qc_dash(i+1)] = RK4second(t(i),qc(i),qc_dash(i),h,func1,func2);
+ t(i + 1) = t(i) + h;
+ Vout(i) = R*qc_dash(i);
+end
+
+%plot
+plot(Vout);
+xlabel('Time');
+ylabel('Amplitude');
+
+end