HOW DO I DO THAT IN MATLAB SERIES?
In this series, I am answering questions that students have asked me about MATLAB.
Contents
TOPIC
How do I write program that calculates the BMI of a person and tells him if he is healthy or not and also recommends his healthy weight
SUMMARY
Language : Matlab 2010a; Authors : Autar Kaw and Sri Harsha Garapati; Mfile available at Last Revised : January 23, 2012; Abstract: This program shows you how to tell a person if he is healthy or not basing on his BMI found using his weight and height and also gives the target recommended healthy weight to a person
clc
clear all
INTRODUCTION
disp('ABSTRACT') disp(' This program shows you how to tell a person if he is healthy or') disp(' not basing on his BMI found using his weight and height and ') disp(' also gives the target recommended healthy weight to a person') disp(' ') disp('AUTHOR') disp(' Autar Kaw and Sri Harsha Garapati of http://autarkaw.wordpress.com') disp(' ') disp('MFILE SOURCE') disp(' http://numericalmethods.eng.usf.edu/blog/bmi_program_example4_ch19_blog.m') disp(' ') disp('LAST REVISED') disp(' February 13, 2012') disp(' ')
ABSTRACT This program shows you how to tell a person if he is healthy or not basing on his BMI found using his weight and height and also gives the target recommended healthy weight to a person AUTHOR Autar Kaw and Sri Harsha Garapati of http://autarkaw.wordpress.com MFILE SOURCE http://numericalmethods.eng.usf.edu/blog/bmi_program_example4_ch19_blog.m LAST REVISED February 13, 2012
INPUTS
% Weight of the person in pounds w =180; % Height of the person in inches h =72;
DISPLAYING INPUTS
disp(' ') disp('INPUTS') % Printing the input weight fprintf('The weight of the person is %g lbs\n',w) % Printing the input height fprintf('The weight of the person is %g inches \n',h)
INPUTS The weight of the person is 180 lbs The weight of the person is 72 inches
CODE and DISPLAYING OUTPUT
% calculating BMI from the formula bmi=(w/h^2)*703; % rounding BMI value to the nearest integer bmi=round(bmi); disp(' ') disp('OUTPUTS') fprintf(' Your BMI is %g\n',bmi) % Part a) disp(' ') disp('Part a)') % Using if-else-end statement to check if the person if helathy or not % if BMI is greater than 25 or BMI or less than 19 person is not-healthy if bmi>25 | bmi<19 disp(' This is an unhealthy BMI') else % if he is not not-healthy then he is healthy disp(' This is a healthy BMI') end % Part b) disp(' ') disp('Part b)') % Using if-end statement to check if BMI is less than 19 if bmi<19 flag=0; end % Using if-end statement to check if BMI is between 19 and 25 if bmi<=25 & bmi>=19 flag=1; end % Using if-end statement to check if BMI is between 25 and 30 if bmi>25 & bmi<=30 flag=2; end % Using if-end statement to check if BMI is greater than 30 if bmi>30 flag=3; end % Printing the value of flag variable fprintf(' The value of flag variable is %g\n',flag) % Part c) disp(' ') disp('Part c)') % Using the value of flag to determine the category and calculate the % target weight of the person if flag==0 disp(' underweight') tw=ceil((19*h^2)/703); fprintf(' Your target weight is %g\n',tw) end if flag==1 disp(' healthy') disp(' maintain your weight') end if flag==2 disp(' overweight') tw=floor((25*h^2)/703); fprintf(' Your target weight is %g\n',tw) end if flag==3 disp(' obese') tw=floor((25*h^2)/703); fprintf(' Your target weight is %g\n',tw) end
OUTPUTS Your BMI is 24 Part a) This is a healthy BMI Part b) The value of flag variable is 1 Part c) healthy maintain your weight