7
QUESTION 1 Write a C++ program to find the positive root of the equation cos()−3+5=0 using Bisection method and Newton’s method. #include<iostream> #include <cmath> using namespace std; double f(double x); //Function of cos(x)-3x+5 double bisec(double a, double b); //Bisection Method subprogram double newton(double a, double b); //Newton Raphson Method double diff(double x); //1 st derivative of function of cos(x)-3x+5 int main() { double a=0,b=1; //To find the interval that contain root while(f(a)*f(b)>0) { a=b;b++; } cout << "Bisection Method" << endl; bisec(a,b); //Call out Bisestion Method subprogram cout << "\nNewton Raphson Method" << endl; newton(a,b); //Call out Newton Raphson subprogram return 0; } double f(double x) { return (double) cos(x)-3*x+5; } double bisec(double a, double b) { double r; do { r=(b+a)/2; if (f(a)*f(r)<0) b=r; else if (f(b)*f(r)<0) a=r; PREPARED BY FAIZ, 2 nd YEAR STUDENT BSc MATHS, UNIVERSITY OF MALAYA

C++ EXERCISES

Embed Size (px)

Citation preview

Page 1: C++ EXERCISES

QUESTION 1

Write a C++ program to find the positive root of the equation cos(𝑥)−3𝑥+5=0

using Bisection method and Newton’s method.

#include<iostream> #include <cmath> using namespace std; double f(double x); //Function of cos(x)-3x+5 double bisec(double a, double b); //Bisection Method subprogram double newton(double a, double b); //Newton Raphson Method double diff(double x); //1st derivative of function of cos(x)-3x+5 int main() {

double a=0,b=1; //To find the interval that contain root while(f(a)*f(b)>0) {

a=b;b++; } cout << "Bisection Method" << endl; bisec(a,b); //Call out Bisestion Method subprogram cout << "\nNewton Raphson Method" << endl; newton(a,b); //Call out Newton Raphson subprogram

return 0;

} double f(double x) {

return (double) cos(x)-3*x+5; } double bisec(double a, double b) {

double r; do {

r=(b+a)/2; if (f(a)*f(r)<0)

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

a=r;

PREPARED BY FAIZ, 2nd YEAR STUDENT

BSc MATHS, UNIVERSITY OF MALAYA

Page 2: C++ EXERCISES

else if (f(r)==0) break;

cout << a << "\t" << b << "\t" << r << "\t" << f(r) << endl;

} while(fabs(r-a)>0.00001 || fabs(r-b)>0.00001); //Correct to 5 decimal places

cout << "The positive root is " << r << endl;

return 0; } double newton(double a, double b) {

double r; cout << a << "\t" << f(a) << "\t" << diff(a) << endl; do {

r=a-(f(a)/diff(a)); b=a; a=r; cout << a<< "\t" << f(a) << "\t" << diff(a) << endl;

} while((a-b)/a>0.00001); //Correct to 5 decimal places

cout << "The positive root is " << a << endl;

return 0;

} double diff(double x) {

return (double) -sin(x)-3; }

//Output: Bisection Method 1.5 2 1.5 0.570737 1.5 1.75 1.75 -0.428246 1.625 1.75 1.625 0.0708229 1.625 1.6875 1.6875 -0.178939 1.625 1.65625 1.65625 -0.0540997 1.64063 1.65625 1.64063 0.00835306 1.64063 1.64844 1.64844 -0.0228757 1.64063 1.64453 1.64453 -0.00726188 1.64258 1.64453 1.64258 0.000545455

Page 3: C++ EXERCISES

1.64258 1.64355 1.64355 -0.00335825 1.64258 1.64307 1.64307 -0.0014064 1.64258 1.64282 1.64282 -0.000430477 1.6427 1.64282 1.6427 5.74887e-005 1.6427 1.64276 1.64276 -0.000186494 1.6427 1.64273 1.64273 -6.45027e-005 1.6427 1.64272 1.64272 -3.50703e-006 1.64271 1.64272 1.64271 2.69908e-005 The positive root is 1.64271 Newton Raphson Method 1 2.5403 -3.84147 1.66128 -0.0742152 -3.99591 1.64271 1.45216e-005 -3.99742 The positive root is 1.64271

QUESTION 2

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 3.10438

#include <iostream> #include <cmath> #define PI 3.14159265 using namespace std; double f(double x); //Function of exp(sin(x)) double trap(double a,double b,double h,double n); double simp13(double a,double b,double h,double n); double simp38(double a,double b,double h,double n); int main() {

double a=0,b=PI/2,h,n=20,exact=3.10438,t,s,r; h=(b-a)/n; //To get the internal size

t=trap(a,b,h,n); //Call out Trapezoidal Method subprogram s=simp13(a,b,h,n); //Call out Simpson 1/3 Method subprogram r=simp38(a,b,h,n); //Call out Simpson 3/8 Method subprogram

cout << "The Exact value is " << exact << endl; cout << "The Trapezoidal value is " << t << endl; cout << "The Simpson 1/3 value is " << s << endl; cout << "The Simpson 3/8 value is " << r << endl;

Page 4: C++ EXERCISES

a=fabs(exact-t); b=fabs(exact-s); h=fabs(exact-r);

cout << "The difference Exact & Trapezoidal is " << a << endl; cout << "The difference Exact & Simpson 1/3 is " << b << endl; cout << "The difference Exact & Simpson 3/8 is " << h << endl;

if(a<b && a<h)

cout << "\nTrapezoidal is the best." << endl; //To get the best method

else if(b<a && b<h) cout << "\nSimpson 1/3 is the best." << endl;

else cout << "\nSimpson 3/8 is the best." << endl;

return 0;

} double f(double x) {

return (double) exp(sin(x)); } double trap(double a,double b,double h,double n) {

double sum=0, trap; for(int i=1;i<n;i++)

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

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

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

double sum_even=0,sum_odd=0,simp_13; for(int i=1;i<n;i++) {

if(i%2==0) sum_even+=f(a+i*h);

if(i%2!=0)

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

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

}

Page 5: C++ EXERCISES

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

double sum_miss=0,sum_triple=0,simp_38; for(int i=1;i<n;i++) {

if(i%3==0) sum_triple+=f(a+i*h);

if(i%3!=0) sum_miss+=f(a+i*h);

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

}

//Output:

The Exact value is 3.10438

The Trapezoidal value is 3.10386

The Simpson 1/3 value is 3.10438

The Simpson 3/8 value is 3.07772

The difference Exact & Trapezoidal is 0.000515029

The difference Exact & Simpson 1/3 is 9.88271e-007

The difference Exact & Simpson 3/8 is 0.0266601

Simpson 1/3 is the best.

QUESTION 3

Write a C++ program using RK2 and RK4 methods to find y(2) for

=

,

y(1)=1.2 with h=0.1

#include <iostream> #include <cmath> using namespace std; double f(double x); //dy/dx of (1+xy)/(x+y) double rk2(double x0,double y0,double h,double n); double rk4(double x0,double y0,double h,double n);

Page 6: C++ EXERCISES

int main() { double x0=1,y0=1.2,x=2,h=0.1,n; n=(x-x0)/h; //To get the number of subintervals

cout << "Rungge-Kutta Method of order 2" << endl; rk2(x0,y0,h,n); //Call out the RK2 subprogram

cout << "\nRungge-Kutta Method of order 4" << endl; rk4(x0,y0,h,n); //Call out the RK4 subprogram return 0; } double f(double x,double y) { return (double) (1+x*y)/(x+y); } double rk2(double x0,double y0,double h,double n) { double k1,k2; for(int i=1;i<=n;i++) { k1=h*f(x0,y0); k2=h*f(x0+h/2,y0+k1/2); y0+=k2; x0+=h;

cout << x0 << "\t" << y0 << "\t" << k1 <<"\t" << k2 << endl;

} cout << "The value of y(2) is " << y0 << endl; return 0; } double rk4(double x0,double y0,double h,double n) { double k1,k2,k3,k4; for(int i=1;i<=n;i++) { k1=h*f(x0,y0); k2=h*f(x0+h/2,y0+k1/2); k3=h*f(x0+h/2,y0+k2/2); k4=h*f(x0+h,y0+k3); y0+=(k1+2*k2+2*k3+k4)/6; x0+=h;

cout<< x0 <<"\t"<< y0 <<"\t"<< k1 <<"\t"<< k2 <<"\t"<< k3 <<"\t"<< k4 << endl;

}

Page 7: C++ EXERCISES

cout << "The value of y(2) is " << y0 << endl; return 0; }

//Output:

Rungge-Kutta Method of order 2

1.1 1.30054 0.1 0.100543

1.2 1.40265 0.101252 0.102106

1.3 1.50685 0.103094 0.104199

1.4 1.61358 0.105417 0.106731

1.5 1.72322 0.108144 0.109637

1.6 1.83608 0.111219 0.112868

1.7 1.95247 0.114599 0.116388

1.8 2.07264 0.118254 0.12017

1.9 2.19683 0.122158 0.12419

2 2.32526 0.126292 0.128432

The value of y(2) is 2.32526

Rungge-Kutta Method of order 4

1.1 1.30057 0.1 0.100543 0.100544 0.101252

1.2 1.4027 0.101252 0.102106 0.102108 0.103094

1.3 1.50692 0.103094 0.104199 0.104204 0.105418

1.4 1.61367 0.105418 0.106732 0.106738 0.108145

1.5 1.72333 0.108145 0.109638 0.109646 0.11122

1.6 1.83621 0.11122 0.112869 0.11288 0.114601

1.7 1.95262 0.114601 0.11639 0.116402 0.118256

1.8 2.07281 0.118256 0.120172 0.120186 0.122161

1.9 2.19702 0.122161 0.124193 0.124208 0.126295

2 2.32547 0.126295 0.128435 0.128452 0.130643

The value of y(2) is 2.32547