Sei sulla pagina 1di 57

//Program : Exercise 19.

2, Question1

//Author : Paavitha Thambiratnam [SES060570]

//Date : 01/03/09

//This question uses the Bisection Method

#include <iostream>

#include <iomanip>

#include <cmath>

double f(double); //function prototype

using namespace std;

int main()

int max_bi, no;

//'max_bi' is the maximum number of bisections

//'no' is just the number of iterations

double a, b, p, x, con;

//a and b are chosen such that f(a).f(b)<0

//'con' is the convergence tolerance

cout<<"Enter values for a lower limit 'a' ";

cout<<"and upper limit 'b' which are close to 2.2:\n";

cin>>a>>b;
p = f(a)*f(b); //bisection method can only be used if p<0

while(p>=0)

cout<<"Enter a different set of values: ";

cin>>a>>b;

p = f(a)*f(b);//re-evaluate p for each loop

cout<<"\nEnter a value for the convergence tolerance: ";

cin>>con;

cout<<"\nEnter a value for the maximum number of bisections: ";

cin>>max_bi;

cout<<"\nThe results are as follows:\n";

cout<<left<<setw(3)<<"no";

cout<<right<<setw(8)<<"a";

cout<<right<<setw(8)<<"b";

cout<<right<<setw(10)<<"x";

cout<<right<<setw(12)<<"f(x)\n";

no = 1;

x = (a+b)/2; //first bisection

while ((no<=max_bi) && ((b-a)>con))

{
cout<<left<<setw(3)<<no;

cout<<setprecision(4)<<fixed;

cout<<right<<setw(8)<<a;

cout<<right<<setw(8)<<b;

cout<<setprecision(6);

cout<<right<<setw(10)<<x;

cout<<right<<setw(12)<<f(x)<<"\n";

if(f(x)>0) //we assume f(a) is positive

a = x;

else

b = x;

x = (a+b)/2; //subsequent bisection formulae

no++;

} //end of while loop

if (no>max_bi) //if the bisections do not converge to the root

cout<<"No Convergence\n";

else

cout<<"The required approximation to the root is: ";

cout<<"x = "<<x<<"(+/-)"<<con<<"\n";

}
return 0;

double f(double x)

return sin(x) - x/3; //this is the function of f(x)

}
// Chew Sie Wai

// SES 060086

// SJES 2235 Assignment

// Exercise 19.2 Question 2

//In Exercise 1 through 6 use the bisection method to find to required root.

//The root of exp(x/3)-x^2=0 close to x=1.1.

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

double f(double x)

double y;

y=exp(x/3)-x*x;

return y;

int main()

int k_max,k;

double a,b,eps,x;
cout<<"Input point a :\n";

cin>>a;

cout<<"Input point b :\n";

cin>>b;

cout<<"Input eps :\n";

cin>>eps;

cout<<"Input k maximum :\n";

cin>>k_max;

cout<<"a= "<<a<<" b= "<<b<<" eps="<<eps<<" k


maximum="<<k_max<<endl<<endl;

cout<<"The result are \n";

cout<<" k a b x f(x)\n";

k=1;

x=0.5*(a+b);

while ((k<=k_max) && ((b-a)>eps))

cout<<k<<setprecision(4)<<fixed<<setw(8)<<a<<setw(8)<<b<<setprecision(6)
<<setw(10);

cout<<x<<setw(12)<<f(x)<<endl;

if(f(x)>0)

a=x;
else

b=x;

x=0.5*(a+b);

k++;

if (k>k_max)

cout<<"No convegence!"<<endl;

return 0;

}
/*LIM YEE CUI

SES 060292

ASSIGNMENT 2271, 19.2, QUES 3*/

#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

double f(double x)

double func;

func=3*log(x)+x*x-3;

return func;

int main ()

int kmax, k;

double a, b, eps, x;

cout<<"input a, b, eps, kmax \n";

cin>>a>>b>>eps>>kmax;

cout<<"the input data are\n";

cout<<"a= "<<a<<" b= "<<b;

cout<<" eps= "<<eps<<" kmax= "<<kmax<<endl;

cout<<"the result are \n";

cout<<"k a b x f(x)\n";

k=1;
x=0.5*(a+b);

while ((k <=kmax) && ((b-a)>eps))

cout<<k<<setprecision
(4)<<fixed<<setw(8)<<a<<setw(8)<<b<<setprecision(6)<<setw(10)<<x<<setw
(12)<<f(x)<<endl;

if (f(x)>0)

b=x;

else

a=x;

x=0.5*(a+b);

k++;

if (k>kmax)

cout<<"no convergence"<<endl;

return 0;

}
//Name: Syazwani Bte Mohamed Nasir

//No.Matric: SEJ070079

//Question no.4 chapter 19.2

/*Use the bisection method to find the largest positive root of

(x^3)-(1.9)x^2-(2.3)x+3.7=0 */

#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

float f(float);

int main()

int kmax,k;

float a,b,eps,x;

cout<<"input a,b,eps,kmax\n";

cin>>a>>b>>eps>>kmax;

cout<<"the input data are\n";

cout<<"a= "<<a<<" b= "<<b;

cout<<" eps= "<<eps<<" kmax= "<<kmax<<"\n";

cout<<"The results are\n";

cout<<"k a b f(a) f(b) x f(x)\n";

k=1;

x=0.5*(a+b);

while((k<= kmax) && ((b-a)> eps))


{

cout<<k<<setprecision(4)<<fixed<<setw(10)<<a<<setw(10)

<<b<<setprecision(6)<<setw(10)<<f(a)<<setw(10)

<<f(b)<<setw(10)<<x<<setw(10)<<f(x)<<"\n";

if(f(x)>0)

b=x;

else

a=x;

x=0.5*(b+a);

k++;

if(k>kmax)

cout<<"no convergence"<<"\n";

return 0;

float f(float x)

return (x*x*x)-1.9*(x*x)-2.3*x+3.7;

}
/*

Name : Gan Ching Siang

Matric number : SER070008

Question : 19.2 Roots of Nonlinear Functions Question 5 :


Use the bisection method to find the smallest root of

Use the bisection method to find the smallest root of

x^3 - 4.5x^2 + 1.3x + 8 = 0.

*/

#include <iostream>

#include <iomanip>

#include <cmath>

float f(float x)

return x*x*x-4.5*x*x+1.3*x+8;

using namespace std;

int main()

int max,d;

float a,b,c,x;

cout<<"a = ";

cin>>a;

cout<<"b = ";

cin>>b;
cout<<"The desired accuracy = ";

cin>>c;

cout<<"Max number of repetitions = ";

cin>>max;

cout<<endl;

cout<<"Results :"<<endl;

cout<<setw(2)<<"k"<<setw(10)<<"a"<<setw(10)<<"b"<<setw(12)<<"x"<<s
etw(14)<<"f(x)"<<endl;

d=1;

x=0.5*(a+b);

while((d<=max) && ((b-a)>c))

cout<<setw(2)<<d<<setw(12)<<setprecision(6)<<fixed<<setw(12)<<a
<<setw(12)<<b

<<setw(12)<<x<<setw(12)<<f(x)<< endl;

if(f(x)<0)

a=x;

else

b=x;

x=0.5*(a+b);

d++;

if (d>max && (b-a)>c)

cout<<"No Convergence"<<endl;

return 0;

}
/*

Output

a = -1.3

b = -0.9

The desired accuracy = 0.0001

Max number of repetitions = 20

Results :

k a b x f(x)

1 -1.300000 -0.900000 -1.100000 -0.205999

2 -1.100000 -0.900000 -1.000000 1.200001

3 -1.100000 -1.000000 -1.050000 0.516126

4 -1.100000 -1.050000 -1.075000 0.159892

5 -1.100000 -1.075000 -1.087500 -0.021840

6 -1.087500 -1.075000 -1.081250 0.069328

7 -1.087500 -1.081250 -1.084375 0.023820

8 -1.087500 -1.084375 -1.085937 0.001009

9 -1.087500 -1.085937 -1.086719 -0.010410

10 -1.086719 -1.085937 -1.086328 -0.004700

11 -1.086328 -1.085937 -1.086133 -0.001846

12 -1.086133 -1.085937 -1.086035 -0.000418

*/
//Lim Horng Cherng SER070018

//Exercises 19.2 - Question 6

//Find the roots of (0.5)*sqrt(1-x^2)-x^2=0

// This program approximates a root between a and b of a function f.

// It is assumed that f(a) is positive and f(b) is negative.

// This program required a function subroutine for f,

// the points a and b, the convergence tolerance, eps,

// and the maximum number of bisections allowed, imax.

#include <iostream>

#include <iomanip>

#include <cmath>

double f(double x)

{double b=sqrt(1-x*x);

return ((0.5)*b)-(x*x);

using namespace std;

int main( )

int imax,i;

double a,b, eps,x;

cout<<"solving the equation (0.5)*sqrt(1-x^2)-x^2=0 "<<endl;


cout <<"a: ";

cin>>a;

cout<<"b: ";

cin>>b;

cout<<"eps: ";

cin>>eps;

cout<<"imax: ";

cin>>imax;

cout << "The results are \n";

cout
<<setw(2)<<"i"<<setw(10)<<"a"<<setw(12)<<"b"<<setw(12)<<"x"<<setw(12)
<<"f(x)"<<endl;

i=1;

x=0.5*(a+b);

while((i <= imax) && (fabs(b-a) > eps))

cout<<setw(2)<<i<<setw(12)<<setprecision(6)<<fixed<<a<<setw(12)<<setpre
cision(6)<<b<<setw(12)<<setprecision(6)<<x<<setw(14)<<setprecision(6)<<f(x
)<<endl;

if ( f(x) > 0 )

a=x;

else

b=x;

x=0.5*(a+b);

i++;

}
if (i > imax && (fabs(b-a)>eps)) cout << "No Convergence!" << endl;

return 0;

/*

OUTPUT

// root (1) with the choice of a=-0.55 and b=-0.65

solving the equation (0.5)*sqrt(1-x^2)-x^2=0

a: -0.55

b: -0.65

eps: 1e-6

imax: 20

The results are

i a b x f(x)

1 -0.550000 -0.650000 -0.600000 0.040000

2 -0.600000 -0.650000 -0.625000 -0.000313

3 -0.600000 -0.625000 -0.612500 0.020079

4 -0.612500 -0.625000 -0.618750 0.009942

5 -0.618750 -0.625000 -0.621875 0.004830

6 -0.621875 -0.625000 -0.623437 0.002262

7 -0.623437 -0.625000 -0.624219 0.000976

8 -0.624219 -0.625000 -0.624609 0.000332

9 -0.624609 -0.625000 -0.624805 0.000010


10 -0.624805 -0.625000 -0.624902 -0.000151

11 -0.624805 -0.624902 -0.624854 -0.000071

12 -0.624805 -0.624854 -0.624829 -0.000031

13 -0.624805 -0.624829 -0.624817 -0.000010

14 -0.624805 -0.624817 -0.624811 -0.000000

15 -0.624805 -0.624811 -0.624808 0.000005

16 -0.624808 -0.624811 -0.624809 0.000002

17 -0.624809 -0.624811 -0.624810 0.000001

Press any key to continue

// root (2) with the choice of a=0.60 and b=0.70

solving the equation (0.5)*sqrt(1-x^2)-x^2=0

a: 0.60

b: 0.70

eps: 1e-6

imax: 20

The results are

i a b x f(x)

1 0.600000 0.700000 0.650000 -0.042533

2 0.600000 0.650000 0.625000 -0.000313

3 0.600000 0.625000 0.612500 0.020079

4 0.612500 0.625000 0.618750 0.009942

5 0.618750 0.625000 0.621875 0.004830

6 0.621875 0.625000 0.623437 0.002262

7 0.623437 0.625000 0.624219 0.000976

8 0.624219 0.625000 0.624609 0.000332


9 0.624609 0.625000 0.624805 0.000010

10 0.624805 0.625000 0.624902 -0.000151

11 0.624805 0.624902 0.624854 -0.000071

12 0.624805 0.624854 0.624829 -0.000031

13 0.624805 0.624829 0.624817 -0.000010

14 0.624805 0.624817 0.624811 -0.000000

15 0.624805 0.624811 0.624808 0.000005

16 0.624808 0.624811 0.624809 0.000002

17 0.624809 0.624811 0.624810 0.000001

Press any key to continue

*/
// Author : Chong Foo Weng (SES050131)

// Date : 11/03/2009

// Task : SJES2271 Assignment

// Question : 14 (Exercise 19.2)

// This program is developed to approximate the root

// of the function f(x)=tan x + 2tanh x. Note that

// the function is not defined for all real numbers.

// A choice of the starting value, x[0] is important

// for convergence to the desired root and eps has

// to be non-negative and as small as possible for

// accuracy. See the report prepared by the author

// for detailed explanation.

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double f(double a)

return(tan(a)+2.0*tanh(a));

double fprime(double b)

return(pow(cos(b),-2.0)+2.0*pow(cosh(b),-2.0));

}
int main()

double x[1000];

double eps;

cout<<" Input the following data :"<<endl;

cout<<endl;

cout<<" x[0] = ";

cin>>x[0];

cout<<" eps = ";

cin>>eps;

cout<<endl;

cout<<"Iteration x[n+1] x[n] f(x[n+1]) Approximate Root"

<<endl;

x[1]=x[0]-(f(x[0])/fprime(x[0]));

int i=0;

while(fabs(x[i+1]-x[i])>eps)

cout<<setw(5)<<i+1<<setprecision(6)<<fixed<<setw(18)<<x[i+1]

<<setprecision(6)<<setw(15)<<x[i]<<setprecision(6)<<setw(15)

<<f(x[i+1])<<setprecision(6)<<setw(16)<<x[i+1]<<endl;

i+=1;

x[i+1]=x[i]-(f(x[i])/fprime(x[i]));

cout<<endl;

return 0;

}
// AMIR HUSAINI BIN AZAHAN

// SER070004

// Q15

# include <cmath>

# include <iostream>

# include <iomanip>

double f0(double x)

double fun ;

fun = x*x*x*x-4.0*x*x*x+x*x+1.2 ;

return fun ;

double f1(double x)

double fun1 ;

fun1 = 4.0*x*x*x-12.0*x*x+2*x ;

return fun1 ;

using namespace std ;

int main ()

{
double y[10];

int i;

i=0;

cout<<"please input your initial x value \n" ;

cin>>y[i];

cout<<"\nusing the Newton Raphson Method we got \n\n" ;

cout<<" n x(n) \n" ;

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

while (i<=9)

cout<<setw(4)<<i+1<<setprecision(6)<<fixed<<setw(24)<<y[i]<<endl ;

y[i+1]=y[i]-(f0(y[i])/f1(y[i]));

i++ ;

cout<<"\nCHECK\nput final x value in the f(x)\n" ;

cout<<f0(y[i])<<endl;

return 0 ;

}
//Author: MOHAMMAD FARID BIN MOHD SHAH

//MATRIC NO: SES 060325

//Question 17

#include <iostream>

#include <cmath>

#include<iomanip>

using namespace std;

double f(double);

double fprime(double);

double newton(double[],double);

void main()

double x[100], root;

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

cout<<"\n";

cout<<"To find the root of 3x - e^(-x) = 0";

cout<<"\n";

cout<<"Select a suitable x0 = ";

cin >> x[0];

cout<<"\n";

cout<<"f(x0)="<<f(x[0])<<endl;

cout<<"\n";

cout<<"f'(x0)="<<fprime(x[0])<<endl;
cout<<endl;

root = newton(x, 0.0005); //calling the final answer for function "double
newton (double x[], double tol)"

cout << root << endl;

double f(double x) //Subprogram 1 (For f(x))

return 3*x - exp((-1)*x);

double fprime(double x) //Subprogram 2 (For f'(x))

return 3 + exp((-1)*x);

double newton (double x[], double tol) //Subprogram 3 (Newton Raphson


Method)

int i, N;

double dif;

cout<<"No. of iterations: N = ";

cin>>N;

cout<<endl;

for(i=0; i<=N; i++)

{
x[i+1] = x[i] - f(x[i])/fprime(x[i]);

cout<<"x"<<i+1<<" =
"<<x[i+1]<<setw(5)<<"f("<<x[i+1]<<") = "<< f(x[i+1])<< setw(10) <<
"f'("<<x[i+1]<<") = "<< fprime(x[i+1])<<endl;

cout<<endl;

do

for(i=0; i<=N; i++)

x[i+1] = x[i] - f(x[i])/fprime(x[i]); //We already have x0, this


part is to find x1,x2,...xn

cout<<"x["<<i+1<<"] = "<< x[i+1]<<endl; //prints out


x1,x2,x3,...,xn

dif = fabs(x[i+1] - x[i]);

while(dif > tol); //when dif<tol(or when condition is not satisfied), we


have the root.

cout<<"Hence, the root is : ";

return x[N+1]; //returning the value of the root(or when condition is not
satisfied)

/*output

THE NEWTON-RAPHSON METHOD


To find the root of 3x - e^(-x) = 0

Select a suitable x0 = 0.8

f(x0)=1.95067

f'(x0)=3.44933

No. of iterations: N = 8

x1 = 0.234478 f(0.234478) = -0.0875492 f'(0.234478) = 3.79098

x2 = 0.257572 f(0.257572) = -0.000209316 f'(0.257572) = 3.77293

x3 = 0.257628 f(0.257628) = -1.18945e-009 f'(0.257628) = 3.77288

x4 = 0.257628 f(0.257628) = -1.11022e-016 f'(0.257628) = 3.77288

x5 = 0.257628 f(0.257628) = 0 f'(0.257628) = 3.77288

x6 = 0.257628 f(0.257628) = 0 f'(0.257628) = 3.77288

x7 = 0.257628 f(0.257628) = 0 f'(0.257628) = 3.77288

x8 = 0.257628 f(0.257628) = 0 f'(0.257628) = 3.77288

x9 = 0.257628 f(0.257628) = 0 f'(0.257628) = 3.77288


x[1] = 0.234478

x[2] = 0.257572

x[3] = 0.257628

x[4] = 0.257628

x[5] = 0.257628

x[6] = 0.257628

x[7] = 0.257628

x[8] = 0.257628

x[9] = 0.257628

Hence, the root is : 0.257628

Press any key to continue . . .

//
// Name: TAI MENG CHUI

// Matrics No: SEJ080057

//Assignment: Exercise 19.2-Question 18

// Solving 1 + tanh x - 2 tan x = 0 using Newton's method.

// Sketching graphs y=tanh x and y = 2 tan x -1 we see there are many positive
roots.

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

double f(double);

double fp(double);

int main()

const double eps=0.00001;

const int kmax=20000; // maximum number of iterations

long double x,x0; // intial guess

cout<< "Please enter a value of x0 where x must be greater than 6.3.\n";

cout<<" x0 is the initial guess.\n";

cin>>x0;

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

int n=0;
// stop the program if the solution not found after kmax iterations

while (n <= kmax)

if (fp(x0) == 0.0)

cout << "You cannot use this method. The denominator is zero!"
<<"\n";

system("PAUSE");

break;

n++;

x=x0 - f(x0)/fp(x0);

cout << n << " " << x <<"\n";

if (fabs(x-x0) < eps)

cout << "The root is " << fixed << setprecision(6) << x0 << "\n";

break;

x0=x;

system("PAUSE");

return 0;

double f(double x)

return (1 + tanh(x)-2*pow(cos(x),-2.0));

}
double fp(double x)

return (pow(cosh(x),-2.0)-2*pow(cos(x),-2.0));

}
// SJES 2271 Scientific Computing I

// Student's name: LIM TENG KEE

// Matric Number : SEP 05 0086

// Lecturer : Dr. Amran Hussin

// Exercise 19.4

// Question 1

#include <iostream>

#include<iomanip>

#include <cmath>

using namespace std;

long double f(long double);

long double g(long double);

long double simps(long double,long double,long double);

int main()

long double a,b,h;

cout<<"Please enter the values of the lower bound a, upper bound b, and
step length h:"<<endl;

cout<<"a=";

cin>>a;

cout<<"b=";

cin>>b;
cout<<"h=";

cin>>h;

cout<<endl;

cout<<"By the composite Simpson's rule, the integral is


"<<setprecision(6)<<fixed<<simps(a,b,h)<<endl;

cout<<"for h = "<<h<<" at the interval ["<<a<<","<<b<<"]"<<endl;

cout<<"whereas the exact integral is "<<g(b)-g(a)<<endl;

return 0;

long double f(long double x)

return 2*pow(x,3)-3*pow(x,2)+4*x-1;

long double g(long double x)

return 0.5*pow(x,4)-pow(x,3)+2*pow(x,2)-x;

long double simps(long double a,long double b, long double h)

int n;
long double x;

long double sum=0, add=0, factor;

x=a;

n=(b-a)/h;

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

factor=3.0+pow(-1,i+1);

if(i==0 || i==n)

factor=1;

add=(h/3)*factor*f(x);

sum +=add;

x+=h;

return sum;

}
//Name: Leong Ying Hsien

//No. Matric : SER 070017

//Exercise 19.4

//Question 2

/*Use the composite trapezoidal rule with the step length

h = 0.1 to evaluate,

I= Intergrate (dx/(1+x*x)

and estimate the error term involved. Compare your results

with the exact value I = I= ¦Ð/4 Repeat the calculation

using the composite Simpson's rule with the same step

length, but without estimating the error.*/

//Date: 12.3.2009

#include <iostream>

#include <cmath>

// This is the function

float f(float x) {

float function;

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

return function;

// This function compute the the sum

float s(float a, float b, double h)

float x=a;

float sum= 0.0;

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


{

x= x+h;

sum += f(x);

return sum;

// This function compute the differentiated of the function

float g (float x)

float eror;

eror= -2*x/pow(1+x*x,2);

return eror;

// Simpson'rule

float sim(float a,float b)

float simpson,c;

c=(a+b)/2;

simpson= (b-a)/6*(f(a)+ 4*f(c)+f(b));

return simpson;

using namespace std;

int main()
{

float a,b,area;

double h;

cout << "Input lower limit a, upper limit b, and step size.\n";

cin >> a >> b >> h;

cout << "The input data are\n";

cout << "a = " << a << " b = " << b << " h= "<< h <<endl;

area= h*0.5*(f(a) + 2*s (a,b,h) + f(b))- h*h/12*(g(b)-g(a));

cout << "Integral = " << area << " for h = " << h <<".\n";

cout << "exact value is="<< atan(1)<<".\n";

cout << "The different between the exact value and the result obtain is="<<
atan(1)-area<<".\n";

cout << "The value which obtain by Simphon's rule is ="<< sim(a,b)<<".\n";

return 0;

}
// LEE CHIEW LAI SER070013

//17 MARCH 2009

//FINAL ASSIGNMENT: EXERCISE 19.4 QUESTION 4

//USE TRAPEZOIDAL AND SIMPSON'S RULE WITH STEP H=0.2

//ESTIMATE POW(X,2)*EXP(-X) FROM 0 TO 2

//n must be even for Simpson's rule

#include <iostream>

#include <cmath>

using namespace std;

long double f(long double x)

{long double f;

f=pow(x,2)*exp(-x);

return f;}

long double trapezium(long double a, long double b, int n)

{long double h=(b-a)/n;

long double x=a;

long double value=(f(a)+f(b))/2.000;

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

{x=x+h;

value=value+f(x);

} return value*h;

long double simpson(long double a,long double b,int n)

{long double g=(b-a)/(n);


long double s=0.000;

long double sum=0.000;

if (n==2)

{s=g/3*(f(a)+4*f(a+g)+f(b));

return s;}

else

{for (long double i=1;i<=n-1;i+=2)s+=f(a+i*g);

for (long double j=2;j<=n-2;j+=2)

sum+=f(a+j*g);

return g/3*(f(a)+4*s+2*sum+f(b));}

int main()

{int n;

long double a,b,h;

cout<<"insert values for lower limit,upper limit,and also the number of partition
which is a even number"<<endl;

cout<<"a=";

cin>>a;

cout<<"b=";

cin>>b;

cout<<"n=";

cin>>n;

cout<<"lower limit="<<a<<endl;

cout<<"upper limit="<<b<<endl;

cout<<"number of partition="<<n<<endl<<endl;

h=(b-a)/n;

cout<<"Integral= "<<trapezium(a,b,n)<<" by using composite trapezoidal's rule


fot number of partition="<<n<<" and h="<<h<<endl;
cout<<"Integral= "<<simpson(a,b,n)<<" by using composite simpson's rule fot
number of partition="<<n<<" and h="<<h<<endl;

return 0;

/*result:

insert values for lower limit,upper limit,and also the number of partition which is a
even number

a=0

b=2

n=10

lower limit=0

upper limit=2

number of partition=10

Integral= 0.646633 by using composite trapezoidal's rule fot number of


partition=10 and h=0.2

Integral= 0.646702 by using composite simpson's rule fot number of partition=10


and h=0.2

*/
//Name: TEH CHONG HENG

//Matric Number: SER070035

//Date: 22 March 2009

//Assignment: Exercise 19.4 Question 8

//This program is used to estimate the value of J2(2), and compare the value
obtained with the exact value given.

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

//The following is the function of the integrand.

//Here we use y as theta and x = 2.0

long double f(long double y)

return cos(2.0*sin(y)-2.0*y);

//The following user defined function is used to estimate the value of integral by
using composite Simpson's Rule.

long double simp(long double a, long double b, long double h)

long double n=(b-a)/(2*h);

long double sum1=0.0;

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

sum1+=f(a+(2*i-1)*h);

long double sum2=0.0;

for(int j=1;j<=n-1;j++)
sum2+=f(a+2*j*h);

return (h/3)*(f(a)+4*sum1+2*sum2+f(b));

int main()

double a,b,h,u;

const long double pi=4.0*atan(1.0);

a=0.0;

b=pi;

h=pi/8;

cout<<"Given that:\n";

cout<<"lower limit of the integration, a = "<<a<<"\n";

cout<<"upper limit of the integration, b = pi = "<<b<<"\n";

cout<<"step length, h = pi/8 = "<<h<<"\n";

cout<<"\n";

u=simp(0,pi,pi/8)/pi;//J2(2)=(1/pi)*(integral of f(y))

cout<<"Using composite Simpson's rule"<<"\n";

cout<<"Value of J2(2) = "<<fixed<<setprecision(6)<<simp(a,b,h)/pi<<"\n";

cout<<"\n";

cout<<"Given that the actual value of J2(2) = 0.352834 "<<"\n";

cout<<"Difference between the estimated value and the actual value =


"<<(0.352834)-u<<"\n";

return 0;

/*Sample Output:
Given that:

lower limit of the integration, a = 0

upper limit of the integration, b = pi = 3.14159

step length, h = pi/8 = 0.392699

Using composite Simpson's rule

Value of J2(2) = 0.352433

Given that the actual value of J2(2)=0.352834

Difference between the estimated value and the actual value = 0.000401*/
//Name: KHAW ZHAN HAO

//Metric Number: SER070012

//Date: 22 March 2009

//Assignment: Exercise 19.4 Question 9

//This program is used to estimate the value of J1(4), and compare the value
obtained with the exact value given.

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

//The following is the function of the integrand.

//Here we use y as theta and we substitute x=4.0

long double f(long double y)

return cos(4.0*sin(y)-y);

//The following user defined function is used to estimate the value of integral by
using composite Simpson's Rule.

long double simp(long double a, long double b, long double h)

long double n=(b-a)/(2*h);

long double sum1=0.0;

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

sum1+=f(a+(2*i-1)*h);

long double sum2=0.0;

for(int j=1;j<=n-1;j++)
sum2+=f(a+2*j*h);

return (h/3)*(f(a)+4*sum1+2*sum2+f(b));

int main()

double a,b,h,u;

const long double pi=4.0*atan(1.0);

a=0.0;

b=pi;

h=pi/10;

cout<<"We are given that:\n";

cout<<"lower limit of the integration, a="<<a<<"\n";

cout<<"upper limit of the integration, b=pi="<<b<<"\n";

cout<<"step length, h=pi/10="<<h<<"\n";

cout<<endl;

u=simp(0,pi,pi/10)/pi;//We know that J1(4)=(1/pi)*(integral of f(y))

cout<<"By using composite Simpson's rule with step length


h=pi/10="<<h<<", \n";

cout<<"The value of
J1(4)="<<fixed<<setprecision(6)<<simp(a,b,h)/pi<<"\n";

cout<<endl;

cout<<"We are given that the actual value of J1(4)=-0.066043 \n";

cout<<"The difference between the estimated value and the actual


value="<<u-(-0.066043)<<"\n";

return 0;

//Sample Output:
/*We are given that:

lower limit of the integration, a=0

upper limit of the integration, b=pi=3.14159

step length, h=pi/10=0.314159

By using composite Simpson's rule with step length h=pi/10=0.314159,

The value of J1(4)=-0.065743

We are given that the actual value of J1(4)=-0.066043

The difference between the estimated value and the actual value=0.000300 */
// Assignment SJES 2271

// Name : Lee Wei Ven

// Matric no. : SER070015

// Exercise 19.7 Question 1 :

// Solve the initial value problem by computer using modified Euler's method.

// y' = (3x^2 + y^2)^0.5 - y with y(2)= 0 and h= 0.2 over the interval 2<= x <=3.

// This program aproximates a solution to a differential equation

// y'=f(x,y) by using modified Euler's method.

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

double f(double x, double y)

return pow ((3*x*x + y*y),0.5) - y;

int main ()

double a, b, h, x[100], y[100];

int n;

x[0] = 2.0;

y[0] = 0.0;

h = 0.2;
n = 5;

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

a=x[i];

b=y[i];

y[i+1]= y[i] + 0.5*h*(f(a,b) + f(a+h,b+h*f(a,b)));

x[i+1]= x[i] + h;

cout << setw(2) << "n" <<setw(8) << "x" << setw(12) << "y" << endl;

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

a=x[i];

cout << setw(2) << i << setprecision(2) << fixed << setw(10) << x[i] <<
setprecision(6) << fixed

<< setw(14) << y[i] << endl;

return 0;

/*output:

n x y

0 2.00 0.000000

1 2.20 0.664426

2 2.40 1.289969
3 2.60 1.894843

4 2.80 2.489930

5 3.00 3.082024

Press any key to continue . . .

*/
//Name : LEE YING YING

//Matric Number : SER070016

//Question : Exercises 19.7 Numerical Solution of Differential Equation

// (Question 2: y' = xy/[(x^2 + y^2)^1/2] with y(1) = 1 and


h = 0.2 over the

// interval 1 <= x <= 2.)

#include <cmath>

#include <iomanip>

#include <iostream>

using namespace std;

double f(double, double);

double f(double x, double y)

double s=pow(x,2);

double t=pow(y,2);

return x*y/(pow(s+t,0.5)); //By changing this equation to sin(x)-y, we can get


the same result as in the text book!

int main()

double a, b, h, x[100], y[100];

int i,n;
h=0.2; n=5;

x[0]=1.0; y[0]=1.0;

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

a=x[i]; b=y[i];

y[i+1]= y[i] + 0.5*h*(f(a,b) + f(a+h,b+h*(f(a,b))));//modified euler's method

x[i+1]= x[i] + h;

cout << setw(2) << "n" <<setw(6) << "x" << setw(10) << "y(mod)"

<< setw(10) << endl;

cout << fixed;

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

a=x[i];

cout << setw(2) << setprecision(1) << i << setw(6) << setprecision(2) <<
x[i]

<< setw(10) << setprecision(6) << y[i] << endl;

return 0;

}
//Final Assignment SJES 2271

//Numerical Solution of Differential Equation

//Name : Lee Wai Khong

//Matric Number : SER 070014

//Exercises 19.7 Question 3

//Solve the initial value problem by computer using modified Euler's method.

//y'=(x^2+y^2)^0.5-xy with y(1)=2 and h=0.2 over the interval 1<=x<=2.

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

double f(double x,double y)

return pow(x*x+y*y,0.5)-x*y;

int main()

double a,b,h,x[10],y[10];

int n,i;

h=0.2; n=5;// we choose n=5 because x is in interval[1,2]

x[0]=1.0; y[0]=2.0;

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


{

a=x[i]; b=y[i];

x[i+1]= x[i] + h;

y[i+1]= y[i] + 0.5*h*(f(a,b) + f(a+h,b+h*f(a,b)));

cout<<setw(2)<<"n"<<setw(7)<<"x"<<setw(10)<<"y(mod)"<<endl;

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

cout<<fixed<<setw(2)<<i<<setw(8)<<setprecision(2)<<x[i]

<<setprecision(6)<<setw(10)<<y[i]<<endl;

return 0;

}
/*

SJES 2271

Name: Diong Pang Khit

Matric no: SER070007

Exercise 19.7 Question 4

Solve the following initial value problems by computer using the Modified Euler's
Method

Question 4:

y'=1/2*(x*x+2*y*y)-x*y with y(1) = 0 and h = 0.1 over the interval 1<=x<=1.5

*/

#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

double f(double x, double y)

return 0.5*(x*x+2*y*y)-x*y;

int main()

double a,b,h,x[100],y[100];

int n;

y[0]=0;

x[0]=1;

h=0.1;
n=5;

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

a=x[i];

b=y[i];

y[i+1]=y[i]+0.5*h*( f(a,b) + f(a+h,b+h*f(a,b)));

x[i+1]=x[i]+h;

cout<<setw(3)<<"n"<<setw(8)<<"x"<<setw(12)<<"y"<<endl;

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

a=x[i];

cout<<setw(3)<<i<<setprecision(2)<<fixed<<setw(10)<<x[i]<<setprecision(6)<
<fixed<<setw(14)<<y[i]<<"\n";

return 0;

/*

n x y

0 1.00 0.000000

1 1.10 0.052625

2 1.20 0.110241

3 1.30 0.172869

4 1.40 0.240554

5 1.50 0.313384

*/

//Name: Melvin Sta Maria


//Matrix no: SER070020

//Exercises Chapter 19.7

//Solve the following initial value problems by computer using

//the fourth order Runge-Kutta algorithm.

//5. y' = cos(2x + y) - 3y with y(1) = 1 and h = 0.2 over

//the interval 1 <= x <= 2.

#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

double f(double x, double y) //used defined function that defined the equation

return cos((2*x+y)*180*7/22) - 3*y; //the value is change from radian to


degree by mutiplying 180 and

} //deviding 22/7

int main()

double a, b, h, x[100], y[100]; //100 values of x & y are declared

int n;

y[0] = 1; //y[0] is initialized to 1

x[0] = 1; //x[0] is initialized to 1

h = 0.2; //h is assign to 0.2

n = 5; //number af values needed

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

a = x[i];

b = y[i];
y[i+1] = y[i] + 0.5*h*( f(a,b) + f(a+h,b+h*f(a,b))); //modified eular
method

x[i+1] = x[i] + h;

cout << setw(3) << " n " << setw(8) << " x " << setw(12) << " y " <<
endl;

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

a = x[i];

cout << setw(3) << i << setprecision(2) << fixed << setw(10) <<
x[i] ;

cout << setprecision(6) << fixed << setw(14) << y[i] << "\n" ;

return 0;

/*

n x y

0 1.00 1.000000

1 1.20 0.457521

2 1.40 0.395412

3 1.60 0.331753

4 1.80 0.156135

5 2.00 0.143471

Press any key to continue

*/

Potrebbero piacerti anche