Referencing Styles : Harvard
The square root of a value V can be found using either one of the
following iterative formulas3
:
Formula (A) Formula (B)
2 1
1
1
2
V
r r
r
= +
2
1 1
2 2
1
( 3)
3
rr V
r
r V
+ = +
where r1 is the previously estimated root.
Your task is to find out which of the above formulas is better for
general use--by performing a large number of numerical
experiments. For this purpose:
a) Write the function SqrtA(float V, float r1, float Error) that
computes and returns the square root of V, using Formula (A),
accurate to within ±Error. This function also counts the number of
required iterations and saves it in the global variable KA.
b) Write the function SqrtB(float V, float r1, float Error) that
computes and returns, using Formula (B), the square root of V
accurate to within ±Error. This function also counts the number of
required iterations and saves it in the global variable KB.
c) Write the function main() that generates 1000 random values of
V = 1e6*rand()*rand(), and for each such value V, prints its
square roots found by SqrtA(V, V/2, 1e-6) and SqrtB(V, V/2, 1e-
6) as well as the number of iterations used by the two functions.
The function main() should also compute the average number of
iterations used by formulas (A) and (B).
In the summary section: Show the program listing and present
the average number of iterations used by (A) and (B), and your
recommendation on which formula is better for general use. Detailed
output is to be placed in the Appendix.
3 See Exercise 2.2.6 in Chapter 2BCEE 231 Homework problems P2 P.2.3
P2.2 (25 marks)
Write the program to plot the following functions and compute the
areas under the curve using integration. Notes: (i) Use compound logical
expressions to form the functions; (ii) Set the global variable iter# = 300 for better plot
accuracy; (iii) To clear the plot use the built-in function clearplot(); (iv) Use function integ() for
integration. Most accurate results are obtained when integ() is used to integrate the function
over each interval.
(a)
− + − < ≤
− ≤ ≤
=
60.75( 3) for 3 9
2
for 0 3
2 ( ) 3
3
x x
x
x
x
g x
(b)
− − < ≤
− − + − − < ≤
− + < ≤
≤ ≤
=
(40 ) for 30 40
(40 ) 54(40 ) 540 for 20 30
14 600 for 10 20
46 for 0 10
( )
2
2
x x
x x x
x x
x x
v x
Present the plots and the computed integrals in the Summary.
P2.3 (25 marks)
Given a 2-D array A having M rows and N columns, the mean value
and standard deviation of the elements of A can be computed using
the following formulas:
The mean value: 1 1
1 with
M N
ij
i j
A K MN
K µ = =
= ∑∑ =
The standard deviation:
( )
1/2 1/2
2 2 2
1 1 1 1
1 1 M N M N
ij ij
i j i j
A A
K K σµ µ = = = =
= −= − ∑∑ ∑∑
The function main() in the following program generates arrays A of
random values normally distributed with zero mean and unit
standard deviation. Your task is to complete the missing parts of
functions MeanArray( ) and StdvArray( ).BCEE 231 Homework problems P2 P.2.4
main()
{
float M, N, K;
for(M = 100; M <= 200; M = M+50)
{
for(N = 200; N <= 400; N = N+50)
{ // Fill array A with random values
randnormfill(A[M,N], 1);
Mean = MeanArray(A, M, N);
Stdv = StdvArray(A, Mean, M, N);
print(^, K = M*N, Mean, Stdv);
}
}
}
MeanArray(mat A, float M, float N)
{ // Compute and return the mean value of
// the elements in array A
. . . . .
. . . . .
}
StdvArray(mat A, float Mean, float M, float N)
{ // Compute and return the standard deviation
// of the elements in array A
. . . . .
. . . . .
}
Summary section: Show the program listing, output and relevant
observations/conclusions.
P2.4 (25 marks)
(a) Write two user-defined functions that use nested for-loops for the
following tasks:
Function AddMatrix(mat A, mat B, mat C) to add 2 matrices:
C A+B
The function should test if matrices A, B and C have equal
order. Note: Use functions nrow() & ncol() to get the size of the matrices.
Function Max(mat A) that returns the largest element (in
magnitude) of matrix A.BCEE 231 Homework problems P2 P.2.5
(b) Write the main() function that:
uses AddMatrix() to sum the following 5 matrices:
− −
−
− −
−
−
−
−
−
3 9 3
1 12 4
3 0 4
2 3 5
4 2 7
0 6 2
5 2 3
1 2 1
2 4 5
3 9 0
uses Max() to find and sum the largest elements of the above
5 matrices.
Show the program and the two sums in the Summary.