170k views
0 votes
Modify the written script file to a single function file. Instead of entering the weight and height from the keyboard, the function should accept weight and height as two input arguments. Capture the calculated BMI to a new variable that can be saved in the workspace.

(1)check for error if the any value passed to input arguments is zero or negative and check for warning if any value passed to input arguments is not reasonable.
(2)Call the function at the command line three times to:
a.Display the BMI calculated from normal weight and height values.
b.Display the error message by passing zero or negative values and immediately terminate the function.
c.Display the calculated abnormal BMI and warning message by passing one (or more) nonreasonable value(s) to the function.

1 Answer

1 vote

Final answer:

To modify a script file into a function file for BMI calculation, create a function that accepts weight and height as arguments, checks for errors and warnings, and applies the BMI formula. The function can be called with different values to produce a normal BMI, display an error message, or show an abnormal BMI with a warning.

Step-by-step explanation:

Modifying Script File to a Function for BMI Calculation

To modify a script file to a function file that calculates body mass index (BMI), you need to create a function that accepts weight and height as input arguments. Here is an example of how such a function may look in MATLAB:

function bmi = calculateBMI(weight, height)
if weight <= 0 || height <= 0
error('Weight and height must be positive numbers.');
elseif weight > 500 || height > 108
warning('The values entered are not within a reasonable range.');
end
bmi = (weight * 703) / (height^2);
end

This function checks for errors with zero or negative values and warns if the values are not reasonable, then calculates the BMI according to the provided formula.

  • Call the function with normal values: bmiNormal = calculateBMI(150, 68);
  • Display an error: calculateBMI(0, 68);
  • Display warning and abnormal BMI: bmiAbnormal = calculateBMI(600, 68);

It is important to make sure that the values passed for weight and height are within a range that is considered physiologically plausible to avoid miscalculations and misinterpretations of the BMI.

User Lynchie
by
8.1k points