Docsity

MSC321 Homework Solutions, Assignments of Earth Sciences

Toggle sidebar
bg1
MSC321 Homework 2
1. Write a program that accepts two values xand yand that prints out the
following function:
f(x, y) =
x+y x ≥0 and y≥0
x+y2x≥0 and y < 0
x2+y x < 0 and y≥0
x2+y2x < 0 and y < 0
(1)
2. Write a program that iteratively reads an integer between 1 and 7, and prints
the corresponding day the of the week. If the value read is greater then 7,
the program should print an error message and should stop if the number is
negative.
3. The mean and standard deviation for Nnumbers x1, x2, . . . , xnis defined as
x=PN
i=1 xi
N=x1+x2+x3+. . . +xn
N(2)
σ=v
u
u
tPN
i=1 x2
i−Nx2
N−1(3)
Write a program that iteratively reads positive numbers from the input, and
that prints out the number of elements read, their mean, and standard de-
viation; the sequence should be terminated with a negative number. Try it
with the following sequence of numbers:
1 9 34 23 24 8 7 15 17 19 22 21 16 27.
4. Vieta’s formula calculates the value of 2/π as an infinite product of nested
square roots:
2
π=1
2√21
2q2 + √2
1
2r2 + q2 + √2
1
2s2 + r2 + q2 + √2
. . .
(4)
Write a program that computes the value of πaccording to the formula above.
Include as many terms as necessary so that the change in the accumulated
value is less then 10−5.
5. Modify the trapezoidal program to integrate the following functions: R1
0√xdx,
R2
0sin2πx2dx, and R4
0f(x)dx where f=x2for x < 1 and f=e−(x−1) for
x > 1.
1

Partial preview of the text

Download MSC321 Homework Solutions and more Assignments Earth Sciences in PDF only on Docsity!

MSC321 Homework 2

  1. Write a program that accepts two values x and y and that prints out the following function:

f (x, y) =

    

x + y x ≥ 0 and y ≥ 0 x + y^2 x ≥ 0 and y < 0 x^2 + y x < 0 and y ≥ 0 x^2 + y^2 x < 0 and y < 0

  1. Write a program that iteratively reads an integer between 1 and 7, and prints the corresponding day the of the week. If the value read is greater then 7, the program should print an error message and should stop if the number is negative.
  2. The mean and standard deviation for N numbers x 1 , x 2 ,... , xn is defined as

x =

∑N i=1 xi N

x 1 + x 2 + x 3 +... + xn N

σ =

√√ √√

(∑ N i=1 x 2 i

) − N x^2 N − 1

Write a program that iteratively reads positive numbers from the input, and that prints out the number of elements read, their mean, and standard de- viation; the sequence should be terminated with a negative number. Try it with the following sequence of numbers: 1 9 34 23 24 8 7 15 17 19 22 21 16 27.

  1. Vieta’s formula calculates the value of 2/π as an infinite product of nested square roots:

π

( 1 2

) ( 1 2

√ 2 +

)   1 2

√ 2 +

√ 2 +

 

  1 2

√

2 +

√ 2 +

√ 2 +

 ...

Write a program that computes the value of π according to the formula above. Include as many terms as necessary so that the change in the accumulated value is less then 10−^5.

  1. Modify the trapezoidal program to integrate the following functions:

∫ (^1) 0

∫ xdx, 2 0 sin

(^2) πx (^2) dx, and ∫^4 0 f^ (x)dx^ where^ f^ =^ x (^2) for x < 1 and f = e−(x−1) (^) for x > 1.