Functions and Recursion 1 some useful problems
2 Function: power Power iteration Power recursive
#include <iostream> #include <cmath> 3 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;
long poweriteration (int number, int exponent) int x, y; long result = 1; x = number; y = exponent; 4 if (y==0) return 1 else for (int i=1; i<=y; i++) result = result*x; return result;
long powerrecursive (int number, int exponent) int x, y; long result = 1; x = number; y = exponent; 5 if (y==0) return 1; else result = x*powerrecursive(x, y-1); return result;
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)
#include <iostream> #include <math.h> using std::cout; using std::endl; using std::fixed; #include <iomanip> 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<double>(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; 7
////////////////////////// // 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; 8
9 Function: sine sin(x)= x x 3 /3! + x 5 /5! x 7 /7! +.. (Taylor expansion)
#include <iostream> #include <math.h> using std::cout; using std::endl; using std::fixed; #include <iomanip> 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; 10
////////////////////////// // 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; 11
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 realvalued 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 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
#include <iostream> using namespace std; 14 using std::cout; using std::cin; using std::endl; #include <cmath> 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;
void sq_root (float a, float b, float c, float guess) float x1, x0, delta; int iters = 0; x0 = guess; 15 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;