Examples
Add comments to the sequence of commands that you wrote to calculate the BMI.
wt_lbs = 205; % This is the subject's weight in pounds
Be descriptive but brief with your comments, and be consistent in placement when writing comments. Note that the variable names give you a clue as to what they are. Make sure you also comment on the purpose of the program - what it will do. Hijacked from the CS 302 homepage, here are some more guidelines for where and what to comment in your code:
- highlight the major steps of your algorithm
- explain long calculations or conditions
- clarify convoluted or unusual code
- mark locations where you suspect a bug may exist
- mark locations where improvements or enhancements are planned
The conversion from English to Metric looks like this:
The first equation converts the numerical value of the inches, I, to the numerical value of meters, M. The second converts L lbs to K kilograms. The next few lines of your script should convert the height and weight values into the needed units. By now you should be able to write those lines of code to calculate the BMI and store the resultant value in a variable.
Finally, add the line to compute the BMI of the person. The BMI Formula is: weight (kg) / [height (m)]2
bmi = wt_kg / ht_m^2
wt_lbs = 205 ht_ft = 6 ht_in = 4 tot_ht_in = ht_in + ht_ft*12 ; % total height in inches ht_m = 0.0254 * tot_ht_in ; % total height in meters wt_kg = wt_lbs / 2.2 ; % weight in kilograms bmi = wt_kg / ht_m.^2 ; % bmi of person disp('bmi is ',num2str(bmi)) % display the results