Increase a variable by 0.1 each iteration

조회 수: 11 (최근 30일)
Alice Cozens
Alice Cozens 2019년 3월 4일
댓글: Star Strider 2019년 3월 5일
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일
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월 5일
Thank you :) It works perfectly!!!
Star Strider
Star Strider 2019년 3월 5일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by