Download presentation

Presentation is loading. Please wait.

Published byKierra Dane Modified over 4 years ago

1
2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline some useful problems

2
2003 Prentice Hall, Inc. All rights reserved. 2 Function: power Power iteration Power recursive

3
2003 Prentice Hall, Inc. All rights reserved. Outline 3 #include using std::cout; using std::cin; using std::endl; // function prototype long powerIteration(int, int); long powerRecursive(int, int); int main() { int x, y; long result; cout << "Enter an integer number"<<endl; cin >> x; cout<< "Enter the power you want to raise your number at: " << endl; cin >> y; result = powerIteration(x,y); cout<< x << " to the power "<< y<< " is ((using iterative method)) " << result << endl; cout << endl; result = powerRecursive(x, y); cout<< x << " to the power "<< y<< " is ((using recursive method)) " << result << endl; cout << endl; return 0; }

4
2003 Prentice Hall, Inc. All rights reserved. Outline 4 long powerIteration (int number, int exponent) { int x, y; long result = 1; x = number; y = exponent; if (y==0) return 1 else{ for (int i=1; i<=y; i++) { result = result*x; } return result; }

5
2003 Prentice Hall, Inc. All rights reserved. Outline 5 long powerRecursive (int number, int exponent) { int x, y; long result = 1; x = number; y = exponent; if (y==0){ return 1; } else { result = x*powerRecursive(x, y-1); return result; }

6
2003 Prentice Hall, Inc. All rights reserved. 6 Function: exponential e x = 1 + x/1! + x 2 /2! + x 3 /3! + ….. (Taylor expansion) e = 1 + 1/1! + 1/2! + 1/3! + ….. Where do you stop the summation? –Until the final result is achieved! Result is achieved if the difference between current result and the result from previous iteration is very small (accuracy is user-defined)

7
2003 Prentice Hall, Inc. All rights reserved. Outline 7 #include using std::cout; using std::endl; using std::fixed; #include using std::setprecision; int factorial(int); int main() { int number = 1; int counter = 1; int fact; double e_old, e_new; double err = 1; e_old = 1; while (err >= 1.0e-10) { factorial = fact(number) e_new = e_old + static_cast (1.0)/ factorial; number = number + 1; err = fabs(e_new - e_old); // update error e_old = e_new; } cout<<setprecision(10)<<fixed<<e_new<<endl;; return 0; }

8
2003 Prentice Hall, Inc. All rights reserved. Outline 8 ////////////////////////// // compute fact(counter)// ////////////////////////// int factorial(int nb) { int fact = 1; counter = nb; if (nb == 0)||(nb == 1) return 1; else { while (counter>=1) { fact = fact*counter; counter = counter -1; } return fact; }

9
2003 Prentice Hall, Inc. All rights reserved. 9 Function: sine sin(x)= x – x 3 /3! + x 5 /5! – x 7 /7! + ….. (Taylor expansion)

10
2003 Prentice Hall, Inc. All rights reserved. Outline 10 #include using std::cout; using std::endl; using std::fixed; #include using std::setprecision; int factorial(int); int main() { float number, fact; cout<<"Enter a number: "<<endl; cin>>number; int counter = 1; double err = 1; double sine_old, sine_new; sine_old = number; int sign = -1; while (err >= 1.0e-10){ counter = counter+2; fact = factorial(counter); sine_new = sine_old + sign*pow(number, counter)/fact; sign = sign*(-1); // change sign err = fabs(sine_new - sine_old); // compute the error sine_old = sine_new; // update sine_old } cout<<setprecision(10)<<fixed<<sine_new<<endl;; return 0; }

11
2003 Prentice Hall, Inc. All rights reserved. Outline 11 ////////////////////////// // compute fact(counter)// ////////////////////////// int factorial(int nb) { int fact = 1; counter = nb; if (nb == 0)||(nb == 1) return 1; else { while (counter>=1) { fact = fact*counter; counter = counter -1; } return fact; }

12
2003 Prentice Hall, Inc. All rights reserved. 12 Newton-Raphson method In numerical analysis, Newton's method (or the Newton–Raphson method or the Newton–Fourier method) is an efficient algorithm for finding approximations to the zeros (or roots) of a real- valued function, f(x). Start with an arbitrary value x 0 (the closer to the zero the better) and then define for each natural number n: Newton's method will usually converge provided the initial guess is close enough to the unknown zero

13
2003 Prentice Hall, Inc. All rights reserved. 13 Newton-Raphson method Try to solve for the roots of: f(x) = ax 2 +bx+c –Example: sqrt(9) f(x) = x 2 -9 = 0 (i.e., a = 1, b=0, c = -9) Start with initial guess: –x 0 Compute the new approximation: –x 1 = x 0 – f(x 0 )/f’(x 0 ) Use x 1 to compute the new approximation –x 2 = x 1 – f(x 1 )/f’(x 1 ) Use x n to compute the new approximation –X n+1 = x n – f(x n )/f’(x n ) When do you stop? –When the difference between the two approximations is very small

14
2003 Prentice Hall, Inc. All rights reserved. Outline 14 #include using namespace std; using std::cout; using std::cin; using std::endl; #include void sq_root (float, float, float, float); int main() { float a, b, c, x0; cout << "Enter values for a, b, and c :"; cin >> a >> b >> c; cout << "Enter a guess for the root :"; cin >> x0; sq_root (a, b, c, x0); return 0; }

15
2003 Prentice Hall, Inc. All rights reserved. Outline 15 void sq_root (float a, float b, float c, float guess) { float x1, x0, delta; int iters = 0; x0 = guess; delta = 1.0; // set delta for first iteration while (delta >.00001) { x1 = x0 - (a*x0*x0 + b*x0 + c) / ( 2*a*x0 + b); delta = fabs ( x1 - x0 ); x0 = x1; // new approximation becomes the old approximation // for the next iteration iters++; // count the number of iterations } cout << "One root is " << x1 << endl; cout << "Found in " << iters << " iterations" << endl; }

Similar presentations

OK

2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.

2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google