Asked by Venkatkumar M
on 10 Jun 2019

vs=10;

r=1;

c=1;

dt=0.1;

z=dt/(2*c);

i= vs/r;

vi=-(i*z)/2;

for j=0:0.1:1

{

if j = 0

{

vr[j]=0-vi;

}

else

{

vr[j+1]=vr[j];

}

end

ik[j]=(vs-(2*vr[j]))/(r+z);

it[j]=(vs/r)*(exp(-j/(r*c)));

vc[j]=(2*vr[j])+(ik[j]*z);

vcr[j]=vc[j]-vr[j]

}

Answer by Matt J
on 10 Jun 2019

You have written your code with C-language syntax. Matlab and C are not the same language.

Venkatkumar M
on 10 Jun 2019

will u please wirite this in matlab code and post pls

Matt J
on 10 Jun 2019

Sign in to comment.

Answer by Bjorn Gustavsson
on 10 Jun 2019

Edited by Bjorn Gustavsson
on 10 Jun 2019

The curly brackets are used to enclose cell-arrays (lists) in matlab, not start and end loops and if-clauses, matlab uses nothing to start those and end to close them. So:

for i1 = 1:4,

if x > 0

a(i1) = pi^i1;

else

a = -23;

end

end

also, I would suggest staying away from i and j for indexing, since they are the imaginary unit too, sooner or later it might come and bite you at an inopportune moment, I go with i1, i2, or i_t, i_x...

HTH

Venkatkumar M
on 13 Jun 2019

i am also done with the course but i dont get any ideas. right now?

how declare it is as array and useit in looping condition

Rik
on 13 Jun 2019

The process is simple: first number all your variables, then add parentheses:

vr0=0-(vi);

ik0=(vs-(2*vr0))/(r+z);

it0=(vs/r)*exp(0/(r*c));

vc0=(2*vr0)+(ik0*z);

vcr0=vc0-vr0;

vr1=vcr0;

%rest of the code is unchanged

Now the second step: add parentheses, making sure you don't index to 0, but start at 1:

vr(1)=0-(vi);

ik(1)=(vs-(2*vr(1)))/(r+z);

it(1)=(vs/r)*exp(0/(r*c));

vc(1)=(2*vr(1))+(ik(1)*z);

vcr(1)=vc(1)-vr(1);

vr(2)=vcr(1);

Now we can replace the indices with a loop iterator and add the condition for the first loop:

if n==1

vr(1)=0-(vi);

else

vr(n)=vcr(n-1)

end

ik(n)=(vs-(2*vr(n)))/(r+z);

numerator=-0.1*(n-1);

it(n)=(vs/r)*exp(numerator/(r*c));

vc(n)=(2*vr(n))+(ik(n)*z);

vcr(n)=vc(n)-vr(n);

And finally add the loop itself and some pre-allocation:

iteration=3;

vr=zeros(1,iterations);

vcr=zeros(1,iterations);

vc=zeros(1,iterations);

it=zeros(1,iterations);

ik=zeros(1,iterations);

for n=1:iterations

if n==1

vr(1)=0-(vi);

else

vr(n)=vcr(n-1);

end

ik(n)=(vs-(2*vr(n)))/(r+z);

numerator=-0.1*(n-1);

it(n)=(vs/r)*exp(numerator/(r*c));

vc(n)=(2*vr(n))+(ik(n)*z);

vcr(n)=vc(n)-vr(n);

end

And now you need to add comments about what your code is doing and why.

Venkatkumar M
on 14 Jun 2019

my code is on modeling of transmission line.

Thank you very much

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.