C++ TUTORIAL 8

23
SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8 QUESTION 1 Write a C++ program to evaluate using Trapezoidal rule up to three significant figures. #include <cmath> #include <iostream> #include <iomanip> using namespace std; float f(float x) { float func; func = exp(x); return func; } void main() { float a,b,h,sum=0, Trap; int i, n; cout <<"Enter the lower limit a : "; cin >> a; cout << "Enter the upper limit b: "; cin >> b; cout << "Enter the number of subintervals n: "; cin >> n; h=(b-a)/n;

Transcript of C++ TUTORIAL 8

Page 1: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

QUESTION 1

Write a C++ program to evaluate∫

using Trapezoidal rule up

to three significant figures.

#include <cmath>

#include <iostream>

#include <iomanip>

using namespace std;

float f(float x)

{

float func;

func = exp(x);

return func;

}

void main()

{

float a,b,h,sum=0, Trap;

int i, n;

cout <<"Enter the lower limit a : ";

cin >> a;

cout << "Enter the upper limit b: ";

cin >> b;

cout << "Enter the number of subintervals n: ";

cin >> n;

h=(b-a)/n;

Page 2: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

for(i=1;i<n;i++)

/*or we can can write for(i=a+h;i<=b-h;i=i+h) { sum+=f(i);*/

{

sum= sum + f(a+i*h);

}

Trap= (h/2)*(f(a) + f(b) +2 *sum);

cout <<"\nThe value of the integral is: " << setprecision(3) << Trap <<endl ;

}

//Output:

Enter the lower limit a : 0

Enter the upper limit b: 1.2

Enter the number of subintervals n: 8

The value of the integral is: 2.32

//Alternative way for question 1

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double trapezoidal(double a,double b,int n)

{

Page 3: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

double h= (b-a)/n;

double z[9],I;

double x[9]= {0.0, 0.15, 0.3,0.45,0.6,0.75,0.9,1.05,1.2 };

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

z[i]= exp(x[i]); //the function is exp(x)

I=h/2*( z[0]+z[8] + 2*(z[1]+z[2]+z[3]+z[4]+z[5]+z[6]+z[7]) );

return (I);

}

int main()

{

double J;

J=trapezoidal(0,1.2,8);

cout<< "The integral value is " << setprecision(3) << J << "\n\n";

return 0;

}

//Output:

The integral value is 2.32

QUESTION 2

Write a C++ function sub-program to evaluate ∫

using

Trapezoidal rule correct to four decimal places. Compare the

result with the exact solution 1.48766

Page 4: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

#include <cmath>

#include <iostream>

#include <iomanip>

using namespace std;

float f(float x)

{

float func;

func = 1/(1+(x*x));

return func;

}

void main()

{

double a,b,h,sum=0, Trap, exact=1.48766;

// we can find the exact value by using the formula exact= atan(b) - atan(a);

int i, n;

cout <<"Enter the lower limit a : ";

cin >> a;

cout << "Enter the upper limit b: ";

cin >> b;

cout << "Enter the number of subintervals n: ";

cin >> n;

h=(b-a)/n;

for(i=1;i<n;i++)

Page 5: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

/*or we can can write for(i=a+h;i<=b-h;i=i+h) { sum+=f(i);*/

{

sum= sum + f(a+i*h);

}

Trap= (h/2)*(f(a) + f(b) +2 *sum);

cout <<"\nThe value of the integral is: " << setprecision(4) <<fixed << Trap <<endl ;

cout<<"The difference in Trapezoidal method = "<<setprecision(4) << fixed <<fabs(exact-

Trap)<<endl;

}

//Output:

Output 1:

Enter the lower limit a : 0

Enter the upper limit b: 12

Enter the number of subintervals n: 8

The value of the integral is: 1.5358

The difference in Trapezoidal method = 0.0482

Output 2:

Enter the lower limit a : 0

Enter the upper limit b: 12

Enter the number of subintervals n: 1000

The value of the integral is: 1.4877

The difference in Trapezoidal method = 0.0000

Page 6: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

QUESTION 3

Write a C++ program to evaluate ∫ √

using Simpson’s

1/3 rule.

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double f(double x)

{

double function;

function= pow(sin(x)+cos(x), 0.5);

return function;

}

void main()

{

double a,b, h,sum_even=0,sum_odd=0,Simpson_13;

int i,n;

cout << "Enter the lower limit a: ";

cin >> a;

cout << "Enter the upper limit b: ";

cin >> b;

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

cin >> n;

h=(b-a)/n;

for(i=1;i<n;i=i++)

{

if(i%2==0)

Page 7: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

sum_even= sum_even+ f(a+i*h);

if(i%2!=0)

sum_odd= sum_odd+ f(a+i*h);

}

Simpson_13=(h/3)*(f(a)+f(b)+ 4*sum_odd +2*sum_even);

cout <<"The value of the integral is " << setprecision(4) <<fixed << Simpson_13 <<

"\n\n";

}

//Output:

Enter the lower limit a: 0

Enter the upper limit b: 1

Enter the number of subintervals: 8

The value of the integral is 1.1394

//Alternative way for question 3

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double simpson13(double a,double b,int n)

{

double z[9],Simp,h=(b-a)/n;

double x[9]={0,0.125,0.25,0.375,0.5,0.625,0.75,0.875,1.0};

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

z[i]= pow(sin(x[i])+cos(x[i]),0.5);

Page 8: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

Simp=(h/3)*((z[0]+z[8]) + 2*(z[2]+z[4]+z[6]) + 4*(z[1]+z[3]+z[5]+z[7]) );

return Simp;

}

int main()

{

double J;

J=simpson13(0,1,8);

cout<< "The integral value is: "<< setprecision(4) <<fixed << J<<endl;

return 0;

}

//Output:

The integral value is: 1.1394

QUESTION 4

Write a C++ function sub-program to evaluate∫

using

Simpson’s 1/3 correct to four decimal places.

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double f(double x)

{

float function;

function= 1/(1+pow(x,2));

return function;

}

Page 9: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

void main()

{

double a,b, h,sum_even=0,sum_odd=0,Simpson_13;

int i,n;

cout << "Enter the lower limit a: ";

cin >> a;

cout << "Enter the upper limit b: ";

cin >> b;

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

cin >> n;

h=(b-a)/n;

for(i=1;i<n;i=i++)

{

if(i%2==0)

sum_even= sum_even+ f(a+i*h);

if(i%2!=0)

sum_odd= sum_odd+ f(a+i*h);

}

Simpson_13=(h/3)*(f(a)+f(b)+ 4*sum_odd +2*sum_even);

cout <<"The value of the integral is " << setprecision(4) <<fixed << Simpson_13 << "\n\n";

}

//Output:

Enter the lower limit a: 0

Enter the upper limit b: 12

Enter the number of subintervals: 6

The value of the integral is 1.4020

Page 10: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

//Alternative way for question 4

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double simpson13(double a,double b,int n)

{

double z[7],Simp,h=(b-a)/n;

double x[7]={0,2,4,6,8,10,12};

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

z[i]= 1/(1+pow(x[i],2));

Simp=(h/3)*((z[0]+z[6]) + 2*(z[2]+z[4]) + 4*(z[1]+z[3]+z[5]) );

return Simp;

}

int main()

{

double J;

J=simpson13(0,12,6);

cout<< "The integral value is: "<< setprecision(4) <<fixed << J<<endl;

return 0;

}

//Output

The integral value is: 1.4020

Page 11: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

QUESTION 5

Write a C++ program to evaluate∫

using Simpson’s 3/8

rule.

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double f(double x)

{

double function;

function=exp(sin(x));

return function;

}

void main()

{

double a,b, h,sum_miss=0,sum_triple=0,Simpson_38;

int i,n;

cout << "Enter the lower limit a: ";

cin >> a;

cout << "Enter the upper limit b: ";

cin >> b;

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

//should be taken as multiples of 3

cin >> n;

h=(b-a)/n;

Page 12: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

for (i=1;i<n;i++)

{

if ( i%3 != 0 )

sum_miss = sum_miss + f(a+i*h);

if(i%3==0)

sum_triple =sum_triple+ f(a+i*h);

}

Simpson_38= (3*h/8)*((f(a)+f(b))+ 3*sum_miss +2*sum_triple);

cout <<"The value of the integral is " << setprecision(4) <<fixed << Simpson_38 << "\n\n";

}

//Output:

Enter the lower limit a: 0

Enter the upper limit b: 1.570796

Enter the number of subintervals: 8

The value of the integral is 3.0381

QUESTION 6

Write a C++ program to evaluate∫

using Simpson’s 3/8

rule.

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

Page 13: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

double f(double x)

{

double function;

function=log10(x);

return function;

}

void main()

{

double a,b, h,sum_miss=0,sum_triple=0,Simpson_38;

int i,n;

cout << "Enter the lower limit a: ";

cin >> a;

cout << "Enter the upper limit b: ";

cin >> b;

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

//should be taken as multiples of 3

cin >> n;

h=(b-a)/n;

for (i=1;i<n;i++)

{

if ( i%3 != 0 )

sum_miss = sum_miss + f(a+i*h);

if(i%3==0)

sum_triple =sum_triple+ f(a+i*h);

}

Simpson_38= (3*h/8)*((f(a)+f(b))+ 3*sum_miss +2*sum_triple);

Page 14: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

cout <<"The value of the integral is " << setprecision(4) <<fixed << Simpson_38 << "\n\n";

}

//Output:

Enter the lower limit a: 2

Enter the upper limit b: 6

Enter the number of subintervals: 8

The value of the integral is 2.2833

QUESTION 7

Write a C++ function program to evaluate∫

using

Trapezoidal, Simpson’s 1/3 and Simpson’s 3/8 rules. Compare the results with the exact solution 2.32012

#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

double f(double x)

{

return exp(x) ;

}

double simpson_38(double a,double b,double n)

{

double h=(b-a)/n, sum_miss=0,sum_triple=0,Simp38;

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

{

if ( i%3 != 0 )

sum_miss = sum_miss + f(a+i*h);

if(i%3==0)

sum_triple =sum_triple+ f(a+i*h);

}

Page 15: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

Simp38= (3*h/8)*((f(a)+f(b))+ 3*sum_miss +2*sum_triple);

return Simp38;

}

double simpson_13(double a,double b,double n)

{

double h=(b-a)/n, sum_odd=0,sum_even=0,Simp13;

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

{

if(i%2==0)

sum_even =sum_even + f(a+i*h);

else

sum_odd=sum_odd + f(a+i*h);

}

Simp13 = (h/3)*( (f(a) + f(b)) + 4*sum_odd + 2*sum_even );

return Simp13;

}

double trapezoidal(double a,double b,double n)

{

double sum=0, Trap, h=(b-a)/n;

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

sum = sum+f(a+i*h);

Trap = (h/2)*( f(a) + f(b) + 2*sum);

return Trap ;

}

int main()

{

double a,b,n, exact= 2.32012;

int ans;

char choice;

Pilihan:

cout << "1. Trapezoidal\n2. Simpson's 1/3 rule\n3. Simpson's 3/8 rule\n\n";

cout << "Which number you wanna choose : ";

cin >> ans;

cout << endl;

Page 16: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

switch(ans)

{

case 1 : cout << "Please input a,b and n : ";

cin >> a >> b >> n;

cout<<"The Exact value = "<< setprecision(6) <<exact<<endl;

cout<<"The result for Trapezoidal method = "

<<setprecision(6)<<trapezoidal(a,b,n)<<endl;

cout<<"The difference in Trapezoidal method = "<<setprecision(6)<<

fabs(exact - trapezoidal(a,b,n))<<endl;

break;

case 2 : cout << "Please input a,b and n : ";

cin >> a >> b >> n;

cout<<"The Exact value = "<<setprecision(6)<<exact<<endl;

cout<<"The result for Simpson's 1/3 method = "<<setprecision(6)<<

simpson_13(a,b,n)<<endl;

cout<<"The difference in Simpson's 1/3

method="<<setprecision(6)<<fabs(exact- simpson_13(a,b,n))<<endl;

break;

case 3 : cout << "Please input a,b and n : ";

cin >> a >> b >> n;

cout<<"The Exact value = "<<setprecision(6)<<exact<<endl;

cout<<"The result for Simpson's 3/8 method = "<< setprecision(6)<<

simpson_38(a,b,n)<<endl;

cout<<"The difference in Simpson's 3/8 method

="<<setprecision(6)<<fabs(exact- simpson_38(a,b,n))<<endl;

break;

default: cout << "Invalid number. Try again : ";

cin >> ans;

goto Pilihan;

break;

}

if(fabs(exact-simpson_38(a,b,n)) <fabs(exact-simpson_13(a,b,n)) &&

fabs(exact-simpson_38(a,b,n))<fabs(exact-trapezoidal(a,b,n)))

Page 17: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

cout<<"Simpson's 3/8 is better"<<endl;

else if(fabs(exact-simpson_13(a,b,n))<fabs(exact-simpson_38(a,b,n)) &&

fabs(exact-simpson_13(a,b,n))<fabs(exact-trapezoidal(a,b,n)))

cout<<"Simpson's 1/3 is better"<<endl;

else

cout<<"Trapezoidal rule is better"<<endl;

check:

cout << "Do u want to try again (Y/N): ";

cin >> choice;

if ( choice == 'N' || choice == 'n')

cout << "Thank You." << endl;

else if ( choice == 'Y' || choice == 'y')

{

goto Pilihan;

}

else goto check;

return 0;

}

//Output:

1. Trapezoidal

2. Simpson's 1/3 rule

3. Simpson's 3/8 rule

Which number you wanna choose : 1

Please input a,b and n : 0 1.2 8

The Exact value = 2.32012

The result for Trapezoidal method = 2.32447

The difference in Trapezoidal method = 0.00434551

Simpson's 1/3 is better

Do u want to try again (Y/N): y

1. Trapezoidal

2. Simpson's 1/3 rule

3. Simpson's 3/8 rule

Which number you wanna choose : 2

Please input a,b and n : 0 1.2 8

Page 18: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

The Exact value = 2.32012

The result for Simpson's 1/3 method = 2.32012

The difference in Simpson's 1/3 method=3.43063e-006

Simpson's 1/3 is better

Do u want to try again (Y/N): y

1. Trapezoidal

2. Simpson's 1/3 rule

3. Simpson's 3/8 rule

Which number you wanna choose : 3

Please input a,b and n : 0 1.2 8

The Exact value = 2.32012

The result for Simpson's 3/8 method = 2.26695

The difference in Simpson's 3/8 method =0.0531698

Simpson's 1/3 is better

Do u want to try again (Y/N): n

Thank You.

//Alternative for question 7 #include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

#define f(x) exp(x)

double trap(double a,double b,double h,double n)

{

int i;

double x1,sumA=0,resultA;

x1=a;

for(i=1;i<n;i++)

{

x1=x1+h;

sumA=sumA+f(x1);

}

resultA=(h/2)*(f(a)+2*sumA+f(b));

return resultA;

}

double simp13(double a,double b,double h,double n)

{

int j;

double x2,sumB1=0,sumB2=0,resultB;

x2=a;

for(j=1;j<n;j++)

Page 19: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

{

x2=x2+h;

if(j%2==0)

sumB1=sumB1+2*f(x2);

else

sumB2=sumB2+4*f(x2);

}

resultB=(h/3)*(f(a)+sumB1+sumB2+f(b));

return resultB;

}

double simp38(double a,double b,double h,double n)

{

double sum_miss=0,sum_triple=0,Simp38;

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

{

if ( i%3 != 0 )

sum_miss = sum_miss + f(a+i*h);

if(i%3==0)

sum_triple =sum_triple+ f(a+i*h);

}

Simp38= (3*h/8)*(f(a)+f(b)+ 3*sum_miss +2*sum_triple);

return Simp38;

}

int main()

{

int n;

double a=0,b=1.2,h,exact= 2.32012;

cout<<"Enter the value of n: ";

cin>>n;

h=(b-a)/n;

cout<<"The Exact value = "<<setprecision(6)<<exact<<endl;

cout<<"The result for Trapezoidal method = "<<setprecision(6)<<trap(a,b,h,n)<<endl;

cout<<"The result for Simpson 1/3 method = "<<setprecision(6)<<simp13(a,b,h,n)<<endl;

cout<<"The result for Simpson 3/8 method = "<<setprecision(6)<<simp38(a,b,h,n)<<endl;

cout<<"The difference in Trapezoidal method = "<<setprecision(6) <<fabs(exact-

trap(a,b,h,n))<<endl;

cout<<"The difference in Simpson 1/3 method = "<<setprecision(6)<<fabs(exact-

simp13(a,b,h,n))<<endl;

Page 20: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

cout<<"The difference in Simpson 3/8 method = "<<setprecision(6)<<fabs(exact-

simp38(a,b,h,n))<<endl;

if(fabs(exact-simp38(a,b,h,n)) <fabs(exact-simp13(a,b,h,n)) &&

fabs(exact-simp38(a,b,h,n))<fabs(exact-trap(a,b,h,n)))

cout<<"Simpson's 3/8 is better"<<endl;

else if(fabs(exact-simp13(a,b,h,n))<fabs(exact-simp38(a,b,h,n)) &&

fabs(exact-simp13(a,b,h,n))<fabs(exact-trap(a,b,h,n)))

cout<<"Simpson's 1/3 is better"<<endl;

else

cout<<"Trapezoidal rule is better"<<endl;

return 0;

}

//Output:

Enter the value of n: 8

The Exact value = 2.32012

The result for Trapezoidal method = 2.32447

The result for Simpson 1/3 method = 2.32012

The result for Simpson 3/8 method = 2.26695

The difference in Trapezoidal method = 0.00434551

The difference in Simpson 1/3 method = 3.43063e-006

The difference in Simpson 3/8 method = 0.0531698

Simpson's 1/3 is better

//Alternative for question 7 #include<iostream>

#include<cmath>

using namespace std;

double f(double a)

{

return (double) exp(a);

}

int main()

Page 21: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

{

double a=0,b=1.2,n,h,Exact,sum3=0,sum=0,sumE=0,sumO=0;

double sumT=0,simps3per8,simp1per3,trapzoid;

cout<<"Enter the number of subintervals,n: ";

cin>>n;

h=(b-a)/n;

Exact=exp(b)-exp(a);

cout<<"Exact value="<<Exact<<endl;

for(int i=1; i<n; i++) //Simpson 3/8

{

if(i%3==0)

sum3=sum3 + f(a+i*h);

else

sum=sum + f(a+i*h);}

simps3per8=((3*h)/8)*(f(a)+f(b)+3*sum+2*sum3);

cout<<"Simpson's 3/8 ="<<simps3per8<<endl;

for(i=1; i<n; i++) //Simpson's 1/3

{

if(i%2==0)

sumE=sumE + f(a+i*h);

else

Page 22: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

sumO=sumO + f(a+i*h);}

simp1per3=(h/3)*(f(a)+f(b)+4*sumO+2*sumE);

cout<<"Simpson's 1/3 ="<<simp1per3<<endl;

for(i=1;i<n;i++) //Trapezoidal

sumT=sumT+f(a+i*h);

trapzoid=(h/2)*(f(a)+f(b)+2*sumT);

cout<<"Trapezoidal= "<<trapzoid<<endl;

if(fabs(Exact-simps3per8)<fabs(Exact-simp1per3) &&

fabs(Exact-simps3per8)<fabs(Exact-trapzoid))

cout<<"Simpson's 3/8 is better"<<endl;

else if(fabs(Exact-simp1per3)<fabs(Exact-simps3per8) &&

fabs(Exact-simp1per3)<fabs(Exact-trapzoid))

cout<<"Simpson's 1/3 is better"<<endl;

else

cout<<"Trapezoidal rule is better"<<endl;

return 0;

}

Page 23: C++ TUTORIAL 8

SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 8

//Output:

Enter the number of subintervals,n: 8

Exact value=2.32012

Simpson's 3/8 =2.26695

Simpson's 1/3 =2.32012

Trapezoidal= 2.32447

Simpson's 1/3 is better