% MR719 - masodrendu rendszer, no. 719
% szimbolikus megoldas az allapotvaltozos leiras megallapitasahoz
clear all
clc

syms R L C us u1 u2 u i iv uv iv uc ii
eq1 = -i + (u1-u2)/R + C*uv == 0;
eq2 = (u-u1)/(2*R) + C* uv == 0;
eq3 = (u2-u1)/R + (u2-us)/R + i ==0 ;
eq4 = u2 == u1 + L*iv;
eq5 = ii == (us-u2)/R;

sol = solve([eq1, eq2, eq3, eq4, eq5], [uv, iv, ii, u1, u2]);

pretty( collect( simplify( sol.uv), [u, i, us]))
pretty( collect( simplify( sol.iv), [u, i, us]))
pretty( collect( simplify( sol.ii), [u, i, us]))

%% lengosegi vizsgalat
% L=1; C=1 eseteben mikor lesz lengo 
% clear all
% 
% L=1; C=1;
% R = 0:.01:10;
% Dv = (L+3*R.*R*C).^2-4*L*C.*(R.^2);
% figure;
% plot(R, Dv, 'r--');

%% Nem lengo jellegu megoldas (
clear all
clc

U0 = 10;
L = 1; C = 1; R=2;

A = [-1/(4*R*C) 1/(4*C);-1/(4*L) -3*R/(4*L)];
B = [1/(4*R*C);1/(4*L)];
CT= [-1/(4*R) 1/4];
D = [1/(4*R)];

[m,la] = eig(A)
la1 = la(1,1); la2=la(2,2); 
m1 = m(:,1); m2 = m(:,2);

XG = A \ ( -B*U0)

kv = m \ (-XG)

k1 = kv(1);k2=kv(2);
xe11 = k1*m1(1); xe12 = k2*m2(1); 
xe21 = k1*m1(2); xe22 = k2*m2(2);
%% allapotvaltozok
t = -0.2:0.001:20;
x1 = stepfun(t,0).*(xe11*exp(la1*t) + xe12 *exp(la2*t) + XG(1));
x2 = stepfun(t,0).*(xe21*exp(la1*t) + xe22 *exp(la2*t) + XG(2));

figure;
  plot(t,x1,'r--', t,x2, 'k--', 'LineWidth',2);
  xlabel('t'); ylabel('x'); title('MR719 - allapotvaltozok');
axis([min(t) max(t) -Inf Inf])

%% valasz 
ye1 = CT*[xe11;xe21]
ye2 = CT*[xe12;xe22]
yg = CT*XG+D*U0

yt = stepfun(t,0).*(ye1*exp(la1*t)+ye2*exp(la2*t) + yg);

figure; 
  plot(t,yt,'b-', 'LineWidth', 2); 
  xlabel('t'); ylabel('y'); title('MR719 - valasz');
  
  