diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-12 14:53:21 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-12 14:53:21 +0000 |
commit | 43c261581bba603aae9713f293e0686ceb74e5e6 (patch) | |
tree | af8fc176caa8df29792604305c8337f0e017e3f8 | |
parent | 5f9760ac7f47dcef22a8bdaaa45cdbbb13170c53 (diff) | |
parent | b665a01b921753dfd3cfc4c26711685c93848145 (diff) | |
download | NumericalAnalysis-43c261581bba603aae9713f293e0686ceb74e5e6.tar.gz NumericalAnalysis-43c261581bba603aae9713f293e0686ceb74e5e6.zip |
Merge branch 'master' of github.com:ymherklotz/NumericalAnalysis
-rw-r--r-- | Part 1/Huen_script.m | 41 | ||||
-rw-r--r-- | Part 1/heun.m | 9 | ||||
-rw-r--r-- | Part 2/RLC_script.m | 29 |
3 files changed, 71 insertions, 8 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 diff --git a/Part 2/RLC_script.m b/Part 2/RLC_script.m index 881d941..f17f77e 100644 --- a/Part 2/RLC_script.m +++ b/Part 2/RLC_script.m @@ -1,4 +1,4 @@ -function RLC_script (tf)
+function RLC_Script (tf)
%initailise the circuits
R = 250;
@@ -15,31 +15,44 @@ t = zeros(1, N); Vout = zeros(1, N);
%input voltage
-% step function of 5 volt
-Vin = @(t)5*heaviside(t);
-%Vin = @(t)
+%%step function of 5 volt
+%Vin = @(t)5*heaviside(t);
+
+%%Impulse with Exponential Decay
+%Tau = 3*(10^-6);
+%Vin = @(t)5*exp(-(t^2)/Tau);
+
+%%Square Wave (5Hz, 100Hz, 500Hz)
+%Vin = @(t)5*square(2*pi*5*t);
+
+
+%%Sine Wave (5Hz, 100Hz, 500Hz)
+Vin = @(t)5*sin(2*pi*5*t);
%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
qc_dash(1) = 0;
qc(1) = 500*(10.^-9);
t(1) = 0;
-%Rouge Kutta
+%Runge Kutta
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 the input function
+plot(t, Vin(t));
-plot(Vout);
+%Plot the output of the system
+figure
+plot(t, Vout);
xlabel('Time');
ylabel('Amplitude');
end
-
|