Skip to content

Instantly share code, notes, and snippets.

@ethanmpeterson
Last active June 27, 2019 01:04
Show Gist options
  • Select an option

  • Save ethanmpeterson/099ca686f1219b4b5c45e71985385a24 to your computer and use it in GitHub Desktop.

Select an option

Save ethanmpeterson/099ca686f1219b4b5c45e71985385a24 to your computer and use it in GitHub Desktop.
MTHE 225 Assignment #1: Matlab script for calculating amplitude responses of an RLC circuit to certain frequencies.
% RLC circuit calculation tool
% MTHE 225
% Ethan Peterson
% Student #: 20105011
% June 13, 2019
% Setup
clc % Clear previous output
% RLC Circuit Values
R = 1; % in ohms
inductors = [0.1 0.47 1 4.7 10 47 100 470]; % in uH
capacitors = [0.100 0.330 0.680 1 3.3 6.8 10 33 68 100]; % in nF
% 80 possible circuit configs
% Frequency Values (in Hz)
targetFreq = 1200 * 10^3;
unwantedRanges = [
600*10^3 1150*10^3
1220*10^3 1600*10^3
];
circuitConfigs = [];
conf = [];
maxA = 0;
bestL = 0;
bestC = 0;
file = fopen('configs.txt', 'w');
for i = 1 : length(inductors)
for j = 1 : length(capacitors)
L = inductors(i);
C = capacitors(j);
A = amp(L * 10^-6, C * 10^-9, targetFreq);
conf(1).A = A;
conf(1).L = L;
conf(1).C = C;
circuitConfigs = [circuitConfigs; conf]; % records all circuit configs in a struct object from worst to best
fprintf(file,'A = %f, L = %f, C = %f\n', A, L, C);
if A > maxA
maxA = A;
bestL = L;
bestC = C;
end
end
end
fclose(file);
disp("best")
disp(maxA)
disp(bestL)
disp(bestC)
% Sort by Amplitude values
amplitudes = [];
for i = 1 : length(circuitConfigs)
amplitudes = [amplitudes; circuitConfigs(i).A];
end
amplitudes = sort(amplitudes);
% find the circuit configs with the top 5 amplitude values
bestAmplitudes = amplitudes(end-5+1:end);
bestConfigs = [];
for i = 1 : length(bestAmplitudes)
A = bestAmplitudes(i);
for j = 1 : length(circuitConfigs)
if circuitConfigs(j).A == A
bestConfigs = [bestConfigs; circuitConfigs(j)];
end
end
end
disp("best 5")
disp(bestConfigs(1))
% Test against unwanted Frequencies
minConfig = bestConfigs([]);
maxConfig = bestConfigs([]);
minA = [100, 100, 100,100,100];
maxA = zeros(5);
for i = 1 : length(bestConfigs)
L = bestConfigs(i).L;
C = bestConfigs(i).C;
for k = 600*10^3 : 1150*10^3
A = amp(L * 10^-6, C * 10^-9, k);
if A > maxA(i)
maxA(i) = A;
maxConfig(i) = bestConfigs(i);
end
end
end
disp("max amongst unwanted")
disp(maxConfig(3))
disp(amp(0.1 * 10^-6, 100 * 10^-9, 1200*10^3))
disp("test")
disp(amp(0.47 * 10^-6, 33 * 10^-9, 1200*10^3))
% Test against resonance freq function
diff = targetFreq;
targetConf = circuitConfigs([]);
for i = 1 : length(circuitConfigs)
w = freq(circuitConfigs(i).L * 10^-6, circuitConfigs(i).C * 10^-9);
if abs(targetFreq - w) < diff
diff = abs(targetFreq - w);
targetConf(1) = circuitConfigs(i);
end
end
%disp(targetConf)
function amplitude = amp(L, C, f)
amplitude = (f*2*pi) / sqrt((L*(2*pi*f)^2 - 1/C)^2 + (f * 2 * pi)^2);
end
function omega = freq(L, C)
omega = 1 / sqrt(L * C);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment