Upload
hassan-usaid
View
219
Download
0
Embed Size (px)
Citation preview
8/2/2019 12. Numerical Methods
1/22
NUMERICAL METHODS
Programs
8/2/2019 12. Numerical Methods
2/22
1. Successive approximation method
Algorithm
1. Start
2. Define the iterative equation
3. Input x0, initial guess
4. Input the accuracy
5. Input the max iterations suggested, n
6. For i =1 to n, increment in steps of 1a. x1= f(x0)
b. Error=|(x1-x0)/x1|
c. x0 =x1
d. If (error
8/2/2019 12. Numerical Methods
3/22
void main()
{ float f(float );
float x0,x1, error, acc;
int i, n;
cin>>x0>>acc>>n;
for(i=0; i
8/2/2019 12. Numerical Methods
4/22
2. Newton Raphson method
1. Start
2. Input x0, acc
3. K=0
4. x[k+1] = x[k] - f(x[k]) / f1(x[k])
5. Error=fabs ((x[k+1] - x[k]) / x[k+1])
6. If (error>= accy)
k=k+1; go to step 4;
7. Print x[k+1]
8. stop
8/2/2019 12. Numerical Methods
5/22
void main()
{
double x[200], accy;
int k = 0;
double f(double a);
double f1(double a);
void where(void);
where();
cout x[0];cout accy;
x[1] = x[0] - f(x[0]) / f1(x[0]);
2. Newton Raphson method
8/2/2019 12. Numerical Methods
6/22
cout
8/2/2019 12. Numerical Methods
7/22
// Subroutine to evaluate the given function
double f(double a)
{double y;
y = a * a * a - 3 * a * a + a + 1;
return(y);
}
// Subroutine to evaluate the derivative of the given function
double f1(double a)
{
double y;
y = 3 * a * a - 6 * a + 1;return(y);
}
2. Newton Raphson method
8/2/2019 12. Numerical Methods
8/22
//Subroutine to determine the intervals where the roots lie
void where(void)
{
float y0, y1, j;
for(j = -50; j
8/2/2019 12. Numerical Methods
9/22
3. Regula Falsi method
1. Read x0, x1, error, n
2. f0 =f(x0)
3. f1= f(x1)
4. For (i = 1 to n) in steps of 1 do
5. x2= (x0*f1-x1*f0)/(f1-f0)
6. f2= f(x2)
7. If |f2|0) x0=x2
else x1=x2
End for loop
9. stop
8/2/2019 12. Numerical Methods
10/22
3. Regula Falsi methodvoid main()
{ int n, i;
float x0,x1,x2,error;cin>>x0>>x1;
cin>>error;
float f0, f1, f2;
f0=f(x0);
f1=f(x1);
do
{ x2=(x0*f1- x1*f0)/(f1- f0);
f2=f(x2);
if ((f0*f2)>0)
{ x0 = x2;
f0 = f2;
}
else
{ x1=x2;
f1 = f2;}
} while (f2>error);
}
8/2/2019 12. Numerical Methods
11/22
4. Fitting a straight line
void main()
{float x[20], y[20];
int i, n;
float s1, s2, s3, s4;
cout n;
for( i = 0; i < n; i++)
{
cout
8/2/2019 12. Numerical Methods
12/22
5. Fitting a parabolavoid main()
{
float x[20], y[20], a[5][5];
int i, n;
float s1, s2, s3, s4, s5, s6, s7;
void guass(float a[5][5]);
cout n;
for( i = 0; i < n; i++)
{
cout
8/2/2019 12. Numerical Methods
13/22
a[0][0] = (float) n;
a[0][1] = s1;
a[0][2] = s3;a[0][3] = s2;
a[1][0] = s1;
a[1][1] = s3;
a[1][2] = s4;
a[1][3] = s6;a[2][0] = s3;
a[2][1] = s4;
a[2][2] = s5;
a[2][3] = s7;
guass(a);
}
//Solution using Guass eliminationmethod
void guass(float a[5][5]){
float x[5], ratio;
int n = 3, i, j, k;
for(k = 0; k < n - 1; k++)
for(i = k + 1; i < n; i++){
ratio = a[i][k] / a[k][k];
for(j = 0; j < n + 1; j++)
a[i][j] -= ratio * a[k][j];
}
5. Fitting a parabola
8/2/2019 12. Numerical Methods
14/22
5. Fitting a parabola
//Back substitution
x[n-1] = a[n - 1][n] / a[n - 1][n-1];
for(k = n - 2; k >= 0; k--)
{
x[k] = a[k][n];
for(j = k + 1;j < n; j++)
x[k] -= a[k][j] * x[j];
x[k] /= a[k][k];
}
cout
8/2/2019 12. Numerical Methods
15/22
6. Trapezoidal rulevoid main()
{
float a, b, h, x0, xn, xi;
double sum;
int n, i;
double funct(float a);
cout a >>b;
cout >n;
h = (b - a) / n;
x0 = a;
xn = b;
sum = funct (x0) + funct (xn);
cout
8/2/2019 12. Numerical Methods
16/22
7. Simpsons rulevoid main()
{
float a, b, h, x0, xn, xi;
double sum;int n, i;
double funct(float a);
cout a >>b;
cout n;
h = (b - a) / n;
x0 = a;
xn = b;
sum = funct (x0) + funct (xn);
cout
8/2/2019 12. Numerical Methods
17/22
double funct(float a)
{
double b;
b = exp (-a * a / 2);
return (b);
}
7. Simpsons rule
8/2/2019 12. Numerical Methods
18/22
8. Gauss elimination method
void main()
{
float a[20][20], ratio, x[20];int i, j, k, n;
cout >n;
cout a[i][j];//Triangularisation
for(k = 0; k < n - 1; k++)
for(i = k + 1; i < n; i++)
{
ratio = a[i][k] / a[k][k];
for(j = 0; j < n + 1; j++)
a[i][j] -= ratio * a[k][j];
}
//Back substitution
x[n-1] = a[n - 1][n] / a[n - 1][n-1];
for(k = n - 2; k >= 0; k--)
{
x[k] = a[k][n];
for(j = k + 1;j < n; j++)
x[k] -= a[k][j] * x[j];
x[k] /= a[k][k];
}
for(i = 0; i < n; i++)cout
8/2/2019 12. Numerical Methods
19/22
9. Gauss quadraturevoid main()
{
double c[10], x[10];double sum = 0;
int n, i;
double funct(double a);
cout n;
switch(n)
{case 2: c[0] = c[1] = 1;
x[0] = -0.57735;
x[1] = 0.57735;
break;
case 3 : c[0] = c[2] = 0.55556;
c[1] = 0.88889;
x[0] = -0.77460;x[1] = 0;
x[2] = 0.77460;
break;
case 4 :c[0] = c[3] = 0.34785;c[1] = c[2] = 0.65215;
x[0] = -0.86114;
x[1] = -0.33998;
x[2] = 0.33998;
x[3] = 0.86114;
break;
8/2/2019 12. Numerical Methods
20/22
case 5 :c[0] = c[4] = 0.23693;
c[1] = c[3] = 0.47863;
c[2] = 0.56889;
x[0] = -0.90618;
x[1] = -0.53847;
x[2] = 0;
x[3] = 0.53847;
x[4] = 0.90618;
break;
}
for(i = 0; i < n; i++)
sum += c[i] * funct(x[i]);
cout
8/2/2019 12. Numerical Methods
21/22
10. Gauss- Siedel Iterationvoid main()
{
float a[10][10],x[10],y[10];
int i,j,n;
clrscr();
/* inputting the coefficients*/
coutn;
cout
8/2/2019 12. Numerical Methods
22/22
/* display of coefficients */
cout