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??

 Réponse acceptée

Mischa Kim
Mischa Kim le 25 Fév 2014
Modifié(e) : Mischa Kim le 25 Fév 2014
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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by