How to save iterations

1 view (last 30 days)
Birender
Birender on 4 Apr 2013
for i=0.01:0.1:1
x=0:i:5; % create Vector of x 0 -> L
N=length(x); % Calculate number of psi values (X)
n=1:N; % Create Vector for n with length of N
Vx=zeros([1 N]); % Create Vector of V(x)
eval(['n(' num2str(i) '=n;']);
% Calculate Energy & Potential
[H,U,E] = CalcUandE(N,x,Vx,hbar,m);
eval(['Hdx' num2str(i) '=H;']);
eval(['Udx' num2str(i) '=U;']);
eval(['Edx' num2str(i) '=E;']);
% Find Analytic for E
[Ea] = AnalyticE(hbar,n,m,L);
eval(['Eadx' num2str(i) '=Ea;']);
Hi I am trying to run my above code and using it store the different values of n, Hdx, Udx, Edx however I am running into an error as my vector i for the loop has decimals. I am hoping some one can assist. Thanking you in advance :)

Accepted Answer

Image Analyst
Image Analyst on 4 Apr 2013
Edited: Image Analyst on 4 Apr 2013
Use an index counter
counter = 1;
for step = 0.01 : 0.1 : 1
% Loop code
Hdx(counter) = % whatever.
counter = counter + 1;
end
Don't use i as a loop index - it's also the imaginary variable. That's why I used step as the name of the loop index. Once you use an index for Hdx you won't need to use eval. See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!