Solution chp 6 6th ed - good knowledge of matlab PDF

Title Solution chp 6 6th ed - good knowledge of matlab
Author Hasan Arshad
Course Introduction to Computer Programming
Institution COMSATS University Islamabad
Pages 52
File Size 800.8 KB
File Type PDF
Total Downloads 36
Total Views 143

Summary

good knowledge of matlab...


Description

Chapter 6

Solved Problems 1. Evaluate the following expressions without using MATLAB. Check the answers with MATLAB. (a) (b) (c)

(d)

Solution >> % Part (a) >> 6*4>32-3 ans = 0 >> % Part (b) >> y=4*3-7-1 y = 1 >> % Part (c) >> y=2*(3 % Part (d) >> (5+~0)/3==3-~(10/5-2) ans = 1 >>

1

2

Chapter 6: Solved Problems

2. Given: , , . Evaluate the following expressions without using MATLAB. Check the answers with MATLAB. (a) (b) (c) (d) Solution >> d=6 d = 6 >> e=4 e = 4 >> f=-2 f = -2 >> % Part (a) >> y=d+f>=e>d-e y = 0 >> % Part (b) >> y=e>d>f y = 1 >> % Part (c) >> y=e-d> % Part (d) >> y=(d/e*f-1*(e-d)/f y = 1 >>

3

Chapter 6: Solved Problems

3. Given: v = [–2 4 1 0 2 1 2 ] and w = [2 5 0 1 2 –1 3 ]. Evaluate the following expressions without using MATLAB. Check the answers with MATLAB. (a) ~v ==~ w (b) w > = v (c) v > ~ –1*w (d) v > –1*w Solution >> v=[-2 4 1 0 2 1 2] v = -2 4 1 >> w=[2 5 0 1 2 -1 3] w = 2 5 0 >> % Part (a) >> ~v==~w ans = 1 1 0 >> % Part (b) >> w>=v ans = 1 1 0 >> % Part (c) >> v>~1*w ans = 0 1 1 >> % Part (d) >> v>-1*w ans = 0 1 1 >>

0

2

1

2

1

2

-1

3

0

1

1

1

1

1

0

1

0

1

1

1

1

1

0

1

4

Chapter 6: Solved Problems

4. Use the vectors v and w from Problem 3. Use relational operators to create a vector u that is made up of the elements of v that are smaller than or equal to the elements of w. Solution Command Window: >> v=[-2 4 1 0 2 1 2]; >> w=[2 5 0 1 2 -1 3]; >> u=v(v6&~0 0|7&9&-3 ans = 1 >> % Part (b) >> 7>6&~0> % Part (c) >> ~4=12/6 ans = 1 >> % Part (d) >> -7 0 v(i)=2*v(i); end if v(i) < 0 v(i)= 3*v(i); end end vNew=v Command Window: v = Columns 1 through 15

7

Chapter 6: Solved Problems

-8 -9 6 14 18 -6 0 -5 14 -1 6 -5 Columns 16 through 19 8 -2 10 11 vNew = Columns 1 through 15 -24 -27 12 28 36 -18 0 -15 28 -3 12 -15 Columns 16 through 19 16 -6 20 22

7

4

-10

14

8

-30

9. Write a program that asks the user to input a vector of integers of arbitrary length. Then, using a for loop the program eliminates all the negative elements. The program displays the vector that was entered and the modified vector, and a message that says how many elements were eliminated. Execute the program and when the program ask the user to input a vector type randi([-15 20],1,25). This creates a 25-element vector with random integers between –15 and 20. Solution Script File: v=randi([-15 20],1, 25) n=length(v); j=0; for i=1:n if v(i) >= 0 j=j+1; vNew(j)=v(i); end end elim=n-j; vNew fprintf('%.0f elements were eliminated.\n',elim) Command Window: v = Columns 1 through 13

8

Chapter 6: Solved Problems

17 6 7 15 13 14 2 -9 20 Columns 14 through 25 10 3 1 -13 9 14 14 11 vNew = Columns 1 through 13 17 6 7 15 13 10 3 1 9 Columns 14 through 17 3 14 14 11 8 elements were eliminated.

5

-9

-7

16

-

-14

-13

3

-12

5

16

2

20

10. The daily high temperature (°F) in New York City and Denver, Colorado during the month of January 2014 is given in the vectors below (data from the U.S. National Oceanic and Atmospheric Administration). NYC = [33 33 18 29 40 55 19 22 32 37 58 54 51 52 45 41 45 39 36 45 33 18 19 19 28 34 44 21 23 30 39] DEN = [39 48 61 39 14 37 43 38 46 39 55 46 46 39 54 45 52 52 62 45 62 40 25 57 60 57 20 32 50 48 28] where the elements in the vectors are in the order of the days in the month. Write a program in a script file that determines and displays the following information: (a) The average temperature for the month in each city (rounded to the nearest degree). (b) The number of days that the temperature was above the average in each city. (c) The number of days that the temperature in Denver was higher than the temperature in New York. Solution Script File: clear, clc NYC = [33 33 18 29 40 55 19 22 32 37 58 54 51 52 45 41 45 ... 39 36 45 33 18 19 19 28 34 44 21 23 30 39]; DEN = [39 48 61 39 14 37 43 38 46 39 55 46 46 39 54 45 52 ... 52 62 45 62 40 25 57 60 57 20 32 50 48

Chapter 6: Solved Problems

28]; %Part (a) NYCav=round(mean(NYC)); DENav=round(mean(DEN)); fprintf('The averager temperature in New York City is: % g F.\n',NYCav) fprintf('The averager temperature in Denver is: % g F.\n',DENav) %Part (b) nNYC=sum(NYC>NYCav); nDEN=sum(DEN>DENav); fprintf('During % g days the temperature in New York City was above the average.\n',nNYC) fprintf('During % g days the temperature in Denver was above the average.\n',nDEN) %Part (c) DENhNYC=sum(DEN>NYC); fprintf('During % g days the temperature in Denver was higher than in New York City.\n',DENhNYC) Command Window: The averager temperature in New York City is: 35 F. The averager temperature in Denver is: 44 F. During 15 days the temperature in New York City was above the average. During 18 days the temperature in Denver was above the average. During 22 days the temperature in Denver was higher than in New York City.

9

10

Chapter 6: Solved Problems

11. The Pascal’s triangle can be displayed as elements in a lower-triangular matrix as shown on the right. Write a MATLAB program that creates a matrix that displays n rows of Pascal’s triangle. Use the program to create 4 and 7 rows Pascal’s triangles. (One way to calculate the value of the elements in the lower portion of the matrix is .) Solution Script File: n=6; pt=zeros(n); for i=1:n for j=1:i pt(i,j)=factorial(i-1)/(factorial(j1)*factorial(i-j)); end end pt Command Window: pt = 1 1 1 1 1 1

0 1 2 3 4 5

0 0 1 3 6 10

0 0 0 1 4 10

0 0 0 0 1 5

0 0 0 0 0 1

1 1 1 1 1 1

0 1 2 3 4 5

0 0 1 3 6 10

0 0 0 1 4 10

0 0 0 0 1 5

0 0 0 0 0 1

11

Chapter 6: Solved Problems

12. Tribonacci numbers are the numbers in a sequence in which the first three elements are 0, 1, and 1, and the value of each subsequent element is the sum of the previous three elements: 0, 1, 1, 2, 4, 7, 13, 24, ...

Write a MATLAB program in a script file that determines and displays the first 25 Fribonacci numbers. Solution Script File: F(1)=0; F(2)=1; F(3)=1; for i=4:25 F(i)=sum(F([i-3:i-1])); end F Command Window: F = Columns 1 through 6 0 1 7 Columns 7 through 12 13 24 274 Columns 13 through 18 504 927 10609 Columns 19 through 24 19513 35890 223317 410744 Column 25 755476

1

2

4

44

81

149

1705

3136

66012

5768

121415

12

Chapter 6: Solved Problems

13. The reciprocal Fibonacci constant ψ is defined by the infinite sum: ∞



ψ =

n=1

where F n are the Fibonacci numbers 1, 1, 2, 3, 5, 8, 13, ... . Each element in this sequence of numbers is the sum of the previous two. Start by setting the first two elements equal to 1, then . Write a MATLAB program in a script file that calculates ψ for a given n . Execute the program for n = 10, 50, and 100. Solution Script File: n=50; F(1)=1; F(2)=1; for i=3:n F(i)=F(i-2)+F(i-1); end xi=sum(1./F) Command Window: xi = 3.359885666243178

14. The value of π can be estimated from: ∞

 n=0

Write a program (using a loop) that determines π for a given n. Run the program with n = 10, n = 100, and n = 1000. Compare the result with pi. (Use format long.) Solution Script File: format long n=input('Enter a value for n S=0; for i=0:n S=S+(-1)^i/(2*i+1)^3; end

');

13

Chapter 6: Solved Problems

ESpi=(32*S)^(1/3) Command Window: Enter a value for n ESpi = 3.141642788603785 >> ed6_HW6_14 Enter a value for n ESpi = 3.141592719141050 >> ed6_HW6_14 Enter a value for n ESpi = 3.141592653657139

10

100

1000

15. The value of π can be estimated from the expression:

Write a MATLAB program in a script file that determine π for any number of terms. The program asks the user to enter the number of terms, and then calculates the corresponding value of π . Execute the program with 5, 10, and 40 terms. Compare the result with pi. (Use format long.) Solution Script File: format long n=5; S2=0; S=1; for i=1:n S2=sqrt(2+S2); S=S*S2/2; end piEst=2/S Command Window:

14

Chapter 6: Solved Problems

piEst = 3.140331156954753 With n=10: piEst = 3.141591421511200 With n=40: piEst = 3.141592653589794

16. Write a program that (a) generates a vector with 20 random integer elements with integers between 10 and 30, (b) replaces all the elements that are not even integers with random integers between 10 and 30, (c) repeats (b) until all the elements are even integers. The program should also count how many times (b) is repeated before all the elements are even integers. When done, the program displays the vector and a statement that states how many iterations were needed for generating the vector. Solution Script File: vInitial=randi([10 30],1, 20) for i=1:30 c=0; for j=1:20 if rem(vInitial(j),2) ~=0 vInitial(j)=randi([10 30]); c=1; end end if c ==0 break end end vFinal=vInitial fprintf('%.0f iterations were done.\n',i-1) Command Window:

15

Chapter 6: Solved Problems

vInitial = 27 12 22 17 26 20 19 30 10 30 13 24 22 27 vFinal = 10 12 22 28 26 20 16 30 10 30 14 24 22 12 4 iterations were done.

20 24

28 17

17 23

20 24

28 18

30 12

17. A vector is given by x = [9 –1.5 13.4 13.3 –2.1 4.6 1.1 5 –6.1 10 0.2]. Using conditional statements and loops, write a program that rearranges the elements of x in order from the smallest to the largest. Do not use MATLAB’s built-in function sort. Solution Script file: clear, clc x=[9 -1.5 13.4 13.3 -2.1 4.6 1.1 5 -6.1 10 0.2] n=length(x); for i=1:n-1 for j=i+1:n if x(j)>

18. The Pythagorean theorem states that a 2 + b 2 = c 2 . Write a MATLAB program in a script file that finds all the combinations of triples a, b, and c that are positive integers all smaller or equal to 50 that satisfy the Pythagorean theorem. Display the results in a three-column table in which every row corresponds to one triple. The first three rows of the table are: 3 4 5 5 12 13 6 8 10 Solution Script file: clear, clc id=1; for k=1:50 for j=k+1:50 for i=j+1:50 if i^2==k^2+j^2 a(id)=k; b(id)=j; c(id)=i; id=id+1; end end end end table=[a' b' c'] Command Window: table = 3 5 6 7 8 9 9

4 12 8 24 15 12 40

5 13 10 25 17 15 41

17

Chapter 6: Solved Problems

10 12 12 14 15 15 16 18 20 21 24 27 30

24 16 35 48 20 36 30 24 21 28 32 36 40

26 20 37 50 25 39 34 30 29 35 40 45 50

>>

19. Write a MATLAB program in a script file that finds and displays all the numbers between 100 and 999 whose product of digits is 6 times the sum of the digits. (e.g. 347 since ). Use a for loop in the program. The loop should start from 100 and end at 999. Solution Script File: for n=100:999 h=fix(n/100); d=fix((n-100*h)/10); s=n-h*100-d*10; mul=h*d*s; add=6*(h+d+s); if mul == add n end end Command Window: n =

18

Chapter 6: Solved Problems

268 n = 286 n = 347 n = 374 n = 437 n = 473 n = 628 n = 682 n = 734 n = 743 n = 826 n = 862

20. A safe prime is a prime number that can be written in the form where p is also a prime number. For example, 47 is a safe prime since and 23 is also a prime number. Write a computer program that finds and displays all the safe primes between 1 and 1000. Do not use MATLAB’s built-in function isprime. Solution Script file: clear, clc k=1; for n=1:500; nispr=1; nsafeisPrime=0; for i=2:fix(n/2) if rem(n,i)==0 nispr=0;

19

Chapter 6: Solved Problems

break end end if nispr==1 nsafe=2*n+1; nsafeisPrime=1; for j=2:fix(nsafe/2) if rem(nsafe,j)==0 nsafeisPrime=0; break end end end if nsafeisPrime==1 SafePrimeNum(k)=nsafe; k=k+1; end end SafePrimeNum Command Window: SafePrimeNum = Columns 1 through 7 3 5 7 11 Columns 8 through 14 83 107 167 179 Columns 15 through 21 359 383 467 479 Columns 22 through 26 719 839 863 887

23

47

59

227

263

347

503

563

587

983

20

Chapter 6: Solved Problems

21. Sexy primes are two prime numbers that the difference between them is 6. For example, 23 and 29 are sexy primes since . Write a computer program that finds all the sexy primes between 1 and 300. The numbers should be displayed in a two-column matrix where each row display one pair. Do not use MATLAB’s built-in function isprime. Solution Script file: clear, clc k=1; for n=1:300; nispr=1; nSexyeisPrime=0; for i=2:fix(n/2) if rem(n,i)==0 nispr=0; break end end if nispr==1 nsexy=n+6; nSexyeisPrime=1; for j=2:fix(nsexy/2) if rem(nsexy,j)==0 nSexyeisPrime=0; break end end end if nSexyeisPrime==1 SexyPrimeNum(k,1)=n; SexyPrimeNum(k,2)=nsexy; k=k+1; end end SexyPrimeNum Command Window:

Chapter 6: Solved Problems

SexyPrimeNum = 1 7 5 11 7 13 11 17 13 19 17 23 23 29 31 37 37 43 41 47 47 53 53 59 61 67 67 73 73 79 83 89 97 103 101 107 103 109 107 113 131 137 151 157 157 163 167 173 173 179 191 197 193 199 223 229 227 233 233 239 251 257 257 263 263 269 271 277 277 283 >>

21

22

Chapter 6: Solved Problems

22. A Mersenne prime is a prime number that is equal to

, where n is an inte-

ger. For example, 31 is a Mersenne prime since . Write a computer program that finds all the Mersenne primes between 1 and 10,000. Do not use MATLAB’s built-in function isprime. Solution Script file: clear, clc k=1; for N=3:10000; nispr=1; for i=2:fix(N/2) if rem(N,i)==0 nispr=0; break end end if nispr==1 n=ceil(log(N)/log(2)); if N == 2^n-1 MersennePrimes(k)=N; k=k+1; end end end MersennePrimes Command Window: MersennePrimes = 3

7

31

127

8191

23

Chapter 6: Solved Problems

23. A perfect number is a positive integer that is equal to the sum of its positive divisors except the number itself. The first two perfect numbers are 6 and 28 since and . Write a computer program that finds the first four perfect numbers. Solution Script file: clear, clc j=1; for N=2:10000 clear div div(1)=1; k=2; for i=2:fix(N/2) if rem(N,i)==0 div(k)=i; k=k+1; end end if sum(div)==N PerfectN(j)=N; j=j+1; end end PerfectN Command Window: PerfectN = 6 >>

28

496

8128

24

Chapter 6: Solved Problems

24. A list of exam scores (S) (in percent out of 100%) is given: 72, 81, 44, 68, 90, 53, 80, 75, 74, 65, 50, 92, 85, 69, 41, 73, 70, 86, 61, 65, 79, 94, 69. Write a computer program that calculates the average (Av) and standard deviation (Sd) of the scores, which are rounded to the nearest integer. Then, the program determines the letter grade of each of the scores according to the following scheme: Score (%) Letter grade Score (%)

A

B

Letter grade Score (%)

C

D

Letter grade F The program displays the values of Av and Sd followed by a list that shows the scores and the corresponding letter grade (e.g. 72% Letter grade C).

Solution Script file: clear,clc G=[72 81 44 68 90 53 80 75 74 65 50 92 85 69 41 73 70 86 61 65 79 94 69]; Av=round(mean(G)); S=round(std(G)); fprintf('Average grade is %3.0f%%. Standard deviation is %3.0f%%.\n',Av,S) disp(' ') n=length(G); for i=1:n if G(i) > Av+1.3*S fprintf('%3i%% Letter grade A\n',G(i)) elseif G(i) > Av+0.5*S & G(i) < Av+1.3*S fprintf('%3i%% Letter grade B\n',G(i)) elseif G(i) > Av-0.5*S & G(i) < Av+0.5*S fprintf('%3i%% Letter grade C\n',G(i)) elseif G(i) > Av-1.3*S & G(i) < Av-0.5*S fprintf('%3i%% Letter grade D\n',G(i)) elseif G(i) < Av-1.3*S fprintf('%3i%% Letter grade F\n',G(i)) end

25

Chapter 6: Solved Problems

end Command Window: Average grade is

71%. Standard deviation is

72% 81% 44% 68% 90% 53% 80% 75% 74% 65% 50% 92% 85% 69% 41% 73% 70% 86% 61% 65% 79% 94% 69% >>

C B F C A D B C C C F A B C F C C B D C B A C

Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter Letter

grade grade grade grade grade grade grade grade grade grade grade grade grade grade grade grade grade grade grade grade grade grade grade

14%.

26

Chapter 6: Solved Problems

25. The Taylor series expansion for ax is: ∞

 n=0

Write a MATLAB program that determines ax using the Taylor series expansion. The program asks the user to type a value for x. Use a loop for adding the terms of the Taylor series. If cn is the nth term in the series, then the sum Sn of the n terms is . In each pass calculate the estimated error E given by

. Stop adding terms when x

The program displays the value of a . Use the program to calculate: (a) 23.5 (b) 6.31.7 Compare the values with those obtained by using a calculator. Solution Script File: a=input('Enter a value for a x=input('Enter a value for x

'); ');

S=1; for n=1:30 cn=log(a)^n/factorial(n)*x^n; Sn=S+cn; if abs((Sn-S)/S)= 100 & Sm 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)') Figure:

39

Chapter 6: Solved Problems

32. One numerical method for calculating the cubic root of a number, 3 P is Halley’s method. The solution process starts by choosing a value x 1 as a first estimate of the solution. Using this value, a second, more accurate value x 2 is calculated with, which is then used for calculating a third, still more accurate value x 3 , and so on. The general equation for calculating the value of x i + 1 from the value of x i is . Write a MATLAB program that calculates the cubic root of a number. In the program use x 1 = P for the first estimate of the solution. Then, by using the general equation in a loop, calculate new, more accurate values. Stop the looping when the estimated relative error E defined by

is smaller than

0.00001. Use the program to calculate: (a)

3

800

(b)

3

59071

(c)

Solution Script File: P=input('Enter a number for calculating its cubic root: '); xi=P*(P^3+2*P)/(2*P^3+P); for n=1:30 xip1=xi*(xi^3+2*P)/(2*xi^3+P); if abs((xip1-xi)/xi)> Enter the value of the area to be converted: 2400 Enter the current units (sqm, sqcm, sqin, sqft or sqyd): sqft Enter the new units (sqm, sqcm, sqin, sqft or sqyd): sqm A= 222.967 sqm >> Enter the value of the area to be converted: 300 Enter the current units (sqm, sqcm, sqin, sqft or sqyd): sqcm Enter the new units (sqm, sqcm, sqin, sqft or sqyd): sqyd A= 0.0358797 sqyd >>

41

42

Chapter 6: Solved Problems

34. In a one-dimensional random walk, the position x of a walker is computed by: xj = xj + s

where s is a random number. Write a program that ca...


Similar Free PDFs