0% found this document useful (0 votes)
61 views48 pages

Solved Problems: Problem 1

This document contains 15 solved problems involving the use of MATLAB for numerical computations and simulations. The problems cover topics such as: 1) Evaluating logical expressions and relational operators in MATLAB's command window. 2) Writing script files to perform calculations, output results, and define and manipulate arrays/matrices. 3) Using loops (for and while) to iterate through calculations, arrays, and define sequences. 4) Applying mathematical functions like square root, factorial, mean, and determinant. 5) Solving equations like finding roots of quadratic equations.

Uploaded by

Iwaiz Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views48 pages

Solved Problems: Problem 1

This document contains 15 solved problems involving the use of MATLAB for numerical computations and simulations. The problems cover topics such as: 1) Evaluating logical expressions and relational operators in MATLAB's command window. 2) Writing script files to perform calculations, output results, and define and manipulate arrays/matrices. 3) Using loops (for and while) to iterate through calculations, arrays, and define sequences. 4) Applying mathematical functions like square root, factorial, mean, and determinant. 5) Solving equations like finding roots of quadratic equations.

Uploaded by

Iwaiz Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Chapter 6

Solved Problems

Problem 1
Command Window:
1.a
>> 5+3>32/4
ans =
0
>> y=2*3>10/5+1>2^2
y =
0
>> y=2*(3>10/5)+(1>2)^2
y =
2
>> 5*3-4*4<=~2*4-2+~0
ans =
1

1
2 Chapter 6: Solved Problems

Problem 2
Script file:
a=6; b=2; c=-5;
disp('Part (a)')
y=a+b>a-b<c
disp('Part (b)')
y=-6<c<-2
disp('Part (c)')
y=b+c>=c>a/b
disp('Part (d)')
y=a+c==~(c+a~=a/b-b)

Command Window:

Part (a)
y =
0
Part (b)
y =
0
Part (c)
y =
0
Part (d)
y =
1
Chapter 6: Solved Problems 3

Problem 3
Script file:
v=[4 -2 -1 5 0 1 -3 8 2];
u=[0 2 1 -1 0 -2 4 3 2];
disp('Part (a)')
~(~v)
disp('Part (b)')
u==v
disp('Part (c)')
u-v<u
disp('Part (d)')
u-(v<u)

Command Window:

Part (a)
ans =
1 1 1 1 0 1 1 1 1
Part (b)
ans =
0 0 0 0 1 0 0 0 1
Part (c)
ans =
1 0 0 1 0 1 0 1 1
Part (d)
ans =
0 1 0 -1 0 -2 3 3 2
4 Chapter 6: Solved Problems

Problem 4
Command Window:
>> v=[4 -2 -1 5 0 1 -3 8 2];
>> w=[0 2 1 -1 0 -2 4 3 2];
>> y=w(w<v)
y =
0 -1 -2 3

Problem 5
Command Window:

>> 0&21
ans =
0
>> ~-2>-1&11>=~0
ans =
1
>> 4-7/2&6<5|-3
ans =
1
>> 3|-1&~2*-3|0
ans =
1
Chapter 6: Solved Problems 5

Problem 6
Script File:
TCH=[75 79 86 86 79 81 73 89 91 86 81 82 86 88 89 90 82 84 81
79 73 69 73 79 82 72 66 71 69 66 66];
TSF=[69 68 70 73 72 71 69 76 85 87 74 84 76 68 79 75 68 68 73
72 79 68 68 69 71 70 89 95 90 66 69];
%part a
disp('Part (a)')
TCHave=mean(TCH)
TSFave=mean(TSF)
% part b
disp('Part (b)')
TCHaboveAVE=sum(TCH>TCHave)
TSFaboveAVE=sum(TSF>TSFave)
% part c
disp('Part (c)')
TSFlowerTCH=sum(TSF<TCH)
dayTSFlowerTCH=find(TSF<TCH)
% part d
disp('Part (d)')
TSFsameTCH=sum(TSF==TCH)
dayTSFsameTCH=find(TSF==TCH)

Command Window:
Part (a)
TCHave =
79.1290
TSFave =
74.5484
Part (b)
TCHaboveAVE =
16
TSFaboveAVE =
11
Part (c)
TSFlowerTCH =
23
dayTSFlowerTCH =
6 Chapter 6: Solved Problems

1 2 3 4 5 6 7 8 9 11
13 14 15 16 17 18 19 20 22 23 24
25 26
Part (d)
TSFsameTCH =
1
dayTSFsameTCH =
30

Problem 7
Script File:

A(1)=0; A(2)=1;
n=20;
for i=3:n
A(i)=A(i-2)+A(i-1);

end
A

Command Window:

A =
Columns 1 through 7
0 1 1 2 3
5 8
Columns 8 through 14
13 21 34 55
89 144 233
Columns 15 through 20
377 610 987 1597
2584 4181
Chapter 6: Solved Problems 7

Problem 8
Script File:

for i=1:4
for j=1:3
A(i,j)=(i+j)/j^2;
end
end
A

Command Window:

A =
2.0000 0.7500 0.4444
3.0000 1.0000 0.5556
4.0000 1.2500 0.6667
5.0000 1.5000 0.7778
8 Chapter 6: Solved Problems

Problem 9
Script File:
n=4;
for i=1:n
for j=1:n
A(i,j)=factorial(i+j-2)/(factorial(i-1)*factorial(j-
1));
end
end
A

Command Window:

A =
1 1 1 1
1 2 3 4
1 3 6 10
1 4 10 20

Executing the script file with n=7:

A =
1 1 1 1 1 1 1
1 2 3 4 5 6 7
1 3 6 10 15 21 28
1 4 10 20 35 56 84
1 5 15 35 70 126 210
1 6 21 56 126 252 462
1 7 28 84 210 462 924
Chapter 6: Solved Problems 9

Problem 10
Script File:

A(1,1)=2; A(1,2)=3;
n=4;
for i=1:n
if i==1
for j=3:n
A(i,j)=A(i,j-2)+A(i,j-1);
end
else
A(i,1)=A(i-1,n-1)+A(i-1,n);
A(i,2)=A(i-1,n)+A(i,1);
for j=3:n
A(i,j)=A(i,j-2)+A(i,j-1);
end
end
end
A

Command Window:

Executing the program and calculating the determinant of A:

A =
2 3 5 8
13 21 34 55
89 144 233 377
610 987 1597 2584
>> det(A)
ans =
-9.7622e-030

Executing the program with begining numbers 1 and 4, and calculating the deter-
minant of A:
10 Chapter 6: Solved Problems

A =
1 4 5 9
14 23 37
60 97 157 254
411 665 1076
1741 2817 4558 7375
11933 19308 31241
50549 81790 1.3234e+005 2.1413e+005 3.4647e+005
5.606e+005 9.0707e+005
1.4677e+006 2.3747e+006 3.8424e+006 6.2171e+006
1.006e+007 1.6277e+007 2.6336e+007
4.2613e+007 6.8949e+007 1.1156e+008 1.8051e+008
2.9207e+008 4.7258e+008 7.6465e+008
1.2372e+009 2.0019e+009 3.2391e+009 5.241e+009
8.4802e+009 1.3721e+010 2.2201e+010
>> det(A)
ans =
0
Chapter 6: Solved Problems 11

Problem 11
Script File:
a=input('Enter the constant a ');
b=input('Enter the constant b ');
c=input('Enter the constant c ');
D=b^2-4*a*c;
if D<0
disp('The equation has no real roots.')
elseif D==0
x=-b/(2*a);
fprintf('The equation has one root: x = %f\n',x)
else
x1=(-b+sqrt(D))/(2*a);
x2=(-b-sqrt(D))/(2*a);
fprintf('The equation has two roots:\n')
fprintf('x1 = %f, x2 = %f\n',x1, x2)
end

Command Window:
11.a
Enter the constant a 2
Enter the constant b 8
Enter the constant c 8
The equation has one root: x = -2.000000

11.b
Enter the constant a -5
Enter the constant b 3
Enter the constant c -4
The equation has no real roots.

11.c
Enter the constant a -2
Enter the constant b 7
Enter the constant c 4
The equation has two roots:
x1 = -0.500000, x2 = 4.000000
12 Chapter 6: Solved Problems

Problem 12
Script File:

i=132^2;
for n=i:2*i
if (rem(n,2)~=0) & (rem(n,11)==0) & (sqrt(n)>132)
break
end
end
fprintf('The required number is: %i\n',n)

Command Window:

The required number is: 17435


Chapter 6: Solved Problems 13

Problem 13
Script File:
m=input('Enter a value (integer) for m ');
S=0;
for n=0:m
S=S+(-1/3)^n/(2*n+1);
end
Result=sqrt(12)*S

Command Window:

Enter a value (integer) for m 5


Result =
3.141308785462883
>>
Enter a value (integer) for m 10
Result =
3.141593304503081
>>
Enter a value (integer) for m 20
Result =
3.141592653595635
14 Chapter 6: Solved Problems

Problem 14
Script File:
m=input('Enter a value for m ');
T=1;
for n=1:m
nsq=(2*n)^2;
T=T*nsq/(nsq-1);
end
Piest=2*T

Command Window:

Enter a value for m 100


Piest =
3.133787490628158
>>
Enter a value for m 100000
Piest =
3.141584799657313
>>
Enter a value for m 10000000
Piest =
3.141592575051505
Chapter 6: Solved Problems 15

Problem 15
Script File:
x=[-3.5 -5 6.2 11 0 8.1 -9 0 3 -1 3 2.5];
n=length(x);
ip=1;
in=1;
for i=1:n
if x(i)>0
P(ip)=x(i);
ip=ip+1;
elseif x(i)<0
N(in)=x(i);
in=in+1;
end
end
P
N

Command Window:

P =
6.2000 11.0000 8.1000 3.0000 3.0000 2.5000
N =
-3.5000 -5.0000 -9.0000 -1.0000
16 Chapter 6: Solved Problems

Problem 16
Script File:
x=[-3.5 5 -6.2 11.1 0 7 -9.5 2 15 -1 3 2.5];
n=length(x);
for i=1:n-1
A=x(i);
for j=i+1:n
if x(j)<A
A=x(j);
x(j)=x(i);
x(i)=A;
end
end
end
x

Command Window:

x =
-9.5000 -6.2000 -3.5000 -1.0000 0 2.0000
2.5000 3.0000 5.0000 7.0000 11.1000 15.0000
Chapter 6: Solved Problems 17

Problem 17
Script File:
Es=[73 91 37 81 63 66 50 90 75 43 88 80 79 69 26 82 89 99 71
59];
n=length(Es);
for i=1:8
A=Es(i);
for j=i+1:n
if Es(j)>A
A=Es(j);
Es(j)=Es(i);
Es(i)=A;
end
end
end
AveTop8Scores=sum(Es(1:8))/8

Command Window:

AveTop8Scores =
87.5000
18 Chapter 6: Solved Problems

Problem 18
Script File:
x=input('Enter a value for an angle in degrees ');
xr=x*pi/180;
y=0;
for n=1:20
yn=(-1)^(n-1)*xr^(2*n-1)/factorial(2*n-1);
yn=y+yn;
if abs((yn-y)/y)<0.000001
y=yn;
break
end
y=yn;
end
y

Command Window:

Enter a value for an angle in degrees 45


y =
0.707106782936867
Enter a value for an angle in degrees 195
y =
-0.258819047933546

Calculator values:
0.7071067812
-0.2588190451
Chapter 6: Solved Problems 19

Problem 19
Script File:

S=0;
for i=1:50
S=S+i;
if S>=1000
disp('S is larger than 1000')
break
end
if S>100
h=floor(S/100);
t=floor((S-h*100)/10);
o=S-h*100-t*10;
if h==t & h==o
Integer=i
Sum=S
break
end
end
end

Command Window:

Integer =
36
Sum =
666
20 Chapter 6: Solved Problems

Problem 20
Script File:

clear,
gender=input('Please enter your gender (male, or female)
','s');
AGE=input('Please enter your age (a number) ');
RHR=input('Please enter your rest hart rate (a number) ');
FitLevel=input('Please enter your fitness level (low,
medium, or high) ','s');
switch gender
case 'male'
factor=(220-AGE)-RHR;
case 'female'
factor=(206-0.88*AGE)-RHR;
end
con=1;
switch FitLevel
case 'low'
INTEN=0.55;
case 'medium'
INTEN=0.65;
case 'high'
INTEN=0.8;
otherwise
disp('ERROR: Fitness level was entered incorrectly.')
con=0;
end
if con==1
THR=factor*INTEN+RHR;
fprintf('Your training heart rate is %3.0f\n',THR)
end

Command Window:

Please enter your gender (male, or female) male


Chapter 6: Solved Problems 21

Please enter your age (a number) 21


Please enter your rest hart rate (a number) 62
Please enter your fitness level (low, medium, or high)
low
Your training heart rate is 137
>>
Please enter your gender (male, or female) female
Please enter your age (a number) 19
Please enter your rest hart rate (a number) 67
Please enter your fitness level (low, medium, or high)
high
Your training heart rate is 165
22 Chapter 6: Solved Problems

Problem 21
The equation of the circle can be rewritten as:

– 2xa – 2yb + a 2 + b 2 – r 2 = – x 2 – y 2
Substituting the three points in the equation gives a system of three linear equa-
tions for the unknowns: a , b , and a 2 + b 2 – r 2 . Once the system is solved a and
b are known, and r can be determined since a 2 + b 2 – r 2 is known.
The problem is solved in the following script file.

PA=input('Enter the coordinates of the first point (vector


with two elements) ');
PB=input('Enter the coordinates of the second point (vector
with two elements) ');
PC=input('Enter the coordinates of the second point (vector
with two elements) ');
A=[-2*PA(1) -2*PA(2) 1; -2*PB(1) -2*PB(2) 1; -2*PC(1) -
2*PC(2) 1];
B=[-(PA(1)^2+PA(2)^2); -(PB(1)^2+PB(2)^2); -
(PC(1)^2+PC(2)^2)];
Cb=A\B;
a=Cb(1)
b=Cb(2)
r=sqrt(Cb(1)^2+Cb(2)^2-Cb(3))
th=linspace(0,pi,50);
xp=a+r*cos(th);
ypp=b+r*sin(th);
ypm=b-r*sin(th);
plot(xp,ypp,xp,ypm,PA(1),PA(2),'*',PB(1),PB(2),'*',PC(1),PC(
2),'*')
axis equal

Command Window:

Enter the coordinates of the first point (vector with


two elements) [13 15]
Enter the coordinates of the second point (vector with
two elements) [4 18]
Chapter 6: Solved Problems 23

Enter the coordinates of the second point (vector with


two elements) [19 3]
a =
4.000000000000001
b =
3.000000000000001
r =
15

Figure:

15

10

-5

-10

-15 -10 -5 0 5 10 15 20
24 Chapter 6: Solved Problems

Problem 22
Script File:

w=input('Enter weight in lb ');


h=input('Enter height in inches ');
BMI=703*w/h^2;
BMI=round(BMI*10)/10;
if BMI< 18.5
fprintf('Your BMI value is %4.1f, which classifies you as
underweight.\n',BMI)
elseif BMI>=18.5 & BMI<25
fprintf('Your BMI value is %4.1f, which classifies you as
Normal.'\n,BMI)
elseif BMI>=25 & BMI<30
fprintf('Your BMI value is %4.1f, which classifies you as
Overweight.\n',BMI)
elseif BMI>30
fprintf('Your BMI value is %4.1f, which classifies you as
OObese.'\n,BMI)
end

Command Window:

(a)

Enter weight in lb 180


Enter height in inches 74
Your BMI value is 23.1, which classifies you as Normal.

(a)

Enter weight in lb 150


Enter height in inches 61
Your BMI value is 28.3, which classifies you as Over-
weight.
Chapter 6: Solved Problems 25

Problem 23
Script File:

t=input('Enter the time the call is made (day, evening, or


night) ','s');
d=input('Enter the duration of the call (in minutes) ');
d=ceil(d);
switch t
case 'day'
if d <=10
cost=d*0.1;
elseif d<=30 & d>10
cost=1+(d-10)*0.08;
else
cost=2.6+(d-30)*0.06;
end
case 'evening'
if d <=10
cost=d*0.07;
elseif d<=30 & d>10
cost=0.7+(d-10)*0.05;
else
cost=1.7+(d-30)*0.04;
end
case 'night'
if d <=10
cost=d*0.04;
elseif d<=30 & d>10
cost=0.4+(d-10)*0.03;
else
cost=1+(d-30)*0.02;
end
end
fprintf('The cost of the call is $%5.2f\n',cost)
26 Chapter 6: Solved Problems

Command Window:

(a)

Enter the time the call is made (day, evening, or


night) day
Enter the duration of the call (in minutes) 8.3
The cost of the call is $ 0.90

(b)

Enter the time the call is made (day, evening, or


night) evening
Enter the duration of the call (in minutes) 34.5
The cost of the call is $ 1.90

(c)
Enter the time the call is made (day, evening, or
night) night
Enter the duration of the call (in minutes) 29.6
The cost of the call is $ 1.00
Chapter 6: Solved Problems 27

Problem 24
Script File:

AmToPy=randi(2000)/100;
fprintf('The amount to be paid is: $%5.2f\n',AmToPy)
Cash=input('Please enter payment in dollars (1, 5, 10 or 20)
');
if Cash<AmToPy
disp('Insufficient payment')
else
R=[0 0 0 0 0 0 0];
BilCoin=[1000 500 100 25 10 5 1];
C=round((Cash-AmToPy)*100);
for i=1:7
if C>=BilCoin(i)
Nunit=round(floor(C/BilCoin(i)));
R(i)=Nunit;
C=C-Nunit*BilCoin(i);
end
end
disp('The change is:')
fprintf('%2.0f $10 bills\n',R(1))
fprintf('%2.0f $5 bills\n',R(2))
fprintf('%2.0f $1 bills\n',R(3))
fprintf('%2.0f quarters\n',R(4))
fprintf('%2.0f dimes\n',R(5))
fprintf('%2.0f nickels\n',R(6))
fprintf('%2.0f cents\n',R(7))
end

Command Window:
Using the program three times:

The amount to be paid is: $ 1.69


Please enter payment in dollars (1, 5, 10 or 20) 20
The change is:
1 $10 bills
1 $5 bills
28 Chapter 6: Solved Problems

3 $1 bills
1 quarters
0 dimes
1 nickels
1 cents

The amount to be paid is: $16.01


Please enter payment in dollars (1, 5, 10 or 20) 20
The change is:
0 $10 bills
0 $5 bills
3 $1 bills
3 quarters
2 dimes
0 nickels
4 cents

The amount to be paid is: $ 8.63


Please enter payment in dollars (1, 5, 10 or 20) 10
The change is:
0 $10 bills
0 $5 bills
1 $1 bills
1 quarters
1 dimes
0 nickels
2 cents
Chapter 6: Solved Problems 29

Problem 25
25.a
Script file:
clear
t=0:0.1:10;
n=length(t);
DG=150; Vd=50;
C=DG/Vd;
ke=0.4; ka=1.6;
K=ka/(ka-ke);
Con=C*K;
Cp=Con*(exp(-ke*t)-exp(-ka*t));
plot(t,Cp)
xlabel('Time (hr)')
ylabel('Drug Concentration (mg/L)')

Figure:

1.8

1.6

1.4
Drug Concentration (mg/L)

1.2

0.8

0.6

0.4

0.2

0
0 1 2 3 4 5 6 7 8 9 10
Time (hr)
30 Chapter 6: Solved Problems

25.b
Script file:
clear
t=0:0.2:24;
n=length(t);
DG=150; Vd=50;
%C=1.5*100/50;
C=DG/Vd;
ke=0.4; ka=1.6;
K=ka/(ka-ke);
Con=C*K;
for i=1:n
Cp(i)=Con*(exp(-ke*t(i))-exp(-ka*t(i)));
if t(i)>4
Cp(i)=Cp(i)+Con*(exp(-ke*(t(i)-4))-exp(-ka*(t(i)-
4)));
end
if t(i)>8
Cp(i)=Cp(i)+Con*(exp(-ke*(t(i)-8))-exp(-ka*(t(i)-
8)));
end
if t(i)>12
Cp(i)=Cp(i)+Con*(exp(-ke*(t(i)-12))-exp(-ka*(t(i)-
12)));
end
if t(i)>16
Cp(i)=Cp(i)+Con*(exp(-ke*(t(i)-16))-exp(-ka*(t(i)-
16)));
end
end
plot(t,Cp)
xlabel('Time (hr)')
ylabel('Drug Concentration (mg/L)')
Chapter 6: Solved Problems 31

Figure:

2.5
Drug Concentration (mg/L)

1.5

0.5

0
0 5 10 15 20 25
Time (hr)
32 Chapter 6: Solved Problems

Problem 26
Script file:
P=110
imax=50;
Emax=0.00001;
x=P;
for i=1:imax
y=(x+P/x)/2;
E=abs((y-x)/x);
x=y;
if E<Emax
break
end
end
if E>Emax
y='ERROR';
disp('Solution was not obtained in 50 iterations')
end
SquareRootOfP=y

Command Window:
26.a
P =
110
SquareRootOfP =
10.4881

26.b
P =
93443
SquareRootOfP =
305.6845

26.c
P =
23.2500
SquareRootOfP =
4.8218
Chapter 6: Solved Problems 33

Problem 27
Script file:

r=1;
%k=1;c=1;
count=0;
for n=10:500;
% n
con=0;
for i=2:n-1
% i
% re=rem(n,i)
if rem(n,i)==0
con=1;
break
end
end
% con
if con==0
% count
if count ==0
pr1=n;
count=1;
elseif n==pr1+2
pr(r,1)=pr1;
pr(r,2)=n;
count=0;
r=r+1;
else
pr1=n;
end
end

end
pr
34 Chapter 6: Solved Problems

Command Window:

pr =
11 13
17 19
29 31
41 43
59 61
71 73
101 103
107 109
137 139
149 151
179 181
191 193
197 199
227 229
239 241
269 271
281 283
311 313
347 349
419 421
431 433
461 463
Chapter 6: Solved Problems 35

Problem 28
Script file:

Vin=input('Enter the value of the volume to be converted:


');
VinUnits=input('Enter the current units (m3, Liter, ft3, or
USGallon): ','s');
VoutUnits=input('Enter the new units (m3, Liter, ft3, or
USGallon): ','s');
error=0;
switch VinUnits
case 'm3'
VL=Vin*1000;
case 'Liter'
VL=Vin;
case 'ft3'
VL=Vin*(12*0.254)^3;
case 'USGallon'
VL=Vin*3.78541178;
otherwise
error=1;
end
switch VoutUnits
case 'm3'
Vout=VL/1000;
case 'Liter'
Vout=VL;
case 'ft3'
Vout=VL/(12*0.254)^3;
case 'USGallon'
Vout=VL/3.78541178;
otherwise
error=1;
end
if error
disp('ERROR current or new units are typed incorrectly.')
36 Chapter 6: Solved Problems

else
fprintf('V= %g %s\n',Vout,VoutUnits)
end

Command Window:

(a) Convert 3.5 m3 to gal.

Enter the value of the volume to be converted: 3.5


Enter the current units (m3, Liter, ft3, or USGallon):
m3
Enter the new units (m3, Liter, ft3, or USGallon):
USGallon
V= 924.602 USGallon

(b) Convert 200 L to ft3.

Enter the value of the volume to be converted: 2000


Enter the current units (m3, Liter, ft3, or USGallon):
Liter
Enter the new units (m3, Liter, ft3, or USGallon): ft3
V= 70.6293 ft3

(c) Convert 480 ft3 to m3.

Enter the value of the volume to be converted: 480


Enter the current units (m3, Liter, ft3, or USGallon):
ft3
Enter the new units (m3, Liter, ft3, or USGallon): m3
V= 13.5921 m3
Chapter 6: Solved Problems 37

Problem 29
Script file:

for j=1:100
x=0;
for i=1:1000
x=x+randn(1,1);
if abs(x)>10
break
end
end
if i==1000
disp('ERROR: Boundary was not reached in 1000 steps')
end
steps(j)=i;
end
AveNumSteps=mean(steps)

Command Window:

AveNumSteps =
104.3300
38 Chapter 6: Solved Problems

Problem 30
Script file:

x(1)=0; y(1)=0;
for i=2:10
n=randi(3);
if n==1
x(i)=0.5*x(i-1);
y(i)=0.5*y(i-1);
elseif n==2
x(i)=0.5*x(i-1)+0.25;
y(i)=0.5*y(i-1)+sqrt(3)/4;
elseif n==3
x(i)=0.5*x(i-1)+0.5;
y(i)=0.5*y(i-1);
end
end
plot(x,y,'^')

Figure when n = 10

0.5

0.4

0.3

0.2

0.1

-0.1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
Chapter 6: Solved Problems 39

Figure when n = 100

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

Figure when n = 1000

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
40 Chapter 6: Solved Problems

Figure when n = 1000

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Chapter 6: Solved Problems 41

Problem 31
Script file:

teams=1:12;
for i= 1:6
for j=1:2
n=length(teams);
N=randi(n);
Table(i,j)=teams(N);
teams(N)=[];
end
end
disp(Table)

Command Window:

5 11
4 2
10 6
3 8
1 7
12 9
42 Chapter 6: Solved Problems

Problem 32
Script file:

disp('Heat Capacity for SO2, SO3, O2, and N2 at various tem-


peratures')
Gas=input('Enter the gas name from the list abobe ','s');
switch Gas
case 'SO2'
a=38.91; b=3.904E-2; c=-3.105E-5; d=8.606E-9;
case 'SO3'
a=48.50; b=9.188E-2; c=-8.54E-5; d=32.4E-9;
case 'O2'
a=29.10; b=1.158E-2; c=-0.6076E-5; d=1.311E-9;
case 'N2'
a=29.00; b=0.2199E-2; c=-0.5723E-5; d=-2.871E-9;
end
c=1;
i=1;
while c==1
T(i)=input('Enter temperature ');
q=input('Additional temperature? (Enter yes or no) ','s');
switch q
case 'yes'
i=i+1;
case 'no'
c=0;
end
end
Cp=a+b*T+c*T.^2+d*T.^3;
Table=[T' Cp'];
fprintf('Heat capacity of %s\n',Gas)
disp(' T (C) Cp (J/(g mol)(C))')
disp(Table)
Chapter 6: Solved Problems 43

Command Window:

(a)

Heat Capacity for SO2, SO3, O2, and N2 at various tem-


peratures
Enter the gas name from the list abobe SO3
Enter temperature (C) 100
Additional temperature? (Enter yes or no) yes
Enter temperature (C) 180
Additional temperature? (Enter yes or no) no

Heat capacity of SO3

T (C) Cp (J/(g mol)(C))


100.0000 57.7204
180.0000 65.2274

(b)

Heat Capacity for SO2, SO3, O2, and N2 at various tem-


peratures
Enter the gas name from the list abobe N2
Enter temperature (C) 220
Additional temperature? (Enter yes or no) yes
Enter temperature (C) 300
Additional temperature? (Enter yes or no) no

Heat capacity of N2

T (C) Cp (J/(g mol)(C))


220.0000 29.4532
300.0000 29.5822
44 Chapter 6: Solved Problems

Problem 33
Script file:

q=input('Enter five quiz grades (0-10) in a vector ');


m=input('Enter three midterm grades (0-100) in a vector ');
f=input('Enter the final grade (0-100) ');
[qmin,iq]=min(q);
q(iq)=[];
qav=mean(q);
mav=mean(m);
if f > mav
[mmin,im]=min(m);
m(im)=[];
mav=mean(m);
end
grade=qav/10*25+mav/100*35+f/100*40;
if grade >= 90
disp('The course grade is A')
elseif grade < 90 & grade >= 80
disp('The course grade is B')
elseif grade < 80 & grade >= 70
disp('The course grade is C')
elseif grade < 70 & grade >= 60
disp('The course grade is D')
else
disp('The course grade is E')
end

Command Window:

(a)

Enter five quiz grades (0-10) in a vector [7 9 4 8 7]


Enter three midterm grades (0-100) in a vector [93 83
87]
Enter the final grade (0-100) 89
The course grade is B
Chapter 6: Solved Problems 45

(b)

Enter five quiz grades (0-10) in a vector [8 6 9 6 9]


Enter three midterm grades (0-100) in a vector [81 75
79]
Enter the final grade (0-100) 72
The course grade is C
46 Chapter 6: Solved Problems

Problem 34
Script file:

clear
clc
disp('A golfer''s record is recorded in a three columns
matrix, ')
disp('where the first column is the course rating, the sec-
ond is the ')
disp('course slope, and the third is the players score')
gr=input('Enter golfer''s record ');
[r c]=size(gr);
if r > 20
gr(21:r,:)=[];
end
hcd=(gr(:,3)-gr(:,1))./gr(:,2)*113;
if r < 5
N=0;
elseif r==5 | r == 6
N=1;
elseif r==7 | r == 8
N=2;
elseif r==9 | r == 10
N=3;
elseif r==11 | r == 12
N=4;
elseif r==13 | r == 14
N=5;
elseif r==15 | r == 16
N=6;
elseif r==17
N=7;
elseif r==18
N=8;
elseif r==19
N=9;
Chapter 6: Solved Problems 47

elseif r>=20
N=10;
end
if N==0
disp('A handicap cannot be computed for fewer than five
rounds')
else
hcdSort=sort(hcd)';
HCD=mean(hcdSort(r+1-N:r));
HCD=floor(HCD*10)/10;
fprintf('The golfer handicap is %4.1f\n',HCD)
end

Command Window:

(a)

A golfer's record is recorded in a three columns


matrix,
where the first column is the course rating, the second
is the
course slope, and the third is the players score
Enter golfer's record [71.6 122 85; 72.8 118 87; 69.7
103 83; 70.3 115 81; 70.9 116 79; 72.3 117 91
71.6 122 89; 70.3 115 83; 72.8 118 92; 70.9 109 80;
73.1 132 94; 68.2 115 78
74.2 135 103; 71.9 121 84]
The golfer handicap is 18.9

(b)

A golfer's record is recorded in a three columns


matrix,
where the first column is the course rating, the second
is the
course slope, and the third is the players score
Enter golfer's record [72.2 119 71; 71.6 122 73; 74 139
78; 68.2 125 69
70.2 130 74; 69.6 109 69; 66.6 111 74]
The golfer handicap is 5.4
48 Chapter 6: Solved Problems

You might also like