1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
function RLC_Script (tf)
%initailise the circuits
R = 250;
L = 650*10^(-3);
C = 3*10^(-6);
h = 0.00001; %step size
%initailise the container
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
%%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;
%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 the input function
plot(t, Vin(t));
%Plot the output of the system
figure
plot(t, Vout);
xlabel('Time');
ylabel('Amplitude');
end
|