Hi dear friends, I wrote a code to model a full bridge rectifier in electrical engineering in MATLAB and it works and the plot of voltage is true. now I am going to write that in Julia. I don’t know why the for loop doesnot work.
although the name of full bridge rectifier may seem unfamiliar with others, It is very basic and I used only if and while for one cycles. it is the plot from MATLAB output
now I used that code from Matlab of course with change according to the Julia programming as follows I don’t know why the for doesnot work
‘’
using Plots
plotly()
Plots.PlotlyBackend()
currentcap=zeros(1,250001);
voltagecap=zeros(1,250001);
currentind=zeros(1,250001);
d= zeros(9,250004);
t_end = 250e-3;
dt = 1e-6;
t = 0:dt:t_end;
vp=20;
freq=50;
vs = vpsin.(2πfreq. t’)
L=1e-3;
R=0.2;
C=1e-6;
c1=0;
c2=0;
c3=0;
c4=0;
c5=0;
c6=0;
i=2;
j=1;
for k=1:12
if vs[1,i]>=0
c1=c1+1;
d[2,i]= vs[1,i];
end
if d[2,i]>d[3,i]
a=1; b=0; f=1; g=0; c=0; e=1; h=0; n=1; x1=d[5,i]-currentind[1,i-1]; x2=currentind[1,i-1]-d[6,i]; x3=d[6,i]-currentcap[1,i-1]; x4=-d[5,i]-d[8,i] ;
Y = [dt/L -dt/L 0 0 1 0 0 0 0
-dt/L dt/L 0 0 0 1 0 0 0
0 0 (1/R)+(C/dt) 0 0 -1 0 0 0
0 0 0 0 -1 0 0 -1 0
1 0 0 -1 0 0 0 0 0
0 a -a 0 0 b 0 0 0
0 0 -c c 0 0 e 0 0
0 0 0 f 0 0 0 g 0
0 h 0 0 0 0 0 0 n];
v =[x1
x2
x3
x4
vp*sin.(2*π*freq.*t'[1,i])
0
0
0
0];
x=inv(Y)*v;
d[:,i]=x;
currentcap[1,i]=(C/dt)*(d[3,i]-d[3,i-1]);
voltagecap[1,i]=d[3,i];
currentind[1,i]=(dt/L)*(d[1,i]-d[2,i])+currentind[1,i-1];
end
i=i+1;
while d[6,i-1]>=0
a=1; b=0; f=1; g=0; c=0; e=1; h=0; n=1; x1=d[5,i]-currentind[1,i-1]; x2=currentind[1,i-1]-d[6,i]; x3=d[6,i]-currentcap[1,i-1]; x4=-d[5,i]-d[8,i];
c2=c2+1;
Y = [dt/L -dt/L 0 0 1 0 0 0 0
-dt/L dt/L 0 0 0 1 0 0 0
0 0 (1/R)+(C/dt) 0 0 -1 0 0 0
0 0 0 0 -1 0 0 -1 0
1 0 0 -1 0 0 0 0 0
0 a -a 0 0 b 0 0 0
0 0 -c c 0 0 e 0 0
0 0 0 f 0 0 0 g 0
0 h 0 0 0 0 0 0 n];
v =[x1
x2
x3
x4
vp*sin.(2*π*freq.*t'[1,i])
0
0
0
0];
x=inv(Y)*v;
d[:,i]=x;
currentcap[1,i]=(C/dt)*(d[3,i]-d[3,i-1]);
voltagecap[1,i]=d[3,i];
currentind[1,i]=(dt/L)*(d[1,i]-d[2,i])+currentind[1,i-1];
i=i+1;
end
if d[6,i]==0
c3=c3+1;
d[4,i]=vs[1,i];
end
if -d[4,i]+d[3,i]>0
a=0; b=1; f=0; g=1; c=1; e=0; h=1; n=0; x1=d[5,i]-currentind[1,i-1]; x2=currentind[1,i-1]+d[9,i]; x3=d[7,i]-currentcap[1,i-1]; x4=-d[5,i]-d[7,i];
Y = [dt/L -dt/L 0 0 1 0 0 0 0
-dt/L dt/L 0 0 0 0 0 0 -1
0 0 (1/R)+(C/dt) 0 0 0 -1 0 0
0 0 0 0 -1 0 1 0 0
1 0 0 -1 0 0 0 0 0
0 a -a 0 0 b 0 0 0
0 0 -c c 0 0 e 0 0
0 0 0 f 0 0 0 g 0
0 h 0 0 0 0 0 0 n];
v =[x1
x2
x3
x4
vp*sin.(2*π*freq.*t'[1,i])
0
0
0
0];
x=inv(Y)*v;
d[:,i]=x;
currentcap[1,i]=(C/dt)*(d[3,i]-d[3,i-1]);
voltagecap[1,i]=d[3,i];
currentind[1,i]=(dt/L)*(d[1,i]-d[2,i])+currentind[1,i-1];
end
i=i+1;
while d[7,i-1]>=0
c5=c5+1;
a=0; b=1; f=0; g=1; c=1; e=0; h=1; n=0; x1=d[5,i]-currentind[1,i-1]; x2=currentind[1,i-1]+d[9,i]; x3=d[7,i]-currentcap[1,i-1]; x4=-d[5,i+1]-d[7,i];
Y = [dt/L -dt/L 0 0 1 0 0 0 0
-dt/L dt/L 0 0 0 0 0 0 -1
0 0 (1/R)+(C/dt) 0 0 0 -1 0 0
0 0 0 0 -1 0 1 0 0
1 0 0 -1 0 0 0 0 0
0 a -a 0 0 b 0 0 0
0 0 -c c 0 0 e 0 0
0 0 0 f 0 0 0 g 0
0 h 0 0 0 0 0 0 n];
v =[x1
x2
x3
x4
vp*sin.(2*π*freq.*t'[1,i])
0
0
0
0];
x=inv(Y)*v;
d[:,i]=x;
currentcap[1,i]=(C/dt)*(d[3,i]-d[3,i-1]);
voltagecap[1,i]=d[3,i];
currentind[1,i]=(dt/L)*(d[1,i]-d[2,i])+currentind[1,i-1];
i=i+1;
end
end
voltagecap=voltagecap’;
plot(t,voltagecap)
‘’
and the figure of this this is as follows:
no need to spend many times for understanding the code.
please just tell me why the for doesnot work . that for should iterate the condition for 12 times but it doesn’t work