Water Filtration Problem for a class
Afficher commentaires plus anciens
I need to find the minimum number of stages that are needed to remove 99% of the initial impurity level of water.
I have a FOR loop set up to due the filtration like so:
for n = 1:21
n_stage(n) = n-1:
y(n) = (1/3)^(n-1);
if (y(n) <= 0.01) Here's where my issues start.
n_1p(n) = n_stage(n);
y_1p(n) = y(n);
end
end
fprintf('Minimum number of stage for 99%% filtration = %d, impurity level = %f\n',n_1p,y_1p);
#1: I'm getting two many answers because I'm not sure how to get the IF statement to stop.
#2: When it prints, it doesn't print the filtration stage (n_1p) nor the impurity level (y_1p) as would expect.
My programming skills are kinda lackluster. This is for a class I'm taking to help with said skills. Any help would be greatly appreciated.
6 commentaires
Troy Brown
le 18 Juil 2019
Walter Roberson
le 18 Juil 2019
You should use
break
to exit a loop early.
Guillaume
le 18 Juil 2019
or use a while loop.
Joel Handy
le 19 Juil 2019
Modifié(e) : Joel Handy
le 19 Juil 2019
In regards to your second question, what doesnt print as expected? Is it more than the fact that you have multiple n_1p and y_1 values?
In regards to the first, since this is homework specifically to work on improving coding skills, I dont want to just give you the answer (and I won't mention that you can do this without a loop at all in 3 lines of code). However, if computing all values is actually important, I will point out two things.
1) there is no reason your loop can't count down, i.e. for n = 21:1
2) You don't need to save more than one n_1p or y_1 (i.e. you don't need to index into them). You just want the values associated with the smallest value of n that meets conditions. So why not just overwrite them each time you find a better answer?
If you just need to find the desired values of n_1p and y_1 and dont care about computing all the other values, then yeah just use a break statement or a while loop instead of a for loop as suggested.
Walter Roberson
le 19 Juil 2019
Note: the syntax for counting down would be 21:-1:1
Troy Brown
le 19 Juil 2019
Modifié(e) : Troy Brown
le 19 Juil 2019
Réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!