Increase a variable by 0.1 each iteration

13 views (last 30 days)
I have created code to find Scrit and Dmax for a series of Dd values, but each iteration seems to increase by a random amount - I need it to increase by 0.1 each time? I believe I need to edit the line i=0.01:0.1:1?
I would also like to be able to save each iteration table in a new excel row, but at the moment the code just overwrites into the first row of my excel spreadsheet?
T=table(Dd, Scrit, Dmax)
filename='Dry diameter.xls';
writetable(T,filename)
- do I need to edit this to new column each run, if so how?
Here is the code:
% Using dry diameter in um
K=0.5;
sigma=0.072;
T=298.15;
Mw=18.01528;
Pw=0.997;
R=8.3143;
x=(4*sigma*Mw)/((R*T*Pw));
Dd=0.1
for i=0.01:0.1:1
Dd=Dd+i
D=linspace(Dd,2,10);
S=((D.^3-Dd^3)./(D.^3-Dd^3+Dd^3*K)).*exp(x./D)
Scrit=max(S)
Dmax=D(find(S==Scrit))
T=table(Dd, Scrit, Dmax)
filename='Dry diameter.xls';
writetable(T,filename)
end
  2 Comments
Jan
Jan on 4 Mar 2019
Edited: Jan on 4 Mar 2019
I have formatted your code toda to make it readable. Please do this by your own in future questions. Thanks.
"but each iteration seems to increase by a random amount" - why do you assume this? What do you observe?
" I believe I need to edit the line i=0.01:0.1:1" - why?
Alice Cozens
Alice Cozens on 4 Mar 2019
each iteration it increases by
0.01
0.11
0.21
0.31
etc.
but I want it just to increase by 0.1 each time

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 4 Mar 2019
You appear to be increasing ‘Dd’ in each iteration. It is increasing by adding whatever value of ‘i’ exists in each iteration to the previous value, so it only appears to be random. If you want ‘Dd’ to increase by 0.1 each time, try this:
Dd=Dd+0.1;
With respect to writing the ‘T’ table, a more efficient approach to your entire code would likely be something like this:
% Using dry diameter in um
K=0.5;
sigma=0.072;
T=298.15;
Mw=18.01528;
Pw=0.997;
R=8.3143;
x=(4*sigma*Mw)/((R*T*Pw));
Dd=0.1
i=0.01:0.1:1;
parmtrs = zeros(numel(i), 3); % Preallocate
for k1 = 1:numel(i)
Dd=Dd+0.1
D=linspace(Dd,2,10);
S=((D.^3-Dd^3)./(D.^3-Dd^3+Dd^3*K)).*exp(x./D)
Scrit=max(S)
Dmax=D(find(S==Scrit))
parmtrs(k1,:) = [Dd, Scrit, Dmax];
end
T=table(parmtrs(:,1), parmtrs(:,2),parmtrs(:,3), 'VariableNames',{'Dd', 'Scrit', 'Dmax'})
filename='Dry diameter.xls';
writetable(T,filename)
with ‘T’ now being:
T =
10×3 table
Dd Scrit Dmax
___ _______ ____
0.2 1.0005 2
0.3 0.99936 2
0.4 0.99703 2
0.5 0.99317 2
0.6 0.98735 2
0.7 0.97912 2
0.8 0.96796 2
0.9 0.95326 2
1 0.93431 2
1.1 0.91022 2
Experiment to get the result you want.
  4 Comments
Alice Cozens
Alice Cozens on 5 Mar 2019
Thank you :) It works perfectly!!!

Sign in to comment.

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!