Illegal use of reserved keyword

9 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by