17
TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++) QUESTION 1 Write a C++ program by using Newton-Raphson method to find the real root near 2 of the equation x 4 – 11x= 8 USING DO-WHILE LOOP(FIRST WAY) #include <iostream> #include <cmath> using namespace std; #define f(x) pow(x,4) - 11*x - 8 #define ff(x) 4*pow(x,3) - 11 void main () { double guess, x1, fguess,ffguess; cout<<"Enter an initial guess of the solution: "; cin>> guess; do { fguess= f(guess); ffguess =ff(guess); x1= guess - (fguess/ ffguess); cout << "\n" <<guess << "\t\t" << x1<< "\t\t" << fguess <<"\t\t" << ffguess << "\n\n"; guess=x1; //new approx. becomes previous and it is approximation for the next iteration

C++ TUTORIAL 7

Embed Size (px)

Citation preview

Page 1: C++ TUTORIAL 7

TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++)

QUESTION 1

Write a C++ program by using Newton-Raphson method to find the real root near 2 of the equation x4 – 11x= 8

USING DO-WHILE LOOP(FIRST WAY)

#include <iostream>

#include <cmath>

using namespace std;

#define f(x) pow(x,4) - 11*x - 8

#define ff(x) 4*pow(x,3) - 11

void main ()

{

double guess, x1, fguess,ffguess;

cout<<"Enter an initial guess of the solution: ";

cin>> guess;

do

{

fguess= f(guess);

ffguess =ff(guess);

x1= guess - (fguess/ ffguess);

cout << "\n" <<guess << "\t\t" << x1<< "\t\t" << fguess <<"\t\t" << ffguess

<< "\n\n";

guess=x1;

//new approx. becomes previous and it is approximation for the next iteration

Page 2: C++ TUTORIAL 7

}while ( fabs(fguess) > 0.00001);

cout<<"The root is "<< x1<<endl;

}

//Output:

Enter an initial guess of the solution: 24.0911

24.0911 18.072 336569 55917.1

18.072 13.5607 106459 23598.1

13.5607 10.1825 33659.1 9963.79

10.1825 7.65875 10630.4 4212.07

7.65875 5.78392 3348.33 1785.94

5.78392 4.41096 1047.53 762.974

4.41096 3.44181 322.039 332.29

3.44181 2.82066 94.4696 152.088

2.82066 2.5125 24.2728 78.7663

2.5125 2.43218 4.21212 52.4422

2.43218 2.42704 0.239179 46.5503

2.42704 2.42702 0.000935689 46.1863

2.42702 2.42702 1.45057e-008 46.1849

The root is 2.42702

Page 3: C++ TUTORIAL 7

TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++)

USING DO-WHILE LOOP(SECOND WAY)

#include <cmath>

#include <iostream>

#include <iomanip>

using namespace std;

double func(double x);

double diff(double x);

double newton(double x);

int main ()

{

double a=2,b,r,iter=1;

cout << "NEWTON-RAPHSON METHOD" << endl;

cout << "x^4-11x-8 [near 2]\n" << endl;

cout << "i\tx\t\tf(x)\t\tf'(x)" <<endl;

do

{

r=newton(a);

cout << setprecision(0) << iter << "\t"<< setprecision(5) << fixed << a << "\t\t"

<< func(a) << "\t\t" << diff(a) << endl;

b=a;

a=r;

iter++;

} while (fabs((a-b)/b) > 0.001);

Page 4: C++ TUTORIAL 7

cout << "\nThe root is " << a << endl;

return 0;

}

double func(double x) {return (double)pow(x,4)-11*x-8;}

double diff(double x) {return (double)4*pow(x,3)-11;}

double newton(double x) {return (double)x-(func(x)/diff(x));}

//Output:

NEWTON-RAPHSON METHOD

x^4-11x-8 [near 2]

i x f(x) f'(x)

1 2.00000 -14.00000 21.00000

2 2.66667 13.23457 64.85185

3 2.46259 1.68798 48.73623

4 2.42796 0.04324 46.25104

The root is 2.42702

Page 5: C++ TUTORIAL 7

TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++)

ALTERNATIVE METHOD FOR QUESTION 1 USING FOR LOOP

#include <iostream>

#include <cmath>

using namespace std;

#define f(x) pow(x,4) - 11*x - 8

#define ff(x) 4*pow(x,3) - 11

void main ()

{

double guess, x1, fguess,ffguess;

cout<<"Enter an initial guess of the solution: ";

cin>> guess;

for(int i=0 ; i<=10; i++)

{

fguess=f(guess);

ffguess =ff(guess);

x1= guess - (fguess/ ffguess);

if (fabs(x1- guess)/guess < 0.00001)

break;

else

guess=x1;

}

cout<<"The root is "<< x1<<endl;

}

Page 6: C++ TUTORIAL 7

//Output:

Enter an initial guess of the solution: 24.0911

The root is 2.42704

QUESTION 2

Write a C++ function program to find the root of the equation f(x)=ex – 3x2 to an accuracy of 5 digits.

USING DO-WHILE LOOP(FIRST WAY)

#include <iostream>

#include <cmath>

using namespace std;

double f( double x)

{

double fx;

fx=exp(x) - 3*pow(x,2);

return fx;

}

double ff(double x)

{

double ffx;

ffx=exp(x) - 6*x;

return ffx;

}

void main ()

{

double guess, x1, fguess,ffguess;

Page 7: C++ TUTORIAL 7

TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++)

cout<<"Enter an initial guess of the solution: ";

cin>> guess;

do

{

fguess= f(guess);

ffguess =ff(guess);

x1= guess - (fguess/ ffguess);

cout << "\n" <<guess << "\t" << x1<< "\t" << fguess <<"\t" << ffguess <<

"\n\n";

guess=x1;

//new approx becomes previous and it is approximation for the next iteration

} while ( fabs(fguess) > 0.000001);

cout<<"The root is "<< x1<<endl;

}

//Output:

Enter an initial guess of the solution: 1

1 0.914155 -0.281718 -3.28172

0.914155 0.910018 -0.0123726 -2.99026

0.910018 0.910008 -3.00348e-005 -2.97574

0.910008 0.910008 -1.79075e-010 -2.9757

The root is 0.910008

Page 8: C++ TUTORIAL 7

USING DO-WHILE LOOP(SECOND WAY)

#include <cmath>

#include <iostream>

#include <iomanip>

using namespace std;

double func(double x);

double diff(double x);

double newton(double x);

int main ()

{

double a=1,b,r;

int iter=1;

cout << "NEWTON-RAPHSON METHOD" << endl;

cout << "e^x-3x^2\n" << endl;

cout << "i\tx\t\tf(x)\t\tf'(x)" <<endl;

do

{

r=newton(a);

cout << iter << "\t"<< setprecision(5) << fixed << a << "\t\t" << func(a) << "\t"

<< diff(a) << endl;

b=a;

a=r;

iter++;

} while (fabs((a-b)/b) > 0.001);

Page 9: C++ TUTORIAL 7

TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++)

cout << "\nThe root is " << a << endl;

return 0;

}

double func(double x) {return (double)exp(x)-3*pow(x,2);}

double diff(double x) {return (double)exp(x)-6*x;}

double newton(double x) {return (double)x-(func(x)/diff(x));}

//Output:

NEWTON-RAPHSON METHOD

e^x-3x^2

i x f(x) f'(x)

1 1.00000 -0.28172 -3.28172

2 0.91416 -0.01237 -2.99026

3 0.91002 -0.00003 -2.97574

The root is 0.91001

QUESTION 3

Write a C++ Function program to find the smallest positive root of the equation x3– 2x – 3 =0. using Successive Approximation Method

#include <cmath>

#include <iostream>

#include <iomanip>

Page 10: C++ TUTORIAL 7

using namespace std;

double f(double x);

double g(double x);

int main ()

{

double xo=0,xn=1;

cout << "SUCCESSIVE APPROXIMATION METHOD" << endl;

cout << "X^3-2X-3\n" << endl;

while (f(xo)*f(xn) > 0)

{

xn=xo;

xo++;

}

while (fabs(xn-xo) > 0.000001)

{

xo=xn;

xn=g(xo);

cout << xo << "\t" << xn << endl;

}

cout << "\nThe root is " << setprecision(6) << xn << endl;

return 0;

}

double f(double x) {return (double)pow(x,3)-2*x-3;}

double g(double x) {return (double)pow(2*x+3,0.333333);}

Page 11: C++ TUTORIAL 7

TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++)

//Output:

SUCCESSIVE APPROXIMATION METHOD

X^3-2X-3

1 1.70998

1.70998 1.85856

1.85856 1.88681

1.88681 1.89208

1.89208 1.89306

1.89306 1.89325

1.89325 1.89328

1.89328 1.89329

1.89329 1.89329

1.89329 1.89329

The root is 1.89329

QUESTION 4

Write a C++ function to find the positive root of the equation cos(x)- 3x +5=0 using Successive Approximation Method

#include <cmath>

#include <iostream>

#include <iomanip>

using namespace std;

double f(double x) {return (double)cos(x)-3*x+5;}

double g(double x) {return (double)(cos(x)+5)/3;}

Page 12: C++ TUTORIAL 7

int main ()

{

double xo=0,xn=1;

cout << "SUCCESSIVE APPROXIMATION METHOD" << endl;

cout << "cos(x)-3x+5\n" << endl;

while (f(xo)*f(xn) > 0)

{

xn=xo;

xo++;

}

while (fabs(xn-xo) > 0.000001)

{

xo=xn;

xn=g(xo);

cout << xo << "\t" << xn << endl;

}

cout << "\nThe root is " << setprecision(6) << xn << endl;

return 0;

}

//Output:

SUCCESSIVE APPROXIMATION METHOD

cos(x)-3x+5

Page 13: C++ TUTORIAL 7

TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++)

1 1.84677

1.84677 1.57584

1.57584 1.66499

1.66499 1.63532

1.63532 1.64517

1.64517 1.6419

1.6419 1.64299

1.64299 1.64262

1.64262 1.64274

1.64274 1.6427

1.6427 1.64272

1.64272 1.64271

1.64271 1.64271

1.64271 1.64271

The root is 1.64271

QUESTION 5

Write a C++ program to find the root of the equation sin(x) + 3cos(x) =2 using Secant method. Use initial approximations 0 and 1.5 #include <cmath>

#include <iostream>

#include <iomanip>

using namespace std;

double f(double x)

{

Page 14: C++ TUTORIAL 7

return (double)sin(x)+3*cos(x)-2;

}

double secant(double x0, double x1)

{

return (double)((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0));

}

int main ()

{

double x0,x1=0,x2=1.5;

cout << "SECANT METHOD" << endl;

cout << "sin(x)+3cos(x)-2\n" << endl;

do

{

x0=x1;

x1=x2;

x2=secant(x0,x1);

cout << setprecision(6) << fixed << x0 << "\t" << x1 << "\t" << x2 << endl;

}while (fabs(x2-x1) > 0.00001);

cout << "\nThe root is " << setprecision(5) << fixed << x2 << endl;

return 0;

}

Page 15: C++ TUTORIAL 7

TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++)

//Output:

SECANT METHOD

sin(x)+3cos(x)-2

0.000000 1.500000 0.837851

1.500000 0.837851 1.160351

0.837851 1.160351 1.218120

1.160351 1.218120 1.207622

1.218120 1.207622 1.207827

1.207622 1.207827 1.207828

The root is 1.20783

QUESTION 6

Use Secant method to find the real root of the equation x3- 8x =5

#include <cmath>

#include <iostream>

#include <iomanip>

using namespace std;

double f(double x)

{

return (double)pow(x,3)-8*x-5;

}

Page 16: C++ TUTORIAL 7

double secant(double x0, double x1)

{

return (double)((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0));

}

void main ()

{

double x0,x1=0,x2=1;

cout << "SECANT METHOD" << endl;

cout << "x^3-8x-5\n" << endl;

while (f(x1)*f(x2) > 0)

{

x1=x2;

x2++;

}

do

{

x0=x1;

x1=x2;

x2=secant(x0,x1);

cout << setprecision(6) << fixed << x0 << "\t" << x1 << "\t" << x2 << endl;

}

while (fabs(x2-x1) > 0.00001);

cout << "\nThe root is " << setprecision(5) << fixed << x2 << endl;

}

Page 17: C++ TUTORIAL 7

TUTORIAL 7 SJEM2231: STRUCTURED PROGRAMMING (C++)

//Output:

SECANT METHOD

x^3-8x-5

3.000000 4.000000 3.068966

4.000000 3.068966 3.090738

3.068966 3.090738 3.100570

3.090738 3.100570 3.100431

3.100570 3.100431 3.100432

The root is 3.10043