23
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6 QUESTION 1 Write a C++ program to find a root of the equation x 3 – 4x – 8.95 = 0 accurate to three decimal places using the Bisection method in the interval (2, 3). #include<iostream> #include<cmath> #include<iomanip> using namespace std; void main() { double x1=2, x2=3, x3, fx1, fx2,fx3; cout << "x1=\t" << "|x2=\t"<<"|x3=\t" << "|fx1=\t\t" << "|fx2=\t\t" << "|fx3=\t" << endl; do { x3 = (x1 + x2)/2; fx1= pow(x1,3) - 4*x1 - 8.95; fx2= pow(x2,3) - 4*x2 - 8.95; fx3= pow(x3,3) - 4*x3 - 8.95; cout << x1 << "\t" <<x2 << "\t" <<x3 <<"\t" << fx1 <<"\t\t" << fx2 << "\t\t" <<fx3 <<endl; if(fx1*fx3 <0) { x2=x3; } else { x1=x3;

C++ TUTORIAL 6

Embed Size (px)

Citation preview

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6

QUESTION 1

Write a C++ program to find a root of the equation x3 – 4x – 8.95 = 0 accurate to

three decimal places using the Bisection method in the interval (2, 3).

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

void main()

{

double x1=2, x2=3, x3, fx1, fx2,fx3;

cout << "x1=\t" << "|x2=\t"<<"|x3=\t" << "|fx1=\t\t" << "|fx2=\t\t" << "|fx3=\t" << endl;

do

{

x3 = (x1 + x2)/2;

fx1= pow(x1,3) - 4*x1 - 8.95;

fx2= pow(x2,3) - 4*x2 - 8.95;

fx3= pow(x3,3) - 4*x3 - 8.95;

cout << x1 << "\t" <<x2 << "\t" <<x3 <<"\t" << fx1 <<"\t\t" << fx2 << "\t\t" <<fx3 <<endl;

if(fx1*fx3 <0)

{

x2=x3;

}

else

{

x1=x3;

}

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

cout << "\nThe root is= " << setprecision(3) <<fixed << x3 <<endl;

}

// Output:

x1= |x2= |x3= |fx1= |fx2= |fx3=

2 3 2.5 -8.95 6.05 -3.325

2.5 3 2.75 -3.325 6.05 0.846875

2.5 2.75 2.625 -3.325 0.846875 -1.36211

2.625 2.75 2.6875 -1.36211 0.846875 -0.289111

2.6875 2.75 2.71875 -0.289111 0.846875 0.270917

2.6875 2.71875 2.70313 -0.289111 0.270917 -0.0110771

2.70313 2.71875 2.71094 -0.0110771 0.270917 0.129423

2.70313 2.71094 2.70703 -0.0110771 0.129423 0.0590492

2.70313 2.70703 2.70508 -0.0110771 0.0590492 0.0239551

2.70313 2.70508 2.7041 -0.0110771 0.0239551 0.00643126

2.70313 2.7041 2.70361 -0.0110771 0.00643126 -0.00232486

2.70361 2.7041 2.70386 -0.00232486 0.00643126 0.00205271

2.70361 2.70386 2.70374 -0.00232486 0.00205271 -0.000136197

2.70374 2.70386 2.7038 -0.000136197 0.00205271 0.000958227

2.70374 2.7038 2.70377 -0.000136197 0.000958227 0.000411007

2.70374 2.70377 2.70375 -0.000136197 0.000411007 0.000137403

2.70374 2.70375 2.70374 -0.000136197 0.000137403 6.02383e-007

2.70374 2.70374 2.70374 -0.000136197 6.02383e-007 -6.77976e-005

2.70374 2.70374 2.70374 -6.77976e-005 6.02383e-007 -3.35976e-005

2.70374 2.70374 2.70374 -3.35976e-005 6.02383e-007 -1.64976e-005

The root is= 2.704 (3 decimal places)

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6

QUESTION 2

Write a C++ program to find a root of the equation ex – 3x = 0 using the Bisection method.

#include <iostream>

#include<cmath>

using namespace std;

void main()

{

int n, count=1;

double x0,x1,x2,fx0,fx1,fx2;

cout <<"\nSolution by BISECTION method \n";

cout <<"\nThe equation is: exp(x) - 3*x= 0\n\n";

cout << "Enter the number of iterations: ";

cin >> n;

/*Finding an Approximate ROOT of Given Equation, Having +ve Value*/

for(x2=1;;x2++)

{

fx2= exp(x2) - 3*x2;

if (fx2 > 0)

{

break;

}

}

/*Finding an Approximate ROOT of Given Equation, Having -ve Value*/

for(x1=x2-1; ; x2--)

{

fx1= exp(x1) - 3*x1;

if( fx1 <0)

{

break;

}

}

//Printing Result

cout << "\t\t-----------------------------------------";

cout <<"\n\t\t ITERATIONS\t\t ROOTS\n";

cout <<"\t\t-----------------------------------------";

for(;count<=n;count++)

{

x0=(x1+x2)/2;

fx0 = exp(x0) - 3*x0;

if( fx0 ==0)

{

cout << x0;

}

else if(fx0*fx1<0)

{

x2=x0;

}

else

{

x1=x0;

fx1=fx0;

}

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6

cout <<"\n\t\tITERATION " << count;

cout <<":\t\t" << x0;

if(fabs((x1-x2)) < 0.000001)

{

cout <<"\n\t\t---------------------------------";

cout <<"\n\tRoot = "<< x0;

cout << "\n\tIterations = " << count <<endl;

cout << "\t\t---------------------------------";

}

}

cout << "\n\t\t----------------------------------------";

cout << "\n\t\tRoot = " << x0;

cout << "\n\t\tIterations = " << count-1<<endl;

cout << "\t\t----------------------------------------" << endl;

}

//Output:

Solution by BISECTION method

The equation is: exp(x) - 3*x= 0

Enter the number of iterations: 19

-----------------------------------------

ITERATIONS ROOTS

-----------------------------------------

ITERATION 1: 1.5

ITERATION 2: 1.75

ITERATION 3: 1.625

ITERATION 4: 1.5625

ITERATION 5: 1.53125

ITERATION 6: 1.51563

ITERATION 7: 1.50781

ITERATION 8: 1.51172

ITERATION 9: 1.51367

ITERATION 10: 1.5127

ITERATION 11: 1.51221

ITERATION 12: 1.51196

ITERATION 13: 1.51208

ITERATION 14: 1.51215

ITERATION 15: 1.51212

ITERATION 16: 1.51213

ITERATION 17: 1.51214

ITERATION 18: 1.51213

ITERATION 19: 1.51213

----------------------------------------

Root = 1.51213

Iterations = 19

----------------------------------------

QUESTION 3 Write a C++ function program to find the roof of f(x) = x6 – x – 1 = 0 accurate to within ξ = 0.001.

Use the Bisection method.

#include <iostream>

#include<cmath>

using namespace std;

#define EPS 0.001

#define f(x) pow(x,6) - x - 1

//global variables

int count=1,n;

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6

void Bisect()

{

double x0,x1,x2,a,b,c;

/*Finding an Approximate ROOT of Given Equation, having +ve Value*/

for(x2=1;;x2++)

{

c=f(x2);

if (c > 0)

{

break;

}

}

/*Finding an Approximate ROOT of Given Equation, Having -ve Value*/

for(x1=x2-1;;x2--)

{

b=f(x1);

if(b <0)

{

break;

}

}

//Printing Result

cout << "\t\t-----------------------------------------";

cout <<"\n\t\t ITERATIONS\t\t ROOTS\n";

cout <<"\t\t-----------------------------------------";

for( ; count<=n; count++)

{

x0=(x1+x2)/2;

a =f(x0);

if(a ==0)

{

x0;

}

else if(a*b<0)

{

x2=x0;

}

else

{

x1=x0;

b=a;

}

cout <<"\n\t\tITERATION " << count;

cout <<":\t\t" << x0;

if(fabs((x1-x2)) < EPS)

{

cout <<"\n\t\t----------------------------------";

cout <<"\n\tRoot = "<< x0;

cout << "\n\tIterations = " << count <<endl;

cout << "\t\t----------------------------------";

}

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6

}

cout << "\n\t\t----------------------------------------";

cout << "\n\t\tRoot = " << x0;

cout << "\n\t\tIterations = " << count-1<<endl;

cout << "\t\t----------------------------------------" << endl;

}

void main()

{

cout <<"\nSolution by BISECTION method \n";

cout <<"\nThe equation is: x^6- x -1= 0\n\n";

cout << "Enter the number of iterations: ";

cin >> n;

Bisect();

}

//Output:

Solution by BISECTION method

The equation is: x^6- x -1= 0

Enter the number of iterations: 20

-----------------------------------------

ITERATIONS ROOTS

-----------------------------------------

ITERATION 1: 1.5

ITERATION 2: 1.25

ITERATION 3: 1.125

ITERATION 4: 1.1875

ITERATION 5: 1.15625

ITERATION 6: 1.14063

ITERATION 7: 1.13281

ITERATION 8: 1.13672

ITERATION 9: 1.13477

ITERATION 10: 1.13379

----------------------------------

Root = 1.13379

Iterations = 10

----------------------------------

ITERATION 11: 1.13428

----------------------------------

Root = 1.13428

Iterations = 11

----------------------------------

ITERATION 12: 1.13452

----------------------------------

Root = 1.13452

Iterations = 12

----------------------------------

ITERATION 13: 1.13464

----------------------------------

Root = 1.13464

Iterations = 13

----------------------------------

ITERATION 14: 1.1347

----------------------------------

Root = 1.1347

Iterations = 14

----------------------------------

ITERATION 15: 1.13474

----------------------------------

Root = 1.13474

Iterations = 15

----------------------------------

ITERATION 16: 1.13472

----------------------------------

Root = 1.13472

Iterations = 16

----------------------------------

ITERATION 17: 1.13473

----------------------------------

Root = 1.13473

Iterations = 17

----------------------------------

ITERATION 18: 1.13472

---------------------------------- Root = 1.13472

Iterations = 18

----------------------------------

ITERATION 19: 1.13473

----------------------------------

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6

Root = 1.13473

Iterations = 19

----------------------------------

ITERATION 20: 1.13472

----------------------------------

Root = 1.13472

Iterations = 20

----------------------------------

----------------------------------------

Root = 1.13472

Iterations = 20

----------------------------------------

QUESTION 4

Write a C++ Function program to find the smallest positive root of the cubic

equation ax3+bx2+cx+d=0 using Bisection method. You can take error tolerance

of 10-6

#include <iostream>

#include <cmath>

using namespace std;

#define f(x) (a*pow(x,3)+b*pow(x,2)+c*x+d)

double Bisection(double a, double b, double c, double d)

{

double x1, x2;

double mid_x;

x1 = 0;

x2 = 1; // We need this (in other case x2 is random value)

mid_x = 0.5; // initialize all variables before use

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

{

x1 = x2;

x2 = x1 + 1;

if (x2 > 1000)

return -1.0; // to prevent overflow

}

for(int i = 1; i < 100; i++)

{

mid_x = (x1 + x2) / 2.0;

if(f(x1) * f(mid_x) < 0)

{

x2 = mid_x;

}

else

{

x1 = mid_x;

}

if(fabs(x1 - x2) < 0.0000001)

{

return (x1 + x2) / 2; // will return immediately

}

}

return mid_x; // return anything. Code can to bounce between two numbers

}

int main()

{

double a, b, c, d, h;

ULANG:

cout << "Enter the value of constants for the cubic equation: " << endl;

cin >> a>> b>> c>> d;

if(a==0)

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6

{

cout << "The value for constant a cannot equal to 0.Try again:\n" << endl;

goto ULANG;

}

h = Bisection(a, b, c, d);

if (h >= 0)

cout << "The smallest +ve root of the cubic equation using Bisection method is: "

<< h << endl;

else

cout << "The root is not found in range from 0 to 1000" << endl;

return 0;

}

//Output:

Enter the value of constants for the cubic equation:

0 1 6 -1

The value for constant a cannot equal to 0.Try again:

Enter the value of constants for the cubic equation:

6 2 0 -1

The smallest +ve root of the cubic equation using Bisection method is: 0.45872

/******ALTERNATIVE METHOD FOR QUESTION 4******/

Done by FAIZ MATHS STUDENT, UM (trust me, he is so genius)

#include <cmath>

#include <iostream>

#include <iomanip>

using namespace std;

void bisec(double a, double b);

void choosepositive(int& x, int& y);

void choosenegative(int& x, int& y);

void zero(int z);

double f(double x);

double mid(double x,double y);

double p,q,r,s;

int main()

{

int z,l=0,n=1,m=0,o=-1,e,g,t=0,u=0,h;

cout << "BISECTION METHOD [CUBIC EQUATION]\n\nEnter ax^3+bx^2+c+d\n" << endl;

cin >> p >> q >> r >> s;

do

{

do

{

zero(m);

cout << "\nChoose:\n" << "1. +ve root\n" << "2. -ve root\n" << endl;

cin >> z;

if (z==1)

{

choosepositive(l,n);e=n;g=n+1;

if (f(l)*f(n) == 0 && f(n)!=0) {cout << "NO MORE REAL +VE ROOT" <<

endl;break;}

if (n==100){cout<<"NO MORE REAL +VE ROOT\n";break;}

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6

bisec(l,n);break;

}

else if (z==2)

{

choosenegative(m,o);t=o;u=o-1;

if (f(m)*f(o) == 0 && f(o)!=0) {cout << "NO MORE REAL -VE ROOT" <<

endl;break;}

if (o==-100){cout<<"NO MORE REAL -VE ROOT\n";break;}

bisec(m,o);break;

}

}

while (z!=1 || z!=2);

cout << "Want Another root?\n1. YES\n2. NO (EXIT)\n" << endl;

cin >> h;l=e;n=g;m=t;o=u;

}

while (h==1);

return 0;

}

void bisec(double a, double b)

{

double n=1,r;

if (f(b)==0) {cout << "\nThe root is " << b <<endl;}

else

{

r=mid(a,b);

cout << "n a\t\tb\t\tx\t\tb-r\t\tf(x)" <<endl;

do

{

cout << setprecision(0) << n << " "<< setprecision(6) << fixed << a << "\t"

<< b << "\t" << r << "\t" << b-r << "\t" << f(r) << endl;

if (f(a)*f(r) < 0) b=r; else if (f(r)*f(b) < 0) a=r; else if (f(r)==0) break;

r=mid(a,b);n++;

}

while (abs(b-r) > 0.000001 );

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

}

}

void choosepositive(int& x, int& y) { while (f(x)*f(y) > 0 && y<100) {x=y;y++;} }

void choosenegative(int& x, int& y) { while (f(x)*f(y) > 0 && y>-100) {x=y;y--;} }

void zero(int z) {if (f(0)==0) {cout << "\nThe root is 0"<< endl;}}

double f(double x) { return (double) p*pow(x,3)+q*pow(x,2)+r*x+s;}

double mid(double x,double y) {return (double)(x+y)/2;}

//Output:

BISECTION METHOD [CUBIC EQUATION]

Enter ax^3+bx^2+c+d

1 2 3 4

Choose:

1. +ve root

2. -ve root

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6

1

NO MORE REAL +VE ROOT

Want Another root?

1. YES

2. NO (EXIT)

1

Choose:

1. +ve root

2. -ve root

2

n a b x b-r f(x)

1 -1.000000 -2.000000 -1.500000 -0.500000 0.625000

The root is -1.750000

Want Another root?

1. YES

2. NO (EXIT)

2

QUESTION 5

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

of 5 digits in the interval 0.5 and 1.0 using the false position method

#include<iostream>

#include<cmath>

using namespace std;

void main()

{

double x1=0.5,x2=1.0, x3, fx1, fx2,fx3;

cout << "x1=\t" << "|x2=\t\t"<<"|x3=\t\t" << "|fx1=\t\t" << "|fx2=\t\t" << "|fx3=\t" << endl;

do

{

fx1= exp(x1) - 3*pow(x1,2);

fx2= exp(x2) - 3*pow(x2,2);

x3 = (x1*fx2 -x2*fx1)/(fx2-fx1);

fx3= exp(x3) - 3*pow(x3,2);

cout << x1 << "\t" <<x2 << "\t" <<x3 <<"\t" << fx1 <<"\t\t" << fx2 << "\t\t" <<fx3 <<endl;

if(fx1*fx3 <0)

{

x2=x3;

}

else

{

x1=x3;

}

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

cout << "\nThe root is= " << x3 <<endl;

}

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6

//Output:

x1= |x2= |x3= |fx1= |fx2= |fx3=

0.5 1 0.880672 0.898721 -0.281718 0.0857699

0.880672 1 0.908523 0.0857699 -0.281718 0.0044143

0.908523 1 0.909934 0.0044143 -0.281718 0.000218671

0.909934 1 0.910004 0.000218671 -0.281718 1.08115e-005

0.910004 1 0.910007 1.08115e-005 -0.281718 5.34494e-007

0.910007 1 0.910008 5.34494e-007 -0.281718 2.64238e-008

0.910008 1 0.910008 2.64238e-008 -0.281718 1.30632e-009

0.910008 1 0.910008 1.30632e-009 -0.281718 6.45808e-011

0.910008 1 0.910008 6.45808e-011 -0.281718 3.19211e-012

0.910008 1 0.910008 3.19211e-012 -0.281718 1.58096e-013

0.910008 1 0.910008 1.58096e-013 -0.281718 7.10543e-015

0.910008 1 0.910008 7.10543e-015 -0.281718 1.33227e-015

0.910008 1 0.910008 1.33227e-015 -0.281718 -4.44089e-016

The root is= 0.910008

QUESTION 6

Write a C++ function program to find a root of cos x – 3x + 5 = 0 using the false

Position method.

#include<iostream>

#include<cmath>

using namespace std;

double f(double x)

{

double fx;

fx= cos(x) - 3*x +5;;

return fx;

}

void main()

{

double x1, x2,x3;

cout << "Enter x1: ";

cin >> x1;

cout << "Enter x2: ";

cin >> x2;

cout << "x1=\t" << "|x2=\t\t"<<"|x3=\t\t" << "|fx1=\t\t" << "|fx2=\t\t" << "|fx3=\t" << endl;

do

{

x3 = (x1*f(x2) -x2*f(x1))/(f(x2)-f(x1));

cout << x1 << "\t" <<x2 << "\t" <<x3 <<"\t" << f(x1) <<"\t\t" << f(x2) << "\t\t" <<f(x3) <<endl;

if(f(x1)*f(x3) <0)

{

x2=x3;

}

else

{

x1=x3;

}

} while ( fabs(x1 - x2) > 0.000001); //the epsilon value can be changed

/***The function is like a straight line that regular falsi can extrapolate to a root even from far

away, such as from (10, 100). But it is not a straight line, so it cannot converge with that much

precision. To avoid this, we can try dropping our precision requirements by order of magnitude

and it will work ****/

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6

cout << "\nThe root is= " << x3 <<endl;

}

//Output:

Output 1:

Enter x1: 1

Enter x2: 2

x1= |x2= |x3= |fx1= |fx2= |fx3=

1 2 1.64207 2.5403 -1.41615 0.00259181

1.64207 2 1.64272 0.00259181 -1.41615 -2.20673e-005

1.64207 1.64272 1.64271 0.00259181 -2.20673e-005 -1.28211e-010

1.64207 1.64271 1.64271 0.00259181 -1.28211e-010 0

The root is= 1.64271

Output 2:

Enter x1: -10000.234

Enter x2: 100000.3452

x1= |x2= |x3= |fx1= |fx2= |fx3=

-10000.2 100000 1.37861 30004.8 -299997 1.05518

1.37861 100000 1.73033 1.05518 -299997 -0.349866

1.37861 1.73033 1.64275 1.05518 -0.349866 -0.000151465

1.37861 1.64275 1.64271 1.05518 -0.000151465 8.03515e-008

1.64271 1.64275 1.64271 8.03515e-008 -0.00015146 -2.75335e-014

The root is= 1.64271

QUESTION 7

Find a real root of the equation x4 – 11x + 8 = 0 accurate to four decimal places

using regular falsi method.

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

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

double FALSI(double a, double b, double c, double d,double e)

{

double x1, x2;

double mid_x;

x1 = 0;

x2 = 1; // need this!! in other case x2 is random value!

mid_x = 0.5; // initialize all variables before use

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

{

x1 = x2;

x2 = x1 + 1;

if (x2 > 1000)

return -1.0; // to prevent overflow

}

for(int i = 1; i < 100; i++)

{

mid_x = (x1*f(x2) -x2*f(x1))/(f(x2)-f(x1));

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6

if(f(x1) * f(mid_x) < 0)

{

x2 = mid_x;

}

else

{

x1 = mid_x;

}

if(fabs(x1 - x2) < 0.0000001)

{

return (x1 + x2) / 2; // will return immediately

}

}

return mid_x; // return anything. Code can to bounce between two numbers

}

int main()

{

double h;

h = FALSI(1, 0, 0,-11,8);

cout << "The real root of the equation by using Falsi method is: " << setprecision(4)

<<fixed<< h << endl;

return 0;

}

//Output:

The real root of the equation by using Falsi method is: 0.7571 (4 decimal places)