clc;
clear all;
close all;
Uz1 = 14 %12;
Uz2 = 5;
Uz3 = 3.9%3.3;
Pz = 12.85;
Uf = 0.7;
Ud = 230*sqrt(2);
Udd = 17.5;
Ivdd = 0.1;
f = 80e3;
Utr = 120.7;
eta = 0.75;           %% účinnost
Bmax = 0.3;
kpcu = 0.3;
sigma = 3e6;
uo = 4*pi*10^-7;
%% pro 3.3 V rozmezí 2 - 3.6 V
%% pro 5 V   rozmezí 4.5 - 5.5 V



%%
%% 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;
%% E1605 CF138
% ao = 9.5e-3;
% bo = 2.5e-3;
% ui = 2100;                     %%ue = 1572 ui = 2100;
% Al = 2350e-9;          %% nH
% 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
lvp = N1_*uo*Iu1max/Bmax-lFe/ui;
% lv = 1e-4;
% Rmv = (1/(uo))*lv/SFe
% RmFe = 1/Al
% N1 = sqrt(L1*(Rmv+RmFe));
% Rmcelk = N1^2/L1
UF1 = 1;
UF2 = 0.45%0.52; %0.9 0.65
UF3 = 0.45; %0.8 0.55

N2 = N1_*(Uz1+UF1)/Utr;

L2 = N2^2*Al1;
%%N1 = N2*Utr/(Uz+Uf);
N3 = N1_*(Uz2+UF2)/Utr;
L3 = N3^2*Al1;
N4 = N1_*(Uz3+UF3)/Utr;
L4 = N4^2*Al1;
%% přepočet
N4 = 4; %N4 = 3;
N1_s = N4*Utr/(Uz3+Uf);
N1 = 105 ;%N1 = 121;
N2_s = N1*(Uz1+UF1)/Utr;
N3_s = N1*(Uz2+UF2)/Utr;


N2 = 14;%N2 = 13;
N3 = 5;%N3 =  6;

lvp_s = N1*uo*Iu1max/Bmax-lFe/ui;
RmFe = (1/(uo*ui))*lFe/SFe;    %%SFe/(uo*ui*lFe);
Rmcelk = N1^2/L1;
Rmv = Rmcelk - RmFe;
lv2 = uo*SFe*Rmv

L2 = N2^2*Al1;
L3 = N3^2*Al1;
L4 = N4^2*Al1;
Uz1 = N2*Utr/N1-UF1
Uz2 = N3*Utr/N1-UF2
Uz3 = N4*Utr/N1-UF3

Iu1ef = Iu1max*sqrt(smax)/sqrt(3);
Iu2max = Iu1max*N1/N2;
s2 = smax*Ud/Utr;
Iu2str = 0.1;
Iu3str = 2;
Iu4str = 0.5;

Iu2ef = 2*Iu2str/(sqrt(1-smax)*sqrt(3));
Iu3ef = 2*Iu3str/(sqrt(1-smax)*sqrt(3));
Iu4ef = 2*Iu4str/(sqrt(1-smax)*sqrt(3));
Iu2max = Iu2ef*sqrt(3)/sqrt(1-smax);
Iu3max = Iu3ef*sqrt(3)/sqrt(1-smax);
Iu4max = Iu4ef*sqrt(3)/sqrt(1-smax);
ro = 3;
Iucelk = Iu2ef+Iu3ef+Iu4ef
Scucelk = Iucelk/ro;
dcelk = sqrt(Scucelk*4/pi);

ro = 3;
Scu1 = Iu1ef/ro;
Scu2 = Iu2ef/ro;
Scu3 = (Iu3ef)/ro;
Scu4 = (Iu4ef+Iu3ef)/ro;
d1 = sqrt(Scu1*4/pi);
d2 = sqrt(Scu2*4/pi);
d3 = sqrt(Scu3*4/pi);
d4 = sqrt(Scu4*4/pi);
Scutot = Scu1*N1 + Scu2*N2 + Scu3*(N3-N4) + Scu4*N4;
kpcu = Scutot*10^-6/So

d1z = 0.2;
d2z = 0.3;
d3z = 0.4;
d4z = 0.4;
Scu1z = pi*d1z^2/4;
Scu2z = pi*d2z^2/4;
Scu3z = 7*pi*d3z^2/4;
Scu4z = 9*pi*d4z^2/4;
Scutot = Scu1z*N1 + Scu2z*N2 + Scu3z*(N3-N4) + Scu4z*N4;
kpcu1 = Scutot*10^-6/So
sigma1 = Iu1ef/Scu1z;
sigma2 = Iu2ef/Scu2z;
sigma3 = (Iu3ef)/Scu3z;
sigma4 = (Iu4ef+Iu3ef)/Scu4z;
%% Isense
UIsense = 1;
%PIsense = UIsense*Iu1max;
RIsense = UIsense/Iu1max;
PIsense = RIsense*Iu1ef^2;
%% výst kondenztor 12V
deltaUb12 = 0.02;
s2 = smax*Ud/Utr;
%Iu2max = Iu2ef*2/(1-s2)                           %%
tnab = (1-smax)*(Iu2max-Iu2str)/(f*Iu2max)
%C = (Iu2max-Iu2str)*tnab/0.1
%%C2 = Iu2str*tnab/0.01;
C1b12 = Iu2str*(1/f-tnab)/deltaUb12;
%C12_1 = Iu2str*smax/f/0.1;
%% Výst LC filtr 12V
fr = 1644;
Au = 500;
deltaUzb12 = 40e-6;
Au12 = deltaUb12/deltaUzb12;
AudB = 20*log10(Au12);
fr1 = f/10^(AudB/40);
L_12 = 100e-6;
dIL_12 = deltaUb12*smax/4/L_12/f;
C2b12 = 1/(2*pi*fr1)^2/L_12
%% v�st kondenztor 5V
deltaUb5 = 0.058;
s2 = smax*Ud/Utr;
%Iu3max = Iu3ef*2/(1-s2)                          %%
tnab = (1-smax)*(Iu3max-Iu3str)/(f*Iu3max)
C1b5 = Iu3str*(1/f-tnab)/deltaUb5;           %% 235 uF
%% V�st LC filtr 5V
fr = 1644;
deltaUzb5 = 18.5e-6;
Au5 = deltaUb5/deltaUzb5
AudB = 20*log10(Au5);
fr1 = f/10^(AudB/40);
L_5 = 80e-6;
dIL_5 = deltaUb5*smax/4/L_5/f;
C2b5 = 1/(2*pi*fr1)^2/L_5                    %% 141 uF / 188 uF
%% v�st kondenztor 3V
deltaUb3 = 0.04;
s2 = smax*Ud/Utr;
%Iu4max = Iu4str*2/(1-s2)                          %%
tnab = (1-smax)*(Iu4max-Iu4str)/(f*Iu4max)
C1b3 = Iu4str*(1/f-tnab)/deltaUb3;          %% 94 uF
%% V�st LC filtr 3V
fr = 1644;
deltaUzb3 = 35e-6;
Au3 = deltaUb3/deltaUzb3
AudB = 20*log10(Au3);
fr1 = f/10^(AudB/40);
L_3 = 100e-6;
dIL_3 = deltaUb3*smax/4/L_3/f;
C2b3 = 1/(2*pi*fr1)^2/L_3                   %% 47 uF
##%% v�st kondenztor 3,3V
##deltaU_33 = 0.05;
##s2 = smax*Ud/Utr;
##Iu4max = Iu4ef*2/(1-s2)
##tnab = (1-s2)*(Iu4max-Iu4ef)/(f*Iu4max)
##C33_v = (Iu4max-Iu4ef)*tnab/deltaU_33
%% 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
k1 = N2/N1;
Urr1 = (Ud*k1 + Uz1)*1.6;
k1 = N3/N1;
Urr2 = (Ud*k1 + Uz2)*1.6;
k1 = N4/N1;
Urr3 = (Ud*k1 + Uz3)*1.6;
UF1 = 1;
UF2 = 0.52%0.52; %0.9 0.65
UF3 = 0.45; %0.8 0.55
Pd1ztr = Iu2str*UF1;
Pd2ztr = Iu3str*UF2;
Pd3ztr = Iu4str*UF3;
RthJa = 70; %cca 70 pro TO 220 bez chladiče
deltaT = 60-40;
deltaTd1 = (RthJa)*(Pd1ztr)
Td1 = 40+deltaTd1
RthJMB = 2.7;
RthH = 21;
RthJa = 62; %cca 50 pro TO 220 bez chladiče
RthC = ((RthJMB + RthH)^-1 + RthJa^-1 )^-1;
%Rthd2 = deltaT/(Pd2ztr)-RthJC
deltaTd2 = (RthC)*(Pd2ztr)%(RthC)*(Pd2ztr)
Td2 = 40+deltaTd2
RthJa = 62; %cca pro TO 220 bez chladiče
deltaTd3 = (RthJa)*(Pd3ztr)
Td3 = 40+deltaTd3
%Rthd3 = deltaT/(Pd3ztr)-RthJC
RthJC = 3;
RthJa = 800; %cca pro TO 220 bez chladiče
%% dimenzování tranzistoru
ton = (10+8)*1e-9;
toff = (60+13)*1e-9;
T = 60;
Rds_on = 1.36; %%1.25; % 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.41;
RthJa = 62;
deltaT = 60-40;
%Rth = deltaT/(Pzv+Psv)-RthJC
deltaT = (RthJC+RthJa)*(Pzv+Psv)
T = 40+deltaT
%%
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
