Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2, Question1
//Date : 01/03/09
#include <iostream>
#include <iomanip>
#include <cmath>
int main()
double a, b, p, x, con;
cin>>a>>b;
p = f(a)*f(b); //bisection method can only be used if p<0
while(p>=0)
cin>>a>>b;
cin>>con;
cin>>max_bi;
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;
{
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";
a = x;
else
b = x;
no++;
cout<<"No Convergence\n";
else
cout<<"x = "<<x<<"(+/-)"<<con<<"\n";
}
return 0;
double f(double x)
}
// Chew Sie Wai
// SES 060086
//In Exercise 1 through 6 use the bisection method to find to required root.
#include <iostream>
#include <cmath>
#include <iomanip>
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;
cin>>b;
cin>>eps;
cin>>k_max;
cout<<" k a b x f(x)\n";
k=1;
x=0.5*(a+b);
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
#include <iostream>
#include <iomanip>
#include <cmath>
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;
cin>>a>>b>>eps>>kmax;
cout<<"k a b x f(x)\n";
k=1;
x=0.5*(a+b);
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
(x^3)-(1.9)x^2-(2.3)x+3.7=0 */
#include <iostream>
#include <iomanip>
#include <cmath>
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;
k=1;
x=0.5*(a+b);
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;
}
/*
*/
#include <iostream>
#include <iomanip>
#include <cmath>
float f(float x)
return x*x*x-4.5*x*x+1.3*x+8;
int main()
int max,d;
float a,b,c,x;
cout<<"a = ";
cin>>a;
cout<<"b = ";
cin>>b;
cout<<"The desired accuracy = ";
cin>>c;
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);
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++;
cout<<"No Convergence"<<endl;
return 0;
}
/*
Output
a = -1.3
b = -0.9
Results :
k a b x f(x)
*/
//Lim Horng Cherng SER070018
#include <iostream>
#include <iomanip>
#include <cmath>
double f(double x)
{double b=sqrt(1-x*x);
return ((0.5)*b)-(x*x);
int main( )
int imax,i;
cin>>a;
cout<<"b: ";
cin>>b;
cout<<"eps: ";
cin>>eps;
cout<<"imax: ";
cin>>imax;
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);
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
a: -0.55
b: -0.65
eps: 1e-6
imax: 20
i a b x f(x)
a: 0.60
b: 0.70
eps: 1e-6
imax: 20
i a b x f(x)
*/
// Author : Chong Foo Weng (SES050131)
// Date : 11/03/2009
#include<iostream>
#include<cmath>
#include<iomanip>
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<<endl;
cin>>x[0];
cin>>eps;
cout<<endl;
<<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 ;
int main ()
{
double y[10];
int i;
i=0;
cin>>y[i];
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<<f0(y[i])<<endl;
return 0 ;
}
//Author: MOHAMMAD FARID BIN MOHD SHAH
//Question 17
#include <iostream>
#include <cmath>
#include<iomanip>
double f(double);
double fprime(double);
double newton(double[],double);
void main()
cout<<"\n";
cout<<"\n";
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)"
return 3 + exp((-1)*x);
int i, N;
double dif;
cin>>N;
cout<<endl;
{
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
return x[N+1]; //returning the value of the root(or when condition is not
satisfied)
/*output
f(x0)=1.95067
f'(x0)=3.44933
No. of iterations: N = 8
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
//
// Name: TAI MENG CHUI
// Sketching graphs y=tanh x and y = 2 tan x -1 we see there are many positive
roots.
#include <iostream>
#include <cmath>
#include <iomanip>
double f(double);
double fp(double);
int main()
cin>>x0;
cout<<"x0="<<x0<<"\n";
int n=0;
// stop the program if the solution not found after kmax iterations
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 << "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
// Exercise 19.4
// Question 1
#include <iostream>
#include<iomanip>
#include <cmath>
int main()
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;
return 0;
return 2*pow(x,3)-3*pow(x,2)+4*x-1;
return 0.5*pow(x,4)-pow(x,3)+2*pow(x,2)-x;
int n;
long double x;
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
//Exercise 19.4
//Question 2
h = 0.1 to evaluate,
I= Intergrate (dx/(1+x*x)
//Date: 12.3.2009
#include <iostream>
#include <cmath>
float f(float x) {
float function;
function=1/(1+x*x);
return function;
float x=a;
x= x+h;
sum += f(x);
return sum;
float g (float x)
float eror;
eror= -2*x/pow(1+x*x,2);
return eror;
// Simpson'rule
float simpson,c;
c=(a+b)/2;
return simpson;
int main()
{
float a,b,area;
double h;
cout << "Input lower limit a, upper limit b, and step size.\n";
cout << "a = " << a << " b = " << b << " h= "<< h <<endl;
cout << "Integral = " << area << " for h = " << h <<".\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
#include <iostream>
#include <cmath>
{long double f;
f=pow(x,2)*exp(-x);
return f;}
{x=x+h;
value=value+f(x);
} return value*h;
if (n==2)
{s=g/3*(f(a)+4*f(a+g)+f(b));
return s;}
else
sum+=f(a+j*g);
return g/3*(f(a)+4*s+2*sum+f(b));}
int main()
{int n;
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;
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
*/
//Name: TEH CHONG HENG
//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>
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.
for(int i=1;i<=n;i++)
sum1+=f(a+(2*i-1)*h);
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;
a=0.0;
b=pi;
h=pi/8;
cout<<"Given that:\n";
cout<<"\n";
u=simp(0,pi,pi/8)/pi;//J2(2)=(1/pi)*(integral of f(y))
cout<<"\n";
return 0;
/*Sample Output:
Given that:
Difference between the estimated value and the actual value = 0.000401*/
//Name: KHAW ZHAN HAO
//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>
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.
for(int i=1;i<=n;i++)
sum1+=f(a+(2*i-1)*h);
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;
a=0.0;
b=pi;
h=pi/10;
cout<<endl;
cout<<"The value of
J1(4)="<<fixed<<setprecision(6)<<simp(a,b,h)/pi<<"\n";
cout<<endl;
return 0;
//Sample Output:
/*We are given that:
The difference between the estimated value and the actual value=0.000300 */
// Assignment SJES 2271
// 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.
#include <iostream>
#include <cmath>
#include <iomanip>
int main ()
int n;
x[0] = 2.0;
y[0] = 0.0;
h = 0.2;
n = 5;
a=x[i];
b=y[i];
x[i+1]= x[i] + h;
cout << setw(2) << "n" <<setw(8) << "x" << setw(12) << "y" << endl;
a=x[i];
cout << setw(2) << i << setprecision(2) << fixed << setw(10) << x[i] <<
setprecision(6) << fixed
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
*/
//Name : LEE YING YING
#include <cmath>
#include <iomanip>
#include <iostream>
double s=pow(x,2);
double t=pow(y,2);
int main()
int i,n;
h=0.2; n=5;
x[0]=1.0; y[0]=1.0;
a=x[i]; b=y[i];
x[i+1]= x[i] + h;
cout << setw(2) << "n" <<setw(6) << "x" << setw(10) << "y(mod)"
a=x[i];
cout << setw(2) << setprecision(1) << i << setw(6) << setprecision(2) <<
x[i]
return 0;
}
//Final Assignment SJES 2271
//Solve the initial value problem by computer using modified Euler's method.
#include <iostream>
#include <cmath>
#include <iomanip>
return pow(x*x+y*y,0.5)-x*y;
int main()
double a,b,h,x[10],y[10];
int n,i;
x[0]=1.0; y[0]=2.0;
a=x[i]; b=y[i];
x[i+1]= x[i] + h;
cout<<setw(2)<<"n"<<setw(7)<<"x"<<setw(10)<<"y(mod)"<<endl;
cout<<fixed<<setw(2)<<i<<setw(8)<<setprecision(2)<<x[i]
<<setprecision(6)<<setw(10)<<y[i]<<endl;
return 0;
}
/*
SJES 2271
Solve the following initial value problems by computer using the Modified Euler's
Method
Question 4:
*/
#include <iostream>
#include <iomanip>
#include <cmath>
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;
a=x[i];
b=y[i];
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
*/
#include <iostream>
#include <iomanip>
#include <cmath>
double f(double x, double y) //used defined function that defined the equation
} //deviding 22/7
int main()
int n;
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
*/