The Body Mass Index, or BMI
Show older comments
Hi I tried to write a function called findbmi that would receive the weight and height as input arguments, and would return the BMI but for some reason it's not working. Please help
this is my code
Weight= input('Enter your weight in lbs, or typer 0 for SI units:')
if Weight==0;
Weight=input('Enter your weight in kg:')
Height=input('Enter your height in m:')
function BMI=findbmi(Weight,Height)
BMI= (Weight/(Height^2)) %kg/m^2
else
Height=input('Enter your height in ft:')
function BMI=findbmi(Weight,Height)
BMI=(Weight/(Height^2))*4.882427111
end
Answers (1)
James Tursa
on 9 Sep 2020
Just get rid of these lines:
function BMI=findbmi(Weight,Height)
8 Comments
Dai Nguyen
on 9 Sep 2020
Dai Nguyen
on 9 Sep 2020
Dai Nguyen
on 9 Sep 2020
Edited: Dai Nguyen
on 9 Sep 2020
Rik
on 9 Sep 2020
Was this the only code in your file? You should try to avoid mixing script files and functions.
James Tursa
on 9 Sep 2020
@Dai: Create two separate files.
% findbmi_script.m
Weight= input('Enter your weight in lbs, or typer 0 for SI units:')
if Weight==0
Weight=input('Enter your weight in kg:');
Height=input('Enter your height in m:');
BMI=findbmi(Weight,Height);
else
Height=input('Enter your height in ft:');
BMI=findbmi(Weight,Height)*4.882427111;
end
And a separate file with the BMI calculation:
% findbmi.m
function bmi=findbmi(weight,height)
bmi= (weight/(height^2));
Then run your script from the command line:
findbmi_script
Dai Nguyen
on 9 Sep 2020
Dai Nguyen
on 9 Sep 2020
Dai Nguyen
on 9 Sep 2020
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!