# Using Loop

**URL:** <https://discourse.julialang.org/t/using-loop/64736>\
**Category:** General Usage\
**Tags:** question, performance\
**Created:** [July 16, 2021, 8:18am UTC](https://discourse.julialang.org/t/using-loop/64736 "2021-07-16T08:18:11Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![sajad\_ghassemi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajad_ghassemi/32/25241_2.png) [@sajad\_ghassemi](https://discourse.julialang.org/u/sajad_ghassemi)\
**Post date:** [July 16, 2021, 8:18am UTC](https://discourse.julialang.org/t/using-loop/64736/1 "2021-07-16T08:18:12Z")

</div>

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

 ![error](https://global.discourse-cdn.com/julialang/original/3X/4/a/4a409feb78bdfbc4bb22208d8d043066c4dbcf40.png)  
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 = vp_sin.(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:  
 ![error](https://global.discourse-cdn.com/julialang/original/3X/b/e/bea5a2fa50b91696c6e249b707c813c21194937f.png)  
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

---

<div class="post-metadata">

**Author:** ![sajad\_ghassemi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajad_ghassemi/32/25241_2.png) [@sajad\_ghassemi](https://discourse.julialang.org/u/sajad_ghassemi)\
**Post date:** [July 16, 2021, 8:27am UTC](https://discourse.julialang.org/t/using-loop/64736/3 "2021-07-16T08:27:55Z")

</div>

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:  
 ![error](https://global.discourse-cdn.com/julialang/original/3X/d/f/df08f3e05c94f4d53a4c357c310450f80df2a067.png)

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [July 16, 2021, 8:29am UTC](https://discourse.julialang.org/t/using-loop/64736/4 "2021-07-16T08:29:22Z")

</div>

> [@sajad\_ghassemi](#):
>
> although the name of full bridge rectifier may seem unfamiliar

It’s a [FULL BRIDGE RECTIFIER](https://www.youtube.com/watch?v=9dtPEk73X9U)!

---

<div class="post-metadata">

**Author:** ![sajad\_ghassemi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajad_ghassemi/32/25241_2.png) [@sajad\_ghassemi](https://discourse.julialang.org/u/sajad_ghassemi)\
**Post date:** [July 16, 2021, 8:32am UTC](https://discourse.julialang.org/t/using-loop/64736/5 "2021-07-16T08:32:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [July 16, 2021, 8:40am UTC](https://discourse.julialang.org/t/using-loop/64736/6 "2021-07-16T08:40:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sajad\_ghassemi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajad_ghassemi/32/25241_2.png) [@sajad\_ghassemi](https://discourse.julialang.org/u/sajad_ghassemi)\
**Post date:** [July 16, 2021, 8:48am UTC](https://discourse.julialang.org/t/using-loop/64736/8 "2021-07-16T08:48:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blackeneth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blackeneth/32/10353_2.png) [@blackeneth](https://discourse.julialang.org/u/blackeneth)\
**Post date:** [July 16, 2021, 11:53pm UTC](https://discourse.julialang.org/t/using-loop/64736/9 "2021-07-16T23:53:00Z")

</div>

I was thinking of Mehdi too! I also have the [t-shirt](https://teespring.com/nl/stores/electroboom?)

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 17, 2021, 12:08am UTC](https://discourse.julialang.org/t/using-loop/64736/10 "2021-07-17T00:08:36Z")

</div>

You will [make it a bit easier for the readers willing to help](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) 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.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 17, 2021, 12:15am UTC](https://discourse.julialang.org/t/using-loop/64736/11 "2021-07-17T00:15:45Z")

</div>

Indeed, lines such as

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

```

could hardly be run.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [July 17, 2021, 1:10am UTC](https://discourse.julialang.org/t/using-loop/64736/12 "2021-07-17T01:10:19Z")

</div>

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
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](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#scope-of-variables).

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

```julia
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:

![image](https://global.discourse-cdn.com/julialang/original/3X/5/3/536a31077289aa3fcf85be62930f15a837548889.png)

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](https://docs.julialang.org/en/v1/manual/functions/#man-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](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#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`](https://docs.julialang.org/en/v1/base/io-network/#Base.println) or [`@printf`](https://docs.julialang.org/en/v1/stdlib/Printf/#Printf.@printf) can be used to print specific output.

---

<div class="post-metadata">

**Author:** ![sajad\_ghassemi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajad_ghassemi/32/25241_2.png) [@sajad\_ghassemi](https://discourse.julialang.org/u/sajad_ghassemi)\
**Post date:** [July 17, 2021, 4:23am UTC](https://discourse.julialang.org/t/using-loop/64736/13 "2021-07-17T04:23:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sajad\_ghassemi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajad_ghassemi/32/25241_2.png) [@sajad\_ghassemi](https://discourse.julialang.org/u/sajad_ghassemi)\
**Post date:** [July 17, 2021, 4:32am UTC](https://discourse.julialang.org/t/using-loop/64736/14 "2021-07-17T04:32:55Z")

</div>

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:))))).

```julia
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

```

![image](https://global.discourse-cdn.com/julialang/original/3X/c/1/c18884b84b4737d1aaf00fbfe686766c4c8aea27.png)

---

<div class="post-metadata">

**Author:** ![sajad\_ghassemi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajad_ghassemi/32/25241_2.png) [@sajad\_ghassemi](https://discourse.julialang.org/u/sajad_ghassemi)\
**Post date:** [July 17, 2021, 4:55am UTC](https://discourse.julialang.org/t/using-loop/64736/15 "2021-07-17T04:55:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 17, 2021, 8:36am UTC](https://discourse.julialang.org/t/using-loop/64736/16 "2021-07-17T08:36:18Z")

</div>

I have already included a link to a webpage with such advices in my first post here. But here it goes again: [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757).

---

<div class="post-metadata">

**Author:** ![sajad\_ghassemi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajad_ghassemi/32/25241_2.png) [@sajad\_ghassemi](https://discourse.julialang.org/u/sajad_ghassemi)\
**Post date:** [July 17, 2021, 9:39am UTC](https://discourse.julialang.org/t/using-loop/64736/17 "2021-07-17T09:39:09Z")

</div>

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;)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 17, 2021, 11:47am UTC](https://discourse.julialang.org/t/using-loop/64736/18 "2021-07-17T11:47:52Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![sajad\_ghassemi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajad_ghassemi/32/25241_2.png) [@sajad\_ghassemi](https://discourse.julialang.org/u/sajad_ghassemi)\
**Post date:** [July 18, 2021, 6:31am UTC](https://discourse.julialang.org/t/using-loop/64736/19 "2021-07-18T06:31:07Z")

</div>

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](https://discourse.julialang.org/u/zdenek_hurak)🙂
