# Using Loop

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

if above code is too long and it is unclear to understand, I summarized the code and show it only for and while and if as follows:

It’s a FULL BRIDGE RECTIFIER!

4 Likes

I am in hurry and I wrote more than 30 codes in MATLAB as well as Julia. I don’t care about my spell and name of circuit. it doesn’t matter full or Fool or whatever. please take a look at code.

I’m not correcting anything, just sharing a fun link about full bridge rectifiers.

1 Like

sorry I didn’t get. thanks for the help atleast I laghued:)

I was thinking of Mehdi too! I also have the t-shirt

2 Likes

You will make it a bit easier for the readers willing to help if the full code is properly between triple backticks, not just part of it. Copying&pasting the code is now unnecessarily less convenient. Even that can increase the chances that somebody will have a look and play with your code.

3 Likes

Indeed, lines such as

``````vs = vpsin.(2πfreq. t’)
``````

could hardly be run.

1 Like

Please make sure your code is between 3 backticks, I advice you to go through the link posted by @zdenek_hurak. If you follow those recommendations, you will more likely get help.

Are you sure your code is only running once?
I put your code in a file and tried to run it in the REPL. After fixing many typos I got these warnings, have you seen them before? They are important and they are actually indicating you how to fix your code.

``````julia> include("JuliaLoopMatlab.jl")
┌ Warning: Assignment to `c1` in soft scope is ambiguous because a global variable by the same name exists: `c1` will be treated as a new local. Disambiguate by using `local c1` to suppress this warning or `global c1` to assign to the existing global variable.
└ @ ~/Desktop/JuliaLoopMatlab.jl:24
┌ Warning: Assignment to `i` in soft scope is ambiguous because a global variable by the same name exists: `i` will be treated as a new local. Disambiguate by using `local i` to suppress this warning or `global i` to assign to the existing global variable.
└ @ ~/Desktop/JuliaLoopMatlab.jl:53
┌ Warning: Assignment to `c2` in soft scope is ambiguous because a global variable by the same name exists: `c2` will be treated as a new local. Disambiguate by using `local c2` to suppress this warning or `global c2` to assign to the existing global variable.
└ @ ~/Desktop/JuliaLoopMatlab.jl:56
┌ Warning: Assignment to `c3` in soft scope is ambiguous because a global variable by the same name exists: `c3` will be treated as a new local. Disambiguate by using `local c3` to suppress this warning or `global c3` to assign to the existing global variable.
└ @ ~/Desktop/JuliaLoopMatlab.jl:84
┌ Warning: Assignment to `c5` in soft scope is ambiguous because a global variable by the same name exists: `c5` will be treated as a new local. Disambiguate by using `local c5` to suppress this warning or `global c5` to assign to the existing global variable.
└ @ ~/Desktop/JuliaLoopMatlab.jl:115

``````

They are very important, what they are telling you is that the variables `i`, and `c1` to `c5` are not being modified inside your loop. Instead you are creating new variables each time that use the value from the ones you defined outside. This is why it appears that the loop only runs once. You can read more about it in the Manual.

Now if I add the word global before all these variables:

``````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 = vp*sin.(2π*freq.* t')
L=1e-3;
R=0.2;
C=1e-6;
global c1=0;
global c2=0;
global c3=0;
global c4=0;
global c5=0;
global c6=0;
global i=2;
global j=1;
for k=1:12
if  vs[1,i]>=0
global 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
global 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];
global 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];
global i=i+1;
end

if  d[6,i]==0
global 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
global i=i+1;
while  d[7,i-1]>=0
global 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];
global i=i+1;
end
end
``````

I get the result you want:

This is because with the word `global`, I instructed Julia to use inside the loop, the same variables as outside the loop.

This makes your program work but note that it will hurt performance if that is something you are interested in. In general the more idiomatic way of writing your code is using functions and passing all those variables as arguments to those functions.

Also from the manual I advice you to go through the section Noteworthy differences from MATLAB. This section explains many useful things when migrating. For example:

• Julia discourages the use of semicolons to end statements. The results of statements are not automatically printed (except at the interactive prompt), and lines of code do not need to end with semicolons. `println` or `@printf` can be used to print specific output.
6 Likes

yeah yeah. Thanks for the advice. it was so practical.

Thanks for the time and energy you spent. you are amazing. it works and the changes you made makes the code disciplined.
I was in a hurry to present it to my professor and I didnot time to make it better although yesterday my professor told me your coding performance is shaky and my code is not smart. I should write from beginning with different logic:( (LOL)

To answer your question about running once I should say I have a problem with Atoms as an IDE from the beginning in my loptop(X556UB with windows 10). I don’t know why when I write a code in it and then I press Run All it doesn’t run. I have to copy code and paste it in REPL. I aksed how can I fix this problem in discourse.julia but no one could not fix this and at the end they suggested me to download another IDE. but I didnot have time to install another:)

yesterday I changed the place of for loop with cliking Space in keyboard and it worked:))))).

``````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 = vp*sin.(2*π*freq.* t')
L=1e-3;
R=0.05;
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
``````

hmm. so how can I make it better?
Is there any course from any website or books I can learn this tips?
I am a newbie in Julia and I am not really familiar with coding tips.
it was my second assignment in Julia.

2 Likes

I am really sorryyyyy. You’re right. I am swamped since I should send a new code in MATLAB until night to my professor and I didn’t have time to modifiy the previous code.
You know the code doesn’t work and I have to write a new one.
I promise I will listen to that advice from next code;)

2 Likes

If this is some homework assignment, people here generally appreciate it if it’s made clear in the initial question that it is. We’re all just volunteers giving their free time to help people, but part of that requires an honest approach and making a good effort to make it easy for us to help with your problem.

In this case, it seems like the code you originally posted was written for a very old version of julia (if it worked in the past, it was probably before the scoping changes that happened in 0.7/1.0, or maybe around the second softer set of changes that happened later), which is the reason it doesn’t work in a more recent version like 1.6.1.

Thank you for considering the links posted by other people in the future - we’re always happy to help people learn julia after all!

6 Likes

Thanks for the response.
actually, for 5 years I worked with simulation and I haven’t written any code since 2015. That’s why my mind doesn’t work like a programmer.
the version of my Julia is 1.6.1.
I promise I will consider everything according to that links which is shared by dear zdenek_hurak