Scilab B a s i c p r a c t i c ef o r t h ec o u r s e 18505-M a t h e m a t i c a l Mo d e l l i n g

19 

Loading....

Loading....

Loading....

Loading....

Loading....

Full text

(1)

Lecture

Notes

of

Athithan

S

UMA18505-Mathematical Modelling

Prepared by

Dr. S. ATHITHAN

Assistant Professor

Department of of Mathematics

Faculty of Engineering and Technology

SRM INSTITUTE OF SCIENCE AND

TECHNOLOGY

Kattankulathur-603203, Kancheepuram District.

(2)

Lecture

Notes

of

Athithan

S

Lab Manual

UMA18505-Mathematical Software Scilab

Contents

1 Exercises 2

1.1 Exercise 1 . . . 2

1.2 Exercise 2 . . . 4

1.3 Exercise 3 . . . 6

1.4 Exercise 4 . . . 8

1.5 Exercise 5 . . . 10

1.6 Exercise 6 . . . 12

(3)

Lecture

Notes

of

Athithan

S

1

Exercises

1.1

Exercise 1

The following exercises are meant to be answered by a single Scilab command. The command may be involved (i.e., it may use a number of parentheses or calls to functions) but can, in essence, be solved by the execution of a single command. If the command is too complicated, feel free to break it up over two or more lines.

1

2 1 . Create a vector of the even whole numbers between 31

and 75.

3

4 2 . Let x = [ 9 1 6 7 ] .

5

6 a . Add 16 to each element

7 b . Add 3 to j u s t the odd−index elements

8 c . Compute the square root of each element

9 d . Compute the square of each element

10 11

12 3 . Let x = [ 3 2 6 8 ] ’ and y = [ 4 1 3 5 ] ’ ( x and y

should be column v e c t o r s ) .

13

14 a . Add the sum of the elements in x to y

15 b . Raise each element of x to the power s p e c i f i e d by

the corresponding element in y .

16 c . Divide each element of y by the corresponding

element in x

17 d . Multiply each element in x by the corresponding

element in y , c a l l i n g the r e s u l t "z".

18 e . Add up the elements in z and a s s i g n the r e s u l t to a

v a r i a b l e c a l l e d "w".

19 f . Compute x ’ ∗ y − w and i n t e r p r e t the r e s u l t

20 21

22 4 . Evaluate the f o l l o w i n g S c i l a b e x p r e s s i o n s by hand

and use S c i l a b to check the answers

23

24 a . 2 / 2 ∗ 3

(4)

Lecture

Notes

of

Athithan

S

26 c . 10 / 2 \ 5 − 3 + 2 ∗ 4

27 d . 3 ^ 2 / 4

28 e . 3 ^ 2 ^ 2

29 f . 2 + round(6 / 9 + 3 ∗ 2) / 2 − 3

30 g . 2 + f l o o r(6 / 9 + 3 ∗ 2) / 2 − 3

31 h . 2 + c e i l (6 / 9 + 3 ∗ 2) / 2 − 3

32

33 5 . Create a vector x with the elements . . .

34

35 a . 2 , 4 , 6 , 8 , . . .

36 b . 10 , 8 , 6 , 4 , 2 , 0 , −2, −4

37 c . 1 , 1/2 , 1/3 , 1/4 , 1/5 , . . .

38 d . 0 , 1/2 , 2/3 , 3/4 , 4/5 , . . .

39 40

41 6 . Create a vector x with the elements x^n = (−1)^(n+1)

/(2n−1) . Add up the elements of the v e r s i o n of t h i s

vector that has 100 elements .

42 43

44 7 . Given a vector , t , of length n , write down the

S c i l a b e x p r e s s i o n s that w i l l c o r r e c t l y compute the f o l l o w i n g :

45

46 a . ln (2 + t + t ^2)

47 b . et (1 + cos(3 t ) )

48 c . cos^2( t ) + s i n^2( t )

49 d . tan^(−1) (1) ( t h i s i s the i n v e r s e tangent f u n c t i o n)

50 e . cot ( t )

51 f . sec ^2( t ) + cot ( t ) − 1

52

53 Test that your s o l u t i o n works f o r t = 1 : 0 . 1 : 5

54 55

56 8 . Make a good p l o t ( i . e . , a non−choppy p l o t) of the

f u n c t i o n f ( x ) = s i n(1/ x ) f o r 0.01 < x < 0 . 1 . How

did you c r e a t e x so that the p l o t looked good?

57

58 9 . In polar c o o r d i n a t e s ( r , t ) , the equation of an

e l l i p s e with one of i t s f o c i at the o r i g i n i s r ( t ) =

(5)

Lecture

Notes

of

Athithan

S

the semi−major a x i s ( along the x−a x i s ) and e i s the

e c c e n t r i c i t y . Plot e l l i p s e s using t h i s formula ,

ensuring that the curves are smooth by s e l e c t i n g an

appropriate number of point s in the angular ( t )

coordinate . Use the command a x i s equal to s e t the

proper a x i s r a t i o to see the e l l i p s e s .

59

60 10. Plot the e x p r e s s i o n ( determined in modelling the

growth of the US population ) P( t ) = 197 ,273 ,000/(1 +

e ^((−0.0313) ( t − 1913.25) ) ) , where t i s the date,

in years AD, using t = 1790 to 2000. What population i s p r e d i c t e d in the year 2020?

1.2

Exercise 2

The following exercises are meant to be answered by a single Scilab command. The command may be involved (i.e., it may use a number of parentheses or calls to functions) but can, in essence, be solved by the execution of a single command. If the command is too complicated, feel free to break it up over two or more lines.

1

2 1 . Given x = [ 3 1 5 7 9 2 6 ] , expla i n what the

f o l l o w i n g commands "mean" by summarizing the net

r e s u l t of the command .

3

4 a . x (3)

5 b . x ( 1 : 7 ) 6 c . x ( 1 :end)

7 d . x ( 1 :end−1)

8 e . x (6:−2:1)

9 f . x ( [ 1 6 2 1 1 ] )

10 g . sum( x )

11

12 2 . Given the array A = [ 2 4 1 ; 6 7 2 ; 3 5 9 ] ,

provide the commands needed to

13

14 a . a s s i g n the f i r s t row of A to a vector c a l l e d x1

15 b . a s s i g n the l a s t 2 rows of A to an array c a l l e d y

16 c . compute the sum over the columns of A

(6)

Lecture

Notes

of

Athithan

S

18 e . compute the standard e r r o r of the mean of each

column of A ( the standard e r r o r of the mean i s

d e f in e d as the standard d e v i a t i o n divided by the square root of the number of elements used to

compute the mean. )

19 20

21 3 . Given the arrays x = [ 1 4 8 ] , y = [ 2 1 5 ] and A = [ 3

1 6 ; 5 2 7 ] , determine which of the f o l l o w i n g statements w i l l c o r r e c t l y execute and provide the r e s u l t . I f the command w i l l not c o r r e c t l y execute ,

s t a t e why i t w i l l not . Using the command whos may be

h e l p f u l here .

22

23 a . x + y

24 b . x + A

25 c . x ’ + y

26 d . A − [ x ’ y ’ ]

27 e . [ x ; y ’ ]

28 f . [ x ; y ]

29 g . A − 3

30

31 4 . Given the array A = [ 2 7 9 7 ; 3 1 5 6 ; 8 1 2 5 ] ,

e x p la i n the r e s u l t s of the f o l l o w i n g commands :

32

33 a . A’

34 b . A( : , [ 1 4 ] ) 35 c . A( [ 2 3 ] , [ 3 1 ] )

36 d . reshape (A, 2 , 6 )

37 e . A( : )

38 f . f l i p u d (A) 39 g . f l i p l r (A)

40 h . [A A(end , : ) ]

41 i . A( 1 : 3 , : )

42 j . [A ; A( 1 : 2 , : ) ]

43 k . sum(A)

44 l . sum(A’ )

45 m. sum(A, 2 )

46 k . [ [ A ; sum(A) ] [ sum(A, 2 ) ; sum(A( : ) ) ] ]

(7)

Lecture

Notes

of

Athithan

S

48 5 . Given the array A from problem 4 , above , provide the

command that w i l l

49

50 a . a s s i g n the even−numbered columns of A to an array

c a l l e d B

51 b . a s s i g n the odd−numbered rows to an array c a l l e d C

52 c . convert A i n t o a 4−by−3 array

53 d . compute the r e c i p r o c a l of each element of A

54 e . compute the square−root of each element of A

55 56

57 6 . Give the f o l l o w i n g commands to c r e a t e an array

c a l l e d F :

58

59 >> randn ( ’ seed ’,123456789)

60 >> F = randn (5 ,10) ;

61

62 a . Compute the mean of each column and a s s i g n the

r e s u l t s to the elements of a vector c a l l e d avg .

63 b . Compute the standard d e v i a t i o n of each column and

a s s i g n the r e s u l t s to the elements of a vector c a l l e d s .

64 c . Compute the vector of t−s c o r e s that t e s t the

hypothesis that the mean of each column i s no

d i f f e r e n t from zero .

65 d . I f Pr ( | t | > 2.132 ) = 0.1 with 4 degrees of freedom ,

are any of the mean values in the vector avg

s t a t i s t i c a l l y d i f f e r e n t from 0?

1.3

Exercise 3

The following exercises are meant to be answered by a single Scilab command. The command may be involved (i.e., it may use a number of parentheses or calls to functions) but can, in essence, be solved by the execution of a single command. If the command is too complicated, feel free to break it up over two or more lines.

1

2 1 . Given that x = [ 1 5 2 8 9 0 1 ] and y = [ 5 2 2 6 0 0

(8)

Lecture

Notes

of

Athithan

S

3

4 a . x > y

5 b . y < x

6 c . x == y

7 d . x <= y

8 e . y >= x

9 f . x | y

10 g . x & y

11 h . x & (~y )

12 i . ( x > y ) | ( y < x ) 13 j . ( x > y ) & ( y < x ) 14

15 2 . The e x e r c i s e s here show the techniques of l o g i c a l−

indexing ( indexing with 0−1 v e c t o r s ) . Given x = 1:10

and y = [ 3 1 5 6 8 2 9 4 7 0 ] , execute and i n t e r p r e t the r e s u l t s of the f o l l o w i n g commands :

16

17 a . ( x > 3) & ( x < 8)

18 b . x ( x > 5)

19 c . y ( x <= 4)

20 d . x ( ( x < 2) | ( x >= 8) )

21 e . y ( ( x < 2) | ( x >= 8) )

22 f . x ( y < 0) 23

24 3 . The i n t r o d u c t i o n of the l o g i c a l data type in v5 . 3

has f o r c e d some changes in the use of non−l o g i c a l

0−1 v e c t o r s as i n d i c e s f o r s u b s c r i p t i n g . You can

see the d i f f e r e n c e s by executing the f o l l o w i n g commands that attempt to e x t r a c t the elementsof y that correspond to e i t h e r the odd ( a . ) or even (b . ) elements of x :

25

26 a . y ( rem (x , 2 ) ) vs . y ( l o g i c a l ( rem (x , 2 ) ) )

27 b . y(~rem (x , 2 ) ) vs . y(~ l o g i c a l ( rem (x , 2 ) ) )

28

29 4 . Given x = [ 3 15 9 12 −1 0 −12 9 6 1 ] , provide the

command( s ) that w i l l

30

31 a . . . . s e t the values of x that are p o s i t i v e to zero

32 b . . . . s e t values that are m u l t i p l e s of 3 to 3 ( rem

(9)

Lecture

Notes

of

Athithan

S

33 c . . . . multiply the values of x that are even by 5

34 d . . . . e x t r a c t the values of x that are g r e a t e r than 10

i n t o a vector c a l l e d y

35 e . . . . s e t the values in x that are l e s s than the mean

to zero

36 f . . . . s e t the values in x that are above the mean to

t h e i r d i f f e r e n c e from the mean

37 38

39 5 . Create the vector x = randperm (35) and then evaluate

the f o l l o w i n g f u n c t i o n using only l o g i c a l indexing :

40

41 y ( x ) = 2 i f x < 6

42 = x − 4 i f 6 <= x < 20

43 = 36 − x i f 20 <= x <= 35

44

45 You can check your answer by p l o t t i n g y vs . x with

symbols . The curve should be a t r i a n g u l a r shape ,

always above zero and with a maximum of 16. I t

might a l s o be u s e f u l to try s e t t i n g x to 1 : 3 5 . Using mu lti ple s t e p s ( or a simple Mfile ) i s

recommended f o r t h i s problem .

1.4

Exercise 4

The following exercises are meant to be answered by a single Scilab command. The command may be involved (i.e., it may use a number of parentheses or calls to functions) but can, in essence, be solved by the execution of a single command. If the command is too complicated, feel free to break it up over two or more lines.

In each of the following questions, evaluate the given Scilab code frag-ments for each of the cases indicated. Use Scilab to check your answers.

1 1 . i f n > 1 a . n = 7 m = ?

2 m = n+1 b . n = 0 m = ?

3 e l s e c . n = −10 m = ?

4 m = n − 1

5 end 6 7

(10)

Lecture

Notes

of

Athithan

S

9 w = 2∗ z b . z = 9 w = ?

10 e l s e i f z < 10 c . z = 60 w = ?

11 w = 9 − z d . z = 200 w = ?

12 e l s e i f z < 100 13 w = s q r t( z ) 14 e l s e

15 w = z

16 end 17 18 19

20 3 . i f T < 30 a . T = 50 h = ?

21 h = 2∗T + 1 b . T = 15 h = ?

22 e l s e i f T < 10 c . T = 0 h = ?

23 h = T − 2

24 e l s e

25 h = 0

26 end 27 28

29 4 . i f 0 < x < 10 a . x = −1 y = ?

30 y = 4∗x b . x = 5 y = ?

31 e l s e i f 10 < x < 40 c . x = 30 y = ?

32 y = 10∗x d . x = 100 y = ?

33 e l s e

34 y = 500

35 end 36 37

38 Write b r i e f s c r i p t s to evaluate the f o l l o w i n g f u n c t i o n s

. I f you s t a r t each s c r i p t with a req uest f o r input

( using input) , you ’ l l be able to t e s t that your code

provides the c o r r e c t r e s u l t s .

39

40 5 . h(T) = T − 10 when 0 < T < 100

41 = 0.45 T + 900 when T > 100

42

43 Test c a s e s : a . T = 5 , h = −5

44 b . T = 110 , h = 949.5

45

(11)

Lecture

Notes

of

Athithan

S

47 = 0 i f x = 0

48 = 1 i f x > 0

49

50 Compare your r e s u l t s to the S c i l a b f u n c t i o n s i g n .

51

52 7 . t ( y ) = 200 when y i s below

10 ,000

53 = 200 + 0.1 ( y − 10 ,000) when y i s between 10 ,000

and 20 ,000

54 = 1 ,200 + 0.15 ( y − 20 ,000) when y i s between 20 ,000

and 50 ,000

55 = 5 ,700 + 0.25 ( y − 50 ,000) when y i s above 50 ,000

56

57 Test c a s e s : a . y = 5 ,000 t = 200

58 b . y = 17 ,000 t = 900

59 b . y = 25 ,000 t = 1 ,950

60 c . y = 75 ,000 t = 11 ,950

61

62 8 . Explain why the f o l l o w i n g i f−block would not be a

c o r r e c t s o l u t i o n to the previous e x e r c i s e .

63

64 i f y < 10000

65 t = 200

66 e l s e i f 10000 < y < 20000

67 t = 200 + 0 . 1 ∗ ( y − 10000)

68 e l s e i f 20000 < y < 50000

69 t = 1200 + 0 . 1 5 ∗ ( y − 20000)

70 e l s e i f y > 50000

71 t = 5700 + 0. 2 5 ∗ ( y − 50000)

72 end

1.5

Exercise 5

Though Scilab has a number of built-in functions that are equivalent to some of the following exercises, use a loop construct to carry out the indicated computations. It will probably be easiest to write a short script since these are inherently multi-line problems.

1

2 1 . Given the vector x = [ 1 8 3 9 0 1 ] , c r e a t e a short

(12)

Lecture

Notes

of

Athithan

S

3

4 a . Add up the values of the elements ( Check with sum. )

5 b . Computes the running sum (f o r element j , the running

sum i s the sum of the elements from 1 to j ,

i n c l u s i v e . Check with cumsum. )

6 c . computes the s i n e of the given x−values ( should be a

vector )

7 8

9 2 . Create an M−by−N array of random numbers ( use rand) .

Move through the array , element by element , and s e t any value that i s l e s s than 0.2 to 0 and any value that i s g r e a t e r than ( or equal to ) 0.2 to 1 .

10 11

12 3 . Given x = [ 4 1 6 ] and y = [ 6 2 7 ] , compute the

f o l l o w i n g arrays

13

14 a . a i j = x i y j

15 b . b i j = xi / yj

16 c . c i = xiyi , then add up the elements of c .

17 d . d i j = xi /(2 + xi + yj )

18 e . e i j = r e c i p r o c a l of the l e s s e r of xi and yj

19 20

21 4 . Write a s c r i p t that w i l l use the random−number

generator rand to determine the

22 f o l l o w i n g : 23

24 a . The number of random numbers i t takes to add up to

20 ( or more ) .

25 b . The number of random numbers i t takes b e f o r e a

number between 0.8 and

26 0.85 occurs .

27 c . The number of random numbers i t takes b e f o r e the

mean of those numbers

28 i s within 0.01 of 0.5 ( the mean of t h i s random−number

generator ) .

29

30 I t w i l l be worthwhile to run your s c r i p t s e v e r a l times

(13)

Lecture

Notes

of

Athithan

S

you p r e d i c t any of the r e s u l t s that are d e s c r i b e d

above ?

31 32

33 5 . Write a s c r i p t that asks f o r a temperature ( in

degrees Fahrenheit ) and computes the e q u i v a l e n t

temperature in degrees C e l c i u s . The s c r i p t should

keep running u n t i l no number i s provided to convert .

[ the f u n c t i o n isempty w i l l be u s e f u l here . ]

1.6

Exercise 6

The following exercises are meant to be answered by either a script or a function Mfile (you decide). The descriptions are complete but the nature of the input/output and display options is left to you. Also, some problems are much simpler than others. Suggestion: start simple and add complexity.

1 1 . The Fibonacci numbers are comuted according to the

f o l l o w i n g r e l a t i o n :

2

3 F(n) = F(n−1) + F(n−2)

4

5 with F(0) = F(1) = 1 .

6

7 a . Compute the f i r s t 10 Fibonacci numbers .

8 b . For the f i r s t 50 Fibonacci numbers , compute the

r a t i o

9

10 F(n) /F(n−1)

11

12 I t i s claimed that t h i s r a t i o approaches the value of

the golden mean ( (1 + s q r t(5) ) /2 ) . What do your

r e s u l t s show?

13

14 2 . The Legendre polynomials (Pn( x ) ) are de fine d by the

f o l l o w i n g recurrance r e l a t i o n

15

16 (n+1) Pn+1(x ) − (2n+1)x Pn( x ) + n Pn−1(x ) = 0

17

18 with P0( x ) = 1 , P1( x ) = x and P2( x ) = (3 x^2 − 1) /2.

(14)

Lecture

Notes

of

Athithan

S

a l l 6 over the i n t e r v a l [−1 ,1]. For more

information on Legendre polynomials , see the Mathworld s i t e .

19

20 3 . The present value of an annuity ( a y e a r ly sum of

money) may be computed from the formula

21

22 P = (A/ i ) [ ( 1 + i )^n − 1 ] / ( 1 + i )^n

23

24 where A i s the annuity ( in Rs . / year ) , i i s the nominal

y e a r l y i n t e r e s t r a t e ( in decimal form ) , n i s the number of years over which the annuity i s paid and P

i s the present value ( in Rs . ) .

25

26 Example computation : I f i = 0.15 (15%) , A = Rs .100/ year

and n = 10 years then P = Rs , 5 0 1 . 8 8 .

27

28 I f you won the Rs .1 ,000 ,000 State Lottery and the

Lottery o f f e r e d you the ch oi ce of Rs . 500 ,000 today

or Rs .50 ,000/ year f o r 20 years , which would you take

? You can assume an i n f l a t i o n ( i n t e r e s t ) r a t e of 5% .

29

30 4 . Write a s c r i p t that asks f o r an i n t e g e r (n) and then

computes the f o l l o w i n g based on the value of the i n t e g e r :

31

32 While the value of n i s g r e a t e r than 1 , r e p l a c e the

i n t e g e r with h a l f of i t s value (n/2) i f the i n t e g e r

i s even . Otherwise , r e p l a c e the i n t e g e r with three

times i t s value , plus 1 (3∗n + 1) .

33

34 Make p r o v i s i o n to count the number of values in ( or the

length of ) the sequence that r e s u l t s .

35

36 Example c a l c u l a t i o n : I f n = 10 , the sequence of

i n t e g e r s i s 5 , 16 , 8 , 4 , 2 , 1 and so the length i s

6 .

37

38 Make a p l o t of the length of the sequence that occurs

(15)

Lecture

Notes

of

Athithan

S

example , when n = 10 , the length i s 6 while f o r n =

15 , the length i s 17. I s there any pattern ? Try

l a r g e r numbers to see i f any pattern occurs . I s

there any i n t e g e r f o r which the sequence does not

terminate ?

39 40

41 5 . Compute and p l o t the path ( s ) of a s e t of random

walkers which are confined bya p a i r of b a r r i e r s at +

B u n i t s and −B u n i t s from the o r i g i n (where the

walkers a l l s t a r t from ) .

42

43 A random walk i s computed by r e p e a t e d l y performing the

c a l c u l a t i o n

44

45 xj+1 = xj + s

46

47 where s i s a number drawn from the standard normal

d i s t r i b u t i o n ( randn in S c i l a b ) . For example , a walk

of N s t e p s would be handled by the code fragment

48

49 x (1) = 0 ;

50 f o r j = 1 :N

51 x ( j +1) = x ( j ) + randn ( 1 , 1 ) ;

52 end 53

54 There are three p o s s i b l e ways that the w a l l s can " act ":

55

56 a . R e f l e c t i n g − In t h i s case , when the new p o s i t i o n i s

o u t s i d e the walls , the walker i s "bounced" back by

the amount that i t exceeded the b a r r i e r .

57 That is ,

58

59 when xj+1 > B,

60 xj+1 = B − |B − xj +1|

61

62 when xj+1 < (−B) ,

63 xj+1 = (−B) + |(−B) − xj +1|

64

65 I f you p l o t the paths , you should not see any p o s i t i o n s

(16)

Lecture

Notes

of

Athithan

S

66

67 b . Absorbing − In t h i s case , i f a walker h i t s or

exceeds the wall p o s i t i o n s , i t " d i e s " or i s absorbed

and the walk ends . For t h i s case , i t i s of

i n t e r e s t to determine the mean l i f e t i m e of a walker

( i . e . , the mean and d i s t r i b u t i o n of the number of

s t e p s the " average " walker w i l l take b e f o r e being

absorbed ) .

68

69 c . P a r t i a l l y absorbing − This case i s a combination of

the previous two c a s e s . When a walker encounters a

wall , "a coin i s f l i p p e d " to see i f the walker

r e l f e c t s or i s absorbed . Assuming a p r o b a b i l i t y p

(0 < p < 1) f o r r e f l e c t i o n , the pseudo−code fragment

that f o l l o w s uses the S c i l a b uniform random−number

generator to make the r e f l e c t / absorb d e c i s i o n :

70

71 i f rand < p

72 r e f l e c t

73 e l s e

74 absorb

75 end 76

77 What do you do with a l l the walks that you generate ?

Compute s t a t i s t i c s , of course .

78 Answering q u e s t i o n s l i k e

79

80 What i s the average p o s i t i o n of the walkers as a

f u n c t i o n of time ?

81 What i s the standard d e v i a t i o n of the p o s i t i o n of the

walkers as a f u n c t i o n of time ?

82 Does the absorbing or r e f l e c t i n g c h a r a c t e r i n f l u e n c e

these summaries ?

83 For the absorbing / p a r t i a l−r e f l e c t i o n case , a p l o t of

the number of s u r v i v i n g walkers as a f u n c t i o n of

step numbers i s a very i n t e r e s t i n g thing .

84

85 I s u s e f u l , i n f o r m a t iv e and i n t e r e s t i n g , p a r t i c u l a r l y i f

g r a p h i c a l l y displayed .

(17)

Lecture

Notes

of

Athithan

S

88

89 6 . Write a f u n c t i o n which computes the cumulative

product of the elements in a vector . The cumulative

product of the j t h element of the vector x , xj , i s d e f in e d by

90

91 pj = ( x1 ) ( x2 ) . . . ( xj )

92

93 f o r j = 1 :length of the vector x . Create 2 d i f f e r e n t

v e r s i o n s of t h i s f u n c t i o n:

94

95 a . One that uses two for−loops to e x p l i c i t l y carry out

the c a l c u l a t i o n s , element by element . An " inner "

loop should accumulate the product and an " outer "

loop should more through the elements of the vector p .

96

97 b . One that uses the b u i l t−in f u n c t i o n prod to r e p l a c e

the inner loop .

98

99 In each case , you can check your r e s u l t s with the b u i l t

−in function , cumprod.

100

101 7 . Follow the d i r e c t i o n s f o r E xer c is e 11 but c r e a t e a

f u n c t i o n that computes the cumulative sum of the

elements of a vector . The elements of the

cumulative sum vector are define d by

102

103 s j = x1 + x2 + . . . + xj

104

105 f o r j = 1 : length of the vector x . 106

107 The b u i l t−in f u n c t i o n s sum and cumsum should be used

i n s t e a d of prod and cumprod, r e s p e c t i v e l y .

108

109 8 . Create a f u n c t i o n that g e n e r a t e s random arrays of

i n t e g e r s beween a and b , i n c l u s i v e . Use the

d e f i n i t i o n l i n e

110

(18)

Lecture

Notes

of

Athithan

S

113 where a and b d e f i n e the ( i n c l u s i v e ) range and M and N

d e f i n e the s i z e of the output array ( rows and

columns , r e s p e c t i v e l y ) .

114

115 a . Test your f u n c t i o n with the f o l l o w i n g p i e c e of code :

116

117 x = randint (10 ,17 ,100000 ,1) ;

118 h i s t (x , 1 0 : 1 7 ) 119

120 The r e s u l t i n g histogram should be nearly f l a t from 10

to 17. Pay p a r t i c u l a r a t t e n t i o n to the endpoints of

the d i s t r i b u t i o n .

121

122 b . Test your f u n c t i o n with the f o l l o w i n g p i e c e s of code

:

123

124 x = randint (−30 ,5 ,100000 ,1) ;

125 h i s t (x ,−30:5)

126

127 x = randint (−45 ,−35 ,100000 ,1) ;

128 h i s t (x,−45:−35) 129

130 x = randint (7 ,−2 ,100000 ,1) ;

131 h i s t (x ,−2:7) 132

133 Consider that each of these uses i s v a l i d .

134

135 c . Modify your code to allow f o r missing / d e f a u l t inputs

. Use the behavior of rand to help design your code

. You w i l l need to use nargin in your r o u t i n e . For

example , the f u n c t i o n should be able to return

136

137 − a 5−by−5 array of i n t e g e r s between 1 and 20: e . g . , A

= randint ( 1 , 2 0 ,5 )

138 − a s i n g l e random i n t e g e r between 10 and 50: e . g . , A

= randint (10 ,50)

(19)

Lecture

Notes

of

Athithan

S

1.7

Exercise 7(Main Exercises for our Lab Course)

1. Basic Mathematical Identities and Formulae (how they come into ex-istence?)

2. Any two methods to find the simple interest and compound interest 3. The staircase problem and handshake problem

4. Tower of Hanoi problem 5. Line and Curve Fitting model 6. Linear and nonlinear models 7. Predator-Prey Model

8. Compartmental Models 9. Models with constraints 10. Probability Models

11. Numerical Simulations for all these models

Contact: (+91) 979 111 666 3 (or) athithansingaram@gmail.com

Figure

Updating...