Illegal use of reserved keyword

조회 수: 8 (최근 30일)
Venkatkumar M
Venkatkumar M 2019년 6월 10일
댓글: Venkatkumar M 2019년 6월 14일
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]
}

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2019년 6월 10일
편집: Bjorn Gustavsson 2019년 6월 10일
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
Rik
Rik 2019년 6월 13일
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 2019년 6월 14일
my code is on modeling of transmission line.
Thank you very much

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by