aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZihan Liu <zl6114@ic.ac.uk>2017-03-12 12:09:11 +0000
committerGitHub <noreply@github.com>2017-03-12 12:09:11 +0000
commita68342d19016cadb1b2e151f991d1ef66e1957d1 (patch)
tree74c1c7e972fd27e844c67242a21eb4eae85fb3fd
parent13f9142d9a0fd4ea76a127d304ce53b00d35a85c (diff)
downloadNumericalAnalysis-a68342d19016cadb1b2e151f991d1ef66e1957d1.tar.gz
NumericalAnalysis-a68342d19016cadb1b2e151f991d1ef66e1957d1.zip
Add files via upload
-rw-r--r--Part 2/RK4second.m16
-rw-r--r--Part 2/RLC_script.m43
2 files changed, 33 insertions, 26 deletions
diff --git a/Part 2/RK4second.m b/Part 2/RK4second.m
index c5403f5..065665f 100644
--- a/Part 2/RK4second.m
+++ b/Part 2/RK4second.m
@@ -1,11 +1,11 @@
-function [qc_next,qc_dash_next] = RK4second(t,qc,qc_dash,h,func1,func2)
+function [xnext, ynext] = RK4second(t, x, y, h, f1, f2)
%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);
+ ky1 = f2(t, x, y);
+ ky2 = f2(t, x + h/3, y + h*ky1/3);
+ ky3 = f2(t, x + 2*h/3, y - h*ky1/3 + h*ky2);
+ ky4 = f2(t, x + h, y + h*ky1 - h*ky2 + h*ky3);
%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
+ xnext = x + h*f1(t, x, y);
+ ynext = y + h/8*(ky1 + 3*ky2 + 3*ky3 + ky4);
+end
diff --git a/Part 2/RLC_script.m b/Part 2/RLC_script.m
index 73977bc..881d941 100644
--- a/Part 2/RLC_script.m
+++ b/Part 2/RLC_script.m
@@ -1,38 +1,45 @@
-function RLC_script(f,h)
+function RLC_script (tf)
+
%initailise the circuits
-R = 280;
-L = 600*10^(-3);
-C = 3*60^(-6);
-tf = 1/f;
-N = round(tf/h,0);
+R = 250;
+L = 650*10^(-3);
+C = 3*10^(-6);
+h = 0.00001; %step size
+
+%initailise the container
-qc_dash = zeros(1,N);
-qc = zeros(1,N);
-t = zeros(1,N);
-Vout = zeros(1,N);
+N = round(tf/h); %number of iterations
+qc = zeros(1, N); %x
+qc_dash = zeros(1, N); %y
+t = zeros(1, N);
+Vout = zeros(1, N);
%input voltage
-Vin = @(t)5;
+% step function of 5 volt
+Vin = @(t)5*heaviside(t);
+%Vin = @(t)
%the coupled equation
-func1 = @(t,qc,qc_dash)qc_dash;
-func2 = @(t,qc,qc_dash)(Vin(t) - qc/C - R*qc_dash) / L;
+func1 = @(t, qc, qc_dash)qc_dash;
+func2 = @(t, qc, qc_dash)(Vin(t) - qc/C - R*qc_dash)/L;
%the initial condition
+qc_dash(1) = 0;
+qc(1) = 500*(10.^-9);
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);
+for i = 1 : N - 1
t(i + 1) = t(i) + h;
+ [qc(i + 1), qc_dash(i + 1)] = RK4second(t(i), qc(i), qc_dash(i), h, func1, func2);
Vout(i) = R*qc_dash(i);
end
%plot
+
plot(Vout);
xlabel('Time');
ylabel('Amplitude');
-
end
+
+