0% found this document useful (0 votes)
84 views4 pages

Examples Sheet 2 PDF

This document provides examples of using functions and loops in C++ to solve mathematical problems. It includes examples of using Newton's method to find roots of polynomials, writing functions to calculate trigonometric functions like sine using power series, and writing functions to compute Fibonacci numbers, solve quadratic equations, and evaluate mathematical expressions. It also provides pointers examples like creating arrays dynamically and passing arguments by reference.
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)
84 views4 pages

Examples Sheet 2 PDF

This document provides examples of using functions and loops in C++ to solve mathematical problems. It includes examples of using Newton's method to find roots of polynomials, writing functions to calculate trigonometric functions like sine using power series, and writing functions to compute Fibonacci numbers, solve quadratic equations, and evaluate mathematical expressions. It also provides pointers examples like creating arrays dynamically and passing arguments by reference.
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/ 4

MATH49111/MATH69111

Examples Sheet 2

Example: Newton’s root finding algorithm


Use Newton’s method to find roots of f (x) = 0 where

f (x) = 9x4 − 42x3 − 1040x2 + 5082x − 5929.

Solution:
Create a function to find the root of
// f u n c t i o n t o f i n d r o o t o f
double p o l y ( double x ) {
return ( ( ( 9 . ∗ x − 4 2 . ) ∗ x − 1 0 4 0 . ) ∗ x + 5 0 8 2 . ) ∗ x −5929;
}

its derivative
// d e r i v a t i v e f u n c t i o n t o f i n d r o o t o f
double p o l y d e r i v ( double x ) {
return ( ( 3 6 . ∗ x − 1 2 6 . ) ∗ x − 2 0 8 0 . ) ∗ x + 5 0 8 2 . ;
}

and a simple root finding function defined by:


// f i n d t h e r o o t o f a f u n c t i o n
// On i n p u t : x i s i n i t i a l g u e s s , On r e t u r n x i s t h e r o o t
// m a x i t e r i s t h e max no o f i t s , t o l i s t h e a c c u r a c y o f t h e r o o t
// 0 w i l l be r e t u r n e d i f no r o o t i s found
// 1 i f i t i s s u c c e s s f u l
int f i n d r o o t ( double& x , int maxiter , double t o l ) {
// f i n d r o o t
}

The algorithm to find the root is given by:

f (xn )
xn+1 = xn −
f 0 (xn )

so we may write the code inside the function as

1
// f i n d r o o t
int i t e r ;
f o r ( i t e r =0; i t e r <m a x i t e r ; i t e r ++){
x = x − poly ( x )/ p o l y d e r i v ( x ) ;
i f ( s t d : : abs ( p o l y ( x))< t o l ) break ;
}
i f ( i t e r==m a x i t e r ) return 0 ;
e l s e return 1 ;

Example use of the program is


int i t e r =100;
double x , t o l =1. e −6;
s t d : : c o u t << ” Enter i n i t i a l g u e s s \n” ;
s t d : : c i n >> x ;
i f ( find root (x , iter , tol ))
s t d : : c o u t << ” There i s a r o o t a t ” << x << ”\n” ;
else
s t d : : c o u t << ” No r o o t found . \ n” ;

Functions and Loops


2.1 Repeat the root finding algorithm using the Secant method:
f (xn−1 )(xn−1 − xn−2 )
xn = xn−1 −
f (xn−1 ) − f (xn−2 )

2.2 Write a function to compute the value of sin(x) using the first N terms of the power
series expansion (N should be an argument to the function):
N
X x2k+1
sin(x) ≈ (−1)k
k=0
(2k + 1)!

How many terms of the series are required to agree with the built-in sin function.
Produce output of the two functions side by side for the values 0, π/6, π/4, π/2, 2π/3
and π.
2.3 Rewrite the above function for sin so that terms are added until the result is accurate
to 10 decimal places. Think about what condition is satified for this to happen – i.e.
what is the truncation error?
2.4 Write a function that takes the arguments a, b and c, computes and then displays
the roots of the quadratic equation
ax2 + bx + c = 0.
You will need to identify whether the roots are real or complex. If the roots are
complex, display the results in the form A + Bi.

2
2.5 Write functions to evaluate the following expressions.
  12 
1+γ
(i) f (γ) = 2 tan−1 1−γ
sin γ
2
(ii) g(x) = √1

log(x3 + 1)e−x

Using x = 0.14, a = 0.235 and b = 1.763, evaluate y = f (g(x)) and y = g(f (x + a) + g(b)).

2.6 Write a function to calculate the nth Fibonacci number. Recall that the Fibonacci
numbers, fn , are defined by

f0 = 0, f1 = 1, fn = fn−1 + fn−2

Use your function to calculate and display the Fibonacci quotient, qn = fn /fn−1 , for
a number of different values
√ of n. You should find that as n increases qn converges to
the golden mean, (1 + 5)/2 ≈ 1.618.

2.7 The recurrence relation


1 19
p1 = , pn = pn−1 − 2pn−2 , (n ≥ 2)
p0 = 1,
3 3
n
has the unique solution pn = 13 , n ≥ 0.


Write a C++ program to compute the terms n = 0 up to n = 20 of the series using


the data type float to store variables. Output the results to a file with columns
showing the following quantities:

n, pn (analytical), pn (numerical), relative error, absolute error

2.8 Run your program again this time using double. Is there any difference between the
results? Can you think of a reason why the results are different?

Pointers
2.9 Create an integer array of dimension 10. Print out the memory addresses of each
element in the array. What do you notice?

2.10 Create two versions of a function that returns the cube of a floating point number.
Pass the variable itself in one version and the pointer to the variable in the other. If
you overwrite the variable in the function does it matter? If so, why?

2.11 Write a function to swap two numbers.

2.12 Write a function that calculates the maximum value of an array of numbers.

3
2.13 Write a function passing x as an argument. Try declaring (in the argument list) as

(i) double x
(ii) double& x
(iii) const double x
(iv) const double& x

What happens when you try to assign x a new value inside the function?

2.14 Use a pointer to create a new double array with n elements (where n is specified
somewhere else in the code):
double ∗ a r r a y ;
a r r a y = new double [ n ]

Assign the values 0 through to 99 to the elements. What happen if you write the
code:
s t d : : c o u t << ” an e l e m e n t o u t s i d e t h e a r r a y ” << a r r a y [ 2 0 0 ] ;

Don’t forget to delete the storage when you’re finished


delete a r r a y [ ] ;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy