clc;
clear all;
close all;
Uz = 120;
Pz = 55;
Uf = 1,5;
Ud = 230*sqrt(2);
f = 120e3;
Utr = 120.7;
eta = 0.8;           %% účinnost
Bmax = 0.3;
kpcu = 0.3;
sigma = 3e6;
uo = 4*pi*10^-7;


%% ETD2910 CF138
ao = 9.5e-3;
bo = 2.5e-3;
ui = 2300;                     %%ue = 1572 ui = 2100;
Al = 2350e-9;          %% nH
lFe = 71e-3;
SFe = 76e-6;
%%

%% ETD2910 CF297
ao = 19.2e-3;
bo = 4.85e-3;
ui = 2300;                     %%ue = 1572 ui = 2100;
Al = 2350e-9;          %% nH
lFe = 71e-3;
SFe = 76e-6;
%% E2005 CF297
## ao = 12e-3;
## bo = 2.95e-3;
## ui = 2300;                     %%ue = 1572 ui = 2100;
## Al = 1700e-9;          %% nH
## lFe = 44.9e-3;
## SFe = 33.5e-6;
%% ETD3411 CF139
% ao = 21e-3;
% bo = 5.6e-3;
% ui = 2100;                     %%ue = 1572 ui = 2100;
% Al = 2500e-9;          %% nH
% lFe = 78.6e-3;
% SFe = 97.1e-6;
%%

%% E1605 CF138
% ao = 9.5e-3;
% bo = 2.5e-3;
% ui = 2100;                     %%ue = 1572 ui = 2100;
% lFe = 37.6e-3;
% SFe = 20.1e-6;

##% E2507 CF195
 ao = 15.8e-3;
 bo = 4.05e-3;
 ui = 2300;                     %%ue = 3200 ui = 5000;
 Al = 2050e-9;          %% nH
 lFe = 57.5e-3;
 SFe = 52.5e-6;
%%
So = ao*bo
UCEmax = Ud + Utr;
smax = 1 - Ud/UCEmax;
L1 = Ud^2*smax^2/(2*f*Pz/eta);
Iu1max = Ud*smax/(f*L1);
SoSj = 2*sqrt(smax/3)*L1*Iu1max^2/(Bmax*kpcu*sigma)  %minimalizace jádra
sqrt(SoSj);
SoSFe = So*SFe
N1 = L1*Iu1max/(Bmax*SFe);
Al1 = L1/N1^2;
L = N1^2*Al; %% L < L1 proto přepočet závitů N
%% pro příliš malou indukčnost L1

lv = 1e-4;
Rmv = (1/(uo))*lv/SFe
RmFe = 1/Al
%N = sqrt(L1*(Rmv+RmFe));
Rmcelk = N1^2/L1
N1 = 47
lvp = N1*uo*Iu1max/Bmax-lFe/ui;
N2 = N1*(Uz+Uf)/Utr;
%%N2 = 30;
L2 = N2^2*Al1;
%%N1 = N2*Utr/(Uz+Uf);
RmFe = (1/(uo*ui))*lFe/SFe;    %%SFe/(uo*ui*lFe);
Rmcelk = N1^2/L1;
Rmv = Rmcelk - RmFe;
lv2 = uo*SFe*Rmv


Iu1ef = Iu1max*sqrt(smax)/sqrt(3);
Iu2str = Pz/Uz;
Iu2ef = 2*Iu2str/(sqrt(1-smax)*sqrt(3));
Iu2max = Iu2ef*sqrt(3)/sqrt(1-smax);
%Iu2max = Iu1max*N1/N2;
s2 = smax*Ud/Utr;
%Iu2ef = Iu2max*sqrt(s2)/sqrt(3);

ro = 3;
Scu1 = Iu1ef/ro;
Scu2 = Iu2ef/ro;
d1 = sqrt(Scu1*4/pi);
d2 = sqrt(Scu2*4/pi);
Scutot = Scu1*N1 + Scu2*N2;
kpcu = Scutot*10^-6/So;

d1z = 0.3;
d2z = 0.3;
Scu1z = 2*pi*d1z^2/4;
Scu2z = 2*pi*d2z^2/4; %% Scu2z = 4*pi*d2z^2/4;
Scutot = Scu1z*N1 + Scu2z*N2;
kpcu1 = Scutot*10^-6/So;
sigma1 = Iu1ef/Scu1z;
sigma2 = Iu2ef/Scu2z;
%% Isense
UIsense = 1;
%PIsense = UIsense*Iu1max;
RIsense = UIsense/Iu1max;
PIsense = RIsense*Iu1ef^2;
%% výst kondenzátor
s2 = smax*Ud/Utr;
Iu2max = Iu2ef*2/(1-s2)
tnab = (1-smax)*(Iu2max-Iu2str)/(f*Iu2max)
C = (Iu2max-Iu2str)*tnab/1
C2 = Iu2str*tnab/1;
C3 = Iu2str*(1/f-tnab)/1;
C1 = Iu2str*smax/f/1;
t = (s2)/f
t1 = tnab;
t2 = (1/f-tnab);
t3 = smax/f
t4 = 1/f
%% filt kondenzátor
Iu1str = Iu1max*smax/2;
dU = 20;
thetha = abs(asin((Ud-dU)/Ud))*180/pi;
tvyb = 1/4*20e-3+thetha/180/2*1/4*20e-3;
Cf = Iu1str* tvyb/dU
%% dimenzování propustné diody
k = N2/N1;
Urr = (Ud*k + Uz)*1.6  % závěrné napětí diody + 50%
Iu2str = 55/120; % proud pro dimenzování diody
Uf = 1.5;
PDztr = Iu2str*Uf;
RthJC = 3.5;
RthJa = 45;
deltaTd = RthJa*PDztr;
Td = 40+deltaTd;
%% dimenzování tranzistoru
ton = (5+16)*1e-9;
toff = (24+12)*1e-9;
T = 60;
Rds_on = 1.5; % pro 25°C 0.95 pro 150 °C 2.22 ohmu
Pzv = Rds_on*Iu1ef^2
Psv = 1/2*f*Ud*Iu1max*(ton+toff)
RthJC = 3;
RthJa = 15;
deltaT = 60-40;
%Rth = deltaT/(Pzv+Psv)-RthJC
deltaT = (RthJC+RthJa)*(Pzv+Psv)
T = 40+deltaT
%% dimenzování
cp = 0.9 %coupling factor of transformer
Lsigma = L1*0.05%%(1-cp^2);
Pzd = f*1/2*Lsigma*Iu1max^2
Pzd1 = Pz/eta*0.1
