% Orai m-file 2020.V.14.
% Frekvenciatartomanybeli viselkedes, reaktans halozat
% 

% 1. Soros rezgokor (veszteseges)
L = 1; C=1;
R= 1;
num = [1/L 0];
den = [1 R/L 1/(L*C)];
om = logspace(-2,2,1e4);
H = polyval(num, j*om) ./ polyval( den, j*om);
K = abs(H);
fi = angle(H);
figure; semilogx(om, K, 'r-'); xlabel('om'); ylabel('K');
figure; semilogx(om, fi, 'r-'); xlabel('om'); ylabel('K');
R = 0.10;
den = [1 R/L 1/(L*C)];
H = polyval(num, j*om) ./ polyval( den, j*om);
K = abs(H);
figure; semilogx(om, K, 'r-'); xlabel('om'); ylabel('K');
R= 1;
R= 10;
den = [1 R/L 1/(L*C)];
H = polyval(num, j*om) ./ polyval( den, j*om);
K = abs(H);
figure; semilogx(om, K, 'r-'); xlabel('om'); ylabel('K');
figure; semilogx(om, 20*log10(K), 'r-'); xlabel('om'); ylabel('K');
R= 1;
den = [1 R/L 1/(L*C)];
H = polyval(num, j*om) ./ polyval( den, j*om);
K1 = abs(H);
R= 0.1;
den = [1 R/L 1/(L*C)];
H = polyval(num, j*om) ./ polyval( den, j*om);
K0p1 = abs(H);
figure; semilogx(om, 20*log10(K), 'r-', om, 20*log10(K1), 'b-', om, 20*log10(K0p1), 'm-'); xlabel('om'); ylabel('K[dB]');
legend('R=10','R=1','R=0.1', 'Location', 'northeast')
R = 1;
num = 1/(L*C);
den = [1 R/L 1/(L*C)];
H = polyval(num, j*om) ./ polyval( den, j*om);
K = abs(H);
figure; semilogx(om, K, 'r-'); xlabel('om'); ylabel('K');
R
fi = angle(H);
figure; semilogx(om, fi, 'r-'); xlabel('om'); ylabel('fi');
figure; semilogx(om, fi*180/pi, 'r-'); xlabel('om'); ylabel('fi');
R = 0.1;
den = [1 R/L 1/(L*C)];
H = polyval(num, j*om) ./ polyval( den, j*om);
K = abs(H);
fi = angle(H);
figure; semilogx(om, fi*180/pi, 'r-'); xlabel('om'); ylabel('fi');
figure; semilogx(om, K, 'r-'); xlabel('om'); ylabel('K');
R = 10;
den = [1 R/L 1/(L*C)];
H = polyval(num, j*om) ./ polyval( den, j*om);
K = abs(H);
fi = angle(H);
figure; semilogx(om, K, 'r-'); xlabel('om'); ylabel('K');
figure; semilogx(om, fi*180/pi, 'r-'); xlabel('om'); ylabel('fi');

% 2. Idealis erositot tartalmazo halozat atviteli karakterisztikaja
% 
clear all
clc
close all
num = 5*conv([1 0],[10 1])
den = 2*conv([2 1],[5/2 1])
om = logspace(-2,2,1e4);
H = polyval(num, j*om) ./ polyval( den, j*om);
K = abs(H);
figure; semilogx(om, K, 'r-'); xlabel('om'); ylabel('K');
figure; semilogx(om, 20*log10(K), 'r-'); xlabel('om'); ylabel('K');

% 3. Reaktans halozat rezonancia es antirezonancia  
% Csatolt tekercseket tartalmazo halozat, szimbolikus megoldas
clear all
clc
close all
% Szimbolikus megoldas
syms U I1 I2 jw L1 L2 M C I1 I2
whos
eq1 = -U + (I1-I2)/(jw*C) + jw*L1*(I1-I2)+jw*M*I2 ==0;
eq2 = (I2-I1)/(jw*C) +jw*L1*(I2-I1)-jw*M*I2+jw*L2*I2+jw*M*(I1-I2) == 0;
whos
% Egyenletrendszer megoldasa
sol = solve( [eq1, eq2], [I1,I2])
% Megoldas kiirasa
sol.I1
pretty( simplify( sol.I1))
% Valasz kiirasa
ZAB = U / sol.I1
pretty( simplify( ZAB ))
pretty( simplify( collect( ZAB, jw) ))

% Adott ertekek eseteben 
ZABnum = subs( ZAB, {L1,L2,M,C}, {10,20,4.4721,2})
vpa(ZABnum)
% "Atvitel" abrazolasa 
% Igazabol a kozos jw utana tag vizsgalata
% Ez a tag valos erteku lesz, mert a tortfuggveny eseteben a szamlaloban es
% a nevezoben is csak paros szamu kitevo szerepel, amely eseteben (jw)^2n
% erteke +- w^2n lesz
num = [360.0006 0 20];
den = [42.1116 0 1];
om = logspace(-3,2,1e4);
H = polyval(num, j*om) ./ polyval( den, j*om);
whos
figure; semilogx(om, abs(H), 'r-'); xlabel('om'); ylabel('K');
figure; semilogx(om, 20*log10(abs(H)), 'r-'); xlabel('om'); ylabel('K');
figure; semilogx(om, angle(H), 'r-'); xlabel('om'); ylabel('K');