7.3: Solutions to Exercises
Solution to Exercise 1
'\begin{align*}a\end{align*} is not equal to '
Solution to Exercise 2
'\begin{align*}a\end{align*} is positive'
Solution to Exercise 3
b * c
gives
a
value of \begin{align*}6\end{align*}, and \begin{align*}5 < 6\end{align*}, so this code will print 'Hello world'.
Solution to Exercise 4
The parentheses around the relational expression
a > 0
will not change its validity, so this code will print 'a is positive'.
Solution to Exercise 5
The parentheses in this expression change its meaning completely. First,
a < b
is evaluated, and since it is false for the given values of
a
and
b
,
it evaluates to zero. The zero is then multiplied by
c
,
giving
a
value of zero which is interpreted as false. So this code prints 'Goodbye world'.
Solution to Exercise 6
'\begin{align*}p1\end{align*} and \begin{align*}p2\end{align*} are not equal'
Solution to Exercise 7
This code will generate an error message, since
a = b
assigns the value of
b
to
a
.
To check if
a
and
b
are equal, use
a == b
.
Solution to Exercise 8
Any value that is not zero.
Solution to Exercise 9
Any value greater than or equal to and less than \begin{align*}7\end{align*}.
Solution to Exercise 10
Any value less than \begin{align*}3\end{align*} or greater than \begin{align*}10\end{align*}.
Solution to Exercise 11
Every value of
a
will print 'Hello world'.
Solution to Exercise 12
The following code will provide the desired behavior:


if n >= 0.01 && n <= 0.01

disp('a is very close to zero')


end

Solution to Exercise 13


if n >= 1 && n <= 4

CA = 16.00;


elseif n >= 5 && n <= 24

CA = 14.00;


elseif n >= 25 && n <= 99

CA = 12.70;


else

CA = 11.00;


end

Solution to Exercise 14
This code by BrieAnne Davis:


if n >= 1 && n <= 4; %if n=1 to 4, CA is $16.00

CA=16.00;


elseif n >= 5 && n <= 24; %if n=5 to 24, CA is $14.00

CA=14.00;


elseif n >= 25 && n <= 99; %if n=25 to 99, CA is $12.70

CA=12.70;


elseif n >= 100; %if n=100 or more, CA is $11.00

CA=11.00;


end %this ends the if statement for CA



if n >= 1 && n <= 9; %if n=1 to 9, CB is $24.64

CB=24.64;


elseif n >= 10 && n <= 49; %if n=10 to 49, CB is $24.32

CB=24.32;


elseif n >= 50 && n <= 99; %if n=50 to 99, CB is $24.07

CB=24.07;


elseif n >= 100; %if n=100 or more, CB is $23.33

CB=23.33;


end %this ends the if statement for CB



if n >= 1 && n <= 24; %if n=1 to 24, CC is $17.98

CC=17.98;


elseif n >= 25 && n <= 49; %if n=25 to 49, CC is $16.78

CC=16.78;


elseif n >= 50; %if n=50 or more, CC is $15.78

CC=15.78;


end %this ends the if statement for CC



if n >= 1 && n <= 9; %if n=1 to 9, CD is $12.50

CD=12.50;


elseif n >= 10 && n <= 99; %if n=10 to 99, CD is $10.42

CD=10.42;


elseif n >= 100; %if n=100 or more, CD is $9.62

CD=9.62;


end %this ends the if statement



CO=45.00;

Cunit = CO + CA + CB + CC + CD;

Solution to Exercise 15
This code was originally written by Bryson Hinton and then modified:


cunit = zeros(1,150);

c0 = 45;

for n=1:150

%compute price for part A

if n >= 1 && n <= 4

ca=16;


elseif n >= 5 && n <= 24

ca=14;


elseif n >= 25 && n <= 99

ca=12.7;


else

ca=11;


end

%compute price for part B

if n >= 1 && n <= 9

cb=24.64;


elseif n >= 10 && n <= 49

cb=24.32;


elseif n >= 50 && n <= 99

cb=24.07;


else

cb=23.33;


end





%compute price for part C

if n >= 1 && n <= 24

cc=17.98;


elseif n >= 25 && n <= 49

cc=16.78;


else

cc=15.78;


end





%compute price for part D

if n >= 1 && n <= 9

cd=12.50;


elseif n >= 10 && n <= 99

cd=10.42;


else

cd=9.62;


end





%sum cost for all parts

cunit(n)= c0 + ca + cb + cc + cd;


end



% Plot cost as a function of n

plot(1:150,cunit);

xlabel('n (units)');

ylabel('cost (dollars)');

title('Cost/unit as a function of number of units');

This code produces the plot in Figure 1
Cost as a function of number of units produced.
Notes/Highlights Having trouble? Report an issue.
Color  Highlighted Text  Notes  

Show More 