Storing decimal numbers in for loop: getting a single vector

I want to store decimal numbers in the for loop.
for e = 0:0.1:1
k = sqrt(2.*e);
q = sqrt(2.*(e+(V0./2)));
r = sqrt (2.*(e+V0));
a = ((r+q+(2.*s1.*lambda)).*(k+q+(2.*s1.*lambda)).*expm(s1.*(r-q).*0.48)) - ((r-q+ (2.*s1.*lambda)).*(k-q+2.*s1.*lambda).*expm(s1.*(r+q).*0.48));
Trans = (4.*k.*q)./a;
wfn = Trans.*conj(Trans);
posit1(e,1) = e;
wfn1(e,1) = wfn;
end
plot( posit1, wfn1);
I want to store all the values from 0 to 1 with an interval of 0.1 Please help me how to do it??

 採用された回答

Mischa Kim
Mischa Kim 2014 年 2 月 25 日
編集済み: Mischa Kim 2014 年 2 月 25 日

0 投票

Parikshit, you cannot use non-integer indices (such as 0.1) to access or write to a matrix. Also, indexing in MATLAB starts with 1, not 0. Instead, use
...
e = 0:0.l:1;
for ii = 1:length(e)
k = sqrt(2.*e(ii));
q = sqrt(2.*(e(ii)+(V0./2)));
r = sqrt(2.*(e(ii)+V0));
a = ((r+q+(2.*s1.*lambda)).*(k+q+(2.*s1.*lambda)).*expm(s1.*(r-q).*0.48)) - ((r-q+ (2.*s1.*lambda)).*(k-q+2.*s1.*lambda).*expm(s1.*(r+q).*0.48));
Trans = (4.*k.*q)./a;
wfn = Trans.*conj(Trans);
posit1(ii) = e(ii);
wfn1(ii) = wfn;
end
Some other suggestions:
  • Instead of accessing e(ii) time and again in the loop, you could do a e_ii = e(ii) as the first line in the loop, and then use e_ii throughout the loop.
  • Especially for large matrices you would want to pre-allocate. That is, put
posit1 = zeros(length(e),1);
wfn1 = zeros(length(e),1);
in front of the for-command.

1 件のコメント

Parikshit
Parikshit 2014 年 2 月 25 日
Thanks a lot Kim...it worked....thanks again

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2014 年 2 月 25 日

コメント済み:

2014 年 2 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by