Illegal use of reserved keyword

4 Ansichten (letzte 30 Tage)
Venkatkumar M
Venkatkumar M am 10 Jun. 2019
Kommentiert: Venkatkumar M am 14 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]
}

Akzeptierte Antwort

Bjorn Gustavsson
Bjorn Gustavsson am 10 Jun. 2019
Bearbeitet: Bjorn Gustavsson am 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
  14 Kommentare
Rik
Rik am 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
Venkatkumar M am 14 Jun. 2019
my code is on modeling of transmission line.
Thank you very much

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 10 Jun. 2019
You have written your code with C-language syntax. Matlab and C are not the same language.
  2 Kommentare
Venkatkumar M
Venkatkumar M am 10 Jun. 2019
will u please wirite this in matlab code and post pls
Matt J
Matt J am 10 Jun. 2019
It should be something pretty easy for you to do yourself, assuming you've been through Matlab's Getting Started documentation.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by