Increase a variable by 0.1 each iteration

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 件のコメント

Jan
Jan 2019 年 3 月 4 日
編集済み: Jan 2019 年 3 月 4 日
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 2019 年 3 月 4 日
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

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

 採用された回答

Star Strider
Star Strider 2019 年 3 月 4 日

0 投票

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 件のコメント

Alice Cozens
Alice Cozens 2019 年 3 月 4 日
Thank you sooo much, this is perfect.
Could you just clarify what the purpose of the varible i is now?
i=0.01:0.1:5;
parmtrs = zeros(numel(i), 3); % Preallocate
for k1 = 1:numel(i)
So if i wanted Dd to run from 0.01 to 5 in 0.01 increments?
Basically I don't understand what the three numbers for i are
Star Strider
Star Strider 2019 年 3 月 4 日
As always, my pleasure.
Could you just clarify what the purpose of the varible i is now?
I kept ‘i’ because yoiu originally wanted to use it as a loop counter. It is now simply a vector, and it couold also be used as the independent variable if you wanted to plot ‘parmtrs’ as a function of it. It has no other purpose at present.
So if i wanted Dd to run from 0.01 to 5 in 0.01 increments?’i = 0.0
There are several options, the easiest being to specify ‘i’ as:
i = 0.01 : 0.01 : 5;
and changing ‘Dd’ to either:
Dd=Dd+0.01;
or:
Dd = i(k1);
Basically I don't understand what the three numbers for i are
The definition for the ‘i’ vector are: start_value : step_value : finish_value. See the documentation on colon,: (link) for details.
Alice Cozens
Alice Cozens 2019 年 3 月 5 日
Thank you :) It works perfectly!!!
Star Strider
Star Strider 2019 年 3 月 5 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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