Need help on a problem.
Mostra commenti meno recenti
In chemistry, the pH of an aqueous solution is a measure of its acidity. A solution with a pH of 7 is said to be neutral, a solution with a pH greater than 7 is basic, and a solution with a pH less than 7 is acidic.
A structure variable is created to store the PH value of a solution. This structure variable has two fields: one is called ‘SolutionName’, and the other is ‘PH’. An example of such a variable is created as:
sol1 = struct('SolutionName', 'water', 'PH', 7);
Write a function that will take in one such a variable as the input argument, determine its acidity based on the pH value, add a new field ‘acidity’ to the structural variable with the determined value (neutral, basic, or acidic ), and return this new structure as the output variable.
For example, if the above sol1 variable is used as the input, the output would be produced as:
>>sol1 = struct('SolutionName', 'water', 'PH', 7);
>>OutputVar = addAcidity(sol1)
>>OutputVar =
SolutionName: 'water'
PH: 7
Acidity: 'neutral'
At the moment, I am trying to write a "sample" for my function that I will be utilizing, but I am stuck.
So far, this is what I have:
pH = input('Enter pH: ','s') ;
for val = pH ;
if val < 7
fprintf('pH = %f,solution is acidic\n',val) ;
elseif val ==7
fprintf('pH = %f,solution is neutral\n',val) ;
elseif val > 7
fprintf('pH = %f,solution is basic\n',val) ;
end
end
When I run this script, it gives me a number (ex. "Enter pH: 7", I will get "pH=55.0, solution is basic"), but I have no idea where the 55 is coming from.
I would greatly appreciate any help I am able to get on this!
Risposta accettata
Più risposte (1)
dpb
il 18 Ott 2022
Spostato: Image Analyst
il 18 Ott 2022
HINT:
double('7')
See what
class(val)
returns after the input() call; that should make it even more clear.
PS. Nicely written code for student -- well done!!!
PPS: You might also think of switch, case, otherwise here instead of the multiple if...else if ... construct.
Categorie
Scopri di più su Chemistry in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!