Help using While loop

1 回表示 (過去 30 日間)
Charles Duggan
Charles Duggan 2019 年 5 月 1 日
コメント済み: Charles Duggan 2019 年 5 月 5 日
Hello. As an extra credit for my MatLab class, I must make a program that uses both For loop and While loop to take user input into a function, and return with a final value (see attached code). I can make it run logically without a while loop. Im looking for an idea of where I should put the While loop and what it should be looking for.
clc
clear
Na = input('Enter Sodium Oxide Content (%)');
Ca = input('Enter Calcium Oxide Content (%)');
Si = input('Enter Silicon Oxide Content (%)');
Batch = [Na,Ca,Si];
p = input('Batch weight in grams');
MM = [61.977;56.077;60.083]; %Provided MolarMass Values
TG = [1.710;1.780;1.000]; %Provided Thermo Values
%--------------------------------------------------------------------
if sum(Batch) <100 %Ensures It is 100%
disp('Error - Total Content is Not 100%')
disp(sum(Batch))
elseif sum(Batch)>100 %Ensures it is 100%
disp('Error - Total Content is Not 100%')
disp(sum(Batch))
end
if p <= 0 %Ensures the Batch isnt a negative weight
disp('Batch Weight is Invalid - Check for negative value')
end
if sum(Batch) == 100 && p > 0 %If 100% and Pos. Batch value then it does the calculations
W = Na*MM(1,1,1);
Q = Ca*MM(2,1,1);
E = Si*MM(3,1,1);
NewB = [W,Q,E];
FWB1 = (W/sum(NewB))*p*TG(1,1,1);
FWB2 = (Q/sum(NewB))*p*TG(2,1,1);
FWB3 = (E/sum(NewB))*p*TG(3,1,1);
R = [FWB1,FWB2,FWB3];
Y = sum(R);
end
disp(FWB1)
disp(FWB2)
disp(FWB3)
disp('Total Batch Weight (g):')
disp(Y)
Please let me know any ideas or reccomendations of what to do here
Final note - I tried to make the while loop use the "sum(Batch) == 100 && p > 0" but that doesnt logically make sense nor work in this"*

採用された回答

Geoff Hayes
Geoff Hayes 2019 年 5 月 2 日
Charles - I think that you could use a while or for loop on this code
W = Na*MM(1,1,1);
Q = Ca*MM(2,1,1);
E = Si*MM(3,1,1);
NewB = [W,Q,E];
FWB1 = (W/sum(NewB))*p*TG(1,1,1);
FWB2 = (Q/sum(NewB))*p*TG(2,1,1);
FWB3 = (E/sum(NewB))*p*TG(3,1,1);
Note how you extract the first, second, and third element from MM for three different calculations. This suggests that a loop could be used here especially since you have already put your Na, Ca, and Si into the Batch array. For example,
NewB = zeros(length(MM),1);
for k = 1:3
NewB(k) = Batch(k) * MM(k);
end
Then do something similar for the FW calculations.
  1 件のコメント
Charles Duggan
Charles Duggan 2019 年 5 月 5 日
clc
clear
Na = input('Enter Sodium Oxide Content (%): ');
Ca = input('Enter Calcium Oxide Content (%): ');
Si = input('Enter Silicon Oxide Content (%): ');
Batch = [Na,Ca,Si];
p = input('Batch weight in grams: ');
MM = [61.977;56.077;60.083]; %Provided MolarMass Values
TG = [1.710;1.780;1.000]; %Provided Thermo Values
k = [1,2,3];
%--------------------------------------------------------------------
if sum(Batch) <100 %Ensures It is 100%
disp('Error - Total Content is Not 100%')
disp(sum(Batch))
elseif sum(Batch)>100 %Ensures it is 100%
disp('Error - Total Content is Not 100%')
disp(sum(Batch))
end
if p <= 0 %Ensures the Batch isnt a negative weight
disp('Batch Weight is Invalid - Check for negative value')
end
if sum(Batch) == 100 && p > 0 %If 100% and Pos. Batch value then it does the calculations
NewB = zeros(length(MM),1);
k = 3;
while k > 0
NewB(k) = Batch(k) * MM(k);
k = k-1;
end
FWB = zeros(length(NewB),1);
l = 3;
while l > 0
FWB(l) = NewB(l)/sum(NewB)*p*TG(l);
l = l-1;
end
end
disp('Sodium Oxide Weight(g)')
disp(FWB(1))
disp('Calcium Oxide Weight(g)')
disp(FWB(2))
disp('Silicon Oxide Weight(g)')
disp(FWB(3))
disp('Total Batch Weight (g):')
disp(sum(FWB))
In case anyone was wondering how this looked in the very end. It needs refinement to make it look a litle nicer but its running correctly.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by