1. Write a program that calculates and comments on the body mass

advertisement
1. Write a program that calculates and comments on the body mass index (BMI) of the
user. The program should ask the user his/her weight and height, and calculate BMI
using the formula:
𝐵𝑀𝐼, 𝑘𝑔/𝑚2 =
𝑚𝑎𝑠𝑠
ℎ𝑒𝑖𝑔ℎ𝑡 2
The comments should be made based on the following conditions:
BMI < 23  the user has healthy weight.
23 < BMI < 29  The user is overweight.
29 < BMI  The user is obese.
%This program finds BMI index
mass=input('Enter weight');
height=input('Enter height');
BMI = mass/height^2;
if (BMI <= 23)
disp('healty weight');
elseif (BMI > 23 && BMI < 29)
disp('overweight');
elseif ( BMI >= 29)
disp('obese');
end
2. What will be the output of the following MATLAB program?
a=
3
n=
2
a=
8
n=
3
3. Write a MATLAB program that asks the user to enter a number from 1 to 3 and then
displays “The number is 1!” if the user enters 1, “The number is 2!” if the user enters 2,
and “The number is 3!” if the user enters 3. Use the switch construct.
%This program prints the entered number
num=input('enter 1,2 or 3');
switch num
case 1
disp('The number is 1')
case 2
disp('The number is 2')
case 3
disp('The number is 3')
otherwise
disp( 'illegal enty' )
end
4. Complete the following program that reads in two integers from user and prints
either 1, 2 or 3 according to the following rules.
1 if only one of them equals to zero
2 if both of them equals to zero
3 if none of them equals to zero.
num1 = input('enter
num2 = input('enter
if xor(num1, num2)
disp('1')
elseif(num1 == 0 &&
disp('2')
elseif(num1 != 0 &&
disp('3')
end
first number');
second number');
num2 ==0)
num2 !=0)
5. Write a script that calculates the sum of first 30 terms of the following sequence.
1 4 7 10 13 16 19 22 25 ....
%this program calculates series sum
sum=0;
for i=1:3:90
sum = sum +i;
end
disp(sum)
6. Consider the following MATLAB function file:
what the output of that M-file would be?
________________
% easy.m , 4/19/2013
for x=1:5;
fprintf('o\n');
end;
7.
function [A,B]=prob(x,y,z)
C = -1*x;
A = 5;
B = 5;
while C<0
if (y<x)&(-z<=x)
A=A+3*abs(x+y);
C=C+A
elseif (x<-y)|(z<10*x)
B=B+3*abs(x-y);
C=C+B
else
C=C+A+B
end
end
________________________
>> [A,B]=prob(2,1,4), [A,B]= prob(1,5,4)
8.
In the file slope.m,
•fill in the missing first line of the file. Read the comment and the body carefully,
so that you do this exactly right!
•one missing line of code in the body of the function
___________________________________________________________________________
………………………
% <== fill in
%slope return slope of line passing through two points
%consumes: x1, y1, x2, y2: scalar numbers
%produces: Slope, a scalar number
%Examples:
% >> slope(2,2,4,4)
% ans = 1
% >> slope(3,1,5,5)
% ans = 2
% >> slope(4,1,4,5)
% Error: slope is undefined
% >>
denom = x1 - x2;
num = y1 - y2;
if (
)
% <== fill in this line of code
error('slope is undefined');
else
theslope = num/denom;
return;
end
function output = slope(x1,x2,y1,y2)
denom = x1 - x2;
num = y1 - y2;
if ( denom ==0 )
% <== fill in this line of code
error('slope is undefined');
else
theslope = num/denom;
return;
end
9. Create the arrays a and b:
𝑎 = [7
3 −1 0
20], 𝑏 = [1
5 −4 9 20]
(3 pts)
and write the Matlab statements to find (4x2=8 pts):
a) the values of a that are in the range of 5 < 𝑎 ≤ 10.
b) the values of b that are either less than 1 (𝑏 < 1) or greater than or equal to 5
(𝑏 ≥ 5).
c) the indices of the values of a that are equal to the corresponding values of b.
d) the indices of the values of b that are greater than 6 (𝑏 > 6).
10. Create the arrays m and n:
𝑚 = [−3 0 0
and find (3x2=6 pts):
a) z = n < ~ m
b) z = m & n
c) z = m|n
2 5 8], 𝑛 = [−5 −2 0 3
4 10] (4 pts)
Download