Date post: | 08-Sep-2015 |
Category: |
Documents |
Upload: | jyoti-goswami |
View: | 45 times |
Download: | 4 times |
Prepared By:
AVANTIKA YADAV
Numerical Techniques Lab Manual
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 1
List of Programs
S.NO. ALGORITHM/ FLOW CHART/ PROGRAM PAGE NO.
1. To deduce error involved in polynomial equation.
2. To Find out the root of the Algebraic and Transcendental equations using Bisection method.
3. To Find out the root of the Algebraic and Transcendental equations using Regula-Falsi method.
4. To Find out the root of the Algebraic and Transcendental equations using Newton-Raphson method.
5. To Find out the root of the Algebraic and Transcendental equations using Iterative method.
6. To implement Numerical Integration using Trapezoidal rule.
7. To implement Numerical Integration using Simpson 1/3 rule.
8. To implement Numerical Integration Simpson 3/8 rule.
9. To implement Newtons Forward Interpolation formula.
10. To implement Newtons Backward Interpolation formula.
11. To implement Gauss Forward Interpolation formula.
12. To implement Gauss Backward Interpolation formula.
13. To implement Bessels Interpolation formula.
14. To implement Sterlings Interpolation formula.
15. To implement Newtons Divided Difference formula.
16. To implement Langranges Interpolation formula.
17. To implement Numerical Differentiations.
18. To implement Least Square Method for curve fitting.
19. To draw frequency chart like histogram, frequency curve and pie-chart etc.
20. To estimate regression equation from sampled data and evaluate values of standard deviation, t-statistics, regression coefficient,
value of R^2 for at least two independent variables.
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 2
1. Algorithm to deduce error involved in polynomial equation.
Step-1. Start of the program.
Step-2. Input the variable t_val, a_value.
Step-3. Calculate absolute error as
abs_err=|t_val-a_val|
Step-4. Calculate relative error as
rel_err=abs_err/t_val
Step-5. Calculate percentage relative error as
p_rel_err=rel_err*100
Step-6. PRINT abs_err, rel_err and p_rel_err
Step-7. STOP
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 3
Program to deduce error involved in polynomial equation
#include
#include
#include
void main()
{
double abs_err, rel_err, p_rel_err, t_val, a_val;
printf(\n INPUT TRUE VALUE:);
scanf(%lf, &t_val);
printf(\n INPUT APPROXIMATE VALUE:);
scanf(%lf, &a_val);
abs_err=fabs(t_val-a_val);
rel_err=abs_err/t_val;
p_rel_err=rel_err*100;
printf(\nABSOLUTE ERROR= %lf, abs_err);
printf(\nRELATIVE ERROR= %lf, rel_err);
printf(\nPERCENTAGE RELATIVE ERROR= %lf, p_rel_err);
getch();
}
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 4
2. Algorithm of BISECTION METHOD.
Step-1. Start of the program.
Step-2. Input the variable x1, x2 for the task.
Step-3. Check f(x1)*f(x2)
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 5
PROGRAM: BISECTION METHOD.
#include
#include
#include
#include
#include
#define EPS 0.00000005
#define F(x) (x)*log10(x)-1.2
void Bisect();
int count=1,n;
float root=1;
void main()
{
clrscr();
printf("\n Solution by BISECTION method \n");
printf("\n Equation is ");
printf("\n\t\t\t x*log(x) - 1.2 = 0\n\n");
printf("Enter the number of iterations:");
scanf("%d",&n);
Bisect();
getch();
}
void Bisect()
{
float x0,x1,x2;
float f0,f1,f2;
int i=0;
for(x2=1;;x2++)
{
f2=F(x2);
if (f2>0)
{
break;
}
}
for(x1=x2-1;;x2--)
{
f1=F(x1);
if(f1
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 6
printf("\n\t\t ITERATIONS\t\t ROOTS\n");
printf("\t\t-----------------------------------------");
for(;count
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 7
3. Algorithm of FALSE POSITION or REGULA-FALSI METHOD.
Step-1. Start of the program.
Step-2. Input the variable x0, x1,e, n for the task.
Step-3. f0=f(x0)
Step-4. f2=f(x2)
Step-5. for i=1 and repeat if i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 8
PROGRAM: FALSE POSITION or REGULA-FALSI METHOD.
#include
#include
#include
#include
#include
#define EPS 0.00005
#define f(x) 3*x+sin(x)-exp(x)
void FAL_POS();
void main()
{
clrscr();
printf("\n Solution by FALSE POSITION method\n");
printf("\n Equation is ");
printf("\n\t\t\t 3*x + sin(x)-exp(x)=0\n\n");
FAL_POS();
}
void FAL_POS()
{
float f0,f1,f2;
float x0,x1,x2;
int itr;
int i;
printf("Enter the number of iteration:");
scanf("%d",&itr);
for(x1=0.0;;)
{
f1=f(x1);
if(f1>0)
{
break;
}
else
{
x1=x1+0.1;
}
}
x0=x1-0.1;
f0=f(x0);
printf("\n\t\t-----------------------------------------");
printf("\n\t\t ITERATION\t x2\t\t F(x)\n");
printf("\t\t--------------------------------------------");
for(i=0;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 9
{
x2=x0-((x1-x0)/(f1-f0))*f0;
f2=f(x2);
if(f0*f2>0)
{
x1=x2;
f1=f2;
}
else
{
x0=x2;
f0=f2;
}
if(fabs(f(2))>EPS)
{
printf("\n\t\t%d\t%f\t%f\n",i+1,x2,f2);
}
}
printf("\t\t--------------------------------------------");
printf("\n\t\t\t\tRoot=%f\n",x2);
printf("\t\t-------------------------------------------");
getch();
}
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 10
4. Algorithm of NEWTON-RAPHSON METHOD.
Step-1. Start of the program.
Step-2. input the variables x0, n for the task.
Step-3. input Epsilon & delta
Step-4. for i= 1 and repeat if i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 11
PROGRAM: NEWTON RAPHSON METHOD.
# include
# include
# include
# include
# include
# define f(x) 3*x -cos(x)-1
# define df(x) 3+sin(x)
void NEW_RAP();
void main()
{
clrscr();
printf ("\n Solution by NEWTON RAPHSON method \n");
printf ("\n Equation is: ");
printf ("\n\t\t\t 3*X - COS X - 1=0 \n\n ");
NEW_RAP();
getch();
}
void NEW_RAP()
{
long float x1,x0;
long float f0,f1;
long float df0;
int i=1;
int itr;
float EPS;
float error;
for(x1=0;;x1 +=0.01)
{
f1=f(x1);
if (f1 > 0)
{
break;
}
}
x0=x1-0.01;
f0=f(x0);
printf(" Enter the number of iterations: ");
scanf(" %d",&itr);
printf(" Enter the maximum possible error: ");
scanf("%f",&EPS);
if (fabs(f0) > f1)
{
printf("\n\t\t The root is near to %.4f\n",x1);
}
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 12
if(f1 > fabs(f(x0)))
{
printf("\n\t\t The root is near to %.4f\n",x0);
}
x0=(x0+x1)/2;
for(;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 13
5. ALGORITHM FOR ITERATION METHOD.
Algorithm 1
Step-1. Read x0, e, n
x0 is the initial guess, e is the allowed error in root, n is total iterations to be
allowed for convergence.
Step-2. x1 g(x0)
Steps 4 to 6 are repeated until the procedure converges to a root or iterations
reach n.
Step-3. For i = 1 to n in steps of 1 do
Step-4. x0 x1
Step-5. x1 g(x0)
Step-6. If e then, GO TO 9
Step-7. end for.
Step-8. Write Does not converge to a root, x0, x1
Step-9. Stop
Step-10. Write converges to a root, i, x1
Step-11. Stop.
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 14
Algorithm 2 (Iteration Method)
Step-1. Define function f(x)
Step-2. Define function df(x)
Step-3. Get the value of a, max_err.
Step-4. Initialize j
Step-5. If df(a) < 1 then b = 1, a = f(a)
Step-6. Print root after j, iteration is f(a)
Step-7. If fabs(b a) > max_err then
Step-8. j++, goto (5)
Step-9. End if
Step-10. Else print root doesnt exist
Step-11. End.
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 15
PROGRAM: ITERATION METHOD.
#include
#include
#include
#define EPS 0.00005
#define F(x) (x*x*x + 1)/2
#define f(x) x*x*x - 2*x + 1
void ITER();
void main ()
{
clrscr();
printf("\n\t Solution by ITERATION method - ");
printf("\n\t Equation is - ");
printf("\n\t\t\t\t X*X*X - 2*X + 1 = 0\n");
ITER();
getch();
}
void ITER()
{
float x1,x2,x0,f0,f1,f2,error;
int i=0,n;
for(x1=1;;x1++)
{
f1=F(x1);
if (f1>0)
break;
}
for(x0=x1-1;;x0--)
{
f0=f(x0);
if(f0
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 16
x2=F(x2);
error=fabs(f2-f1);
if(errorEPS)
printf("\n\n\t NOTE:- The number of iterations are not sufficient.");
printf("\n\n\n\t\t\t------------------------------");
printf("\n\t\t\t The root is %.4f",f2);
printf("\n\t\t\t-----------------------------");
}
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 17
6. ALGORITHM OF TRAPEZOIDAL RULE
Step-1. Start of the program.
Step-2. Input Lower limit a
Step-3. Input Upper Limit b
Step-4. Input number of sub intervals n
Step-5. h=(b-a)/n
Step-6. sum=0
Step-7. sum=fun(a)+fun(b)
Step-8. for i=1; i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 18
PROGRAM: TRAPEZOIDAL METHOD.
# include
# include
# include
# include
# include
float fun(float);
void main()
{
float result=1;
float a,b;
float h,sum;
int i,j;
int n;
clrscr();
printf("\n\n Enter the range - ");
printf("\n\n Lower Limit a - ");
scanf("%f" ,&a);
printf("\n\n Upper Limit b - ");
scanf("%f" ,&b);
printf("\n\n Enter number of subintervals - ");
scanf("%d" ,&n);
h=(b-a)/n;
sum=0;
sum=fun(a)+fun(b);
for(i=1;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 19
7. ALGORITHM OF SIMPSONS 1/3rd RULE
Step-1. Start of the program.
Step-2. Input Lower limit a
Step-3. Input Upper limit b
Step-4. Input number of subintervals n
Step-5. h=(ba)/n
Step-6. sum=0
Step-7. sum=fun(a)+4*fun(a+h)+fun(b)
Step-8. for i=3; i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 20
PROGRAM: SIMPSONS 1/3rd
METHOD OF NUMERICAL INTEGRATION
#include
#include
#include
#include
#include
float fun(float);
void main()
{
float result=1;
float a,b;
float sum,h;
int i,j,n;
clrscr();
printf("\n Enter the range - ");
printf("\n Lower Limit a - ");
scanf("%f",&a)
;printf("\n Upper limit b - ");
scanf("%f",&b);
printf("\n\n Enter number of subintervals - ");
scanf("%d",&n);
h=(b-a)/n;
sum=0;
sum=fun(a)+4*fun(a+h)+fun(b);
for(i=3;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 21
8. ALGORITHM OF SIMPSONS 3/8th RULE
Step-1. Start of the program.
Step-2. Input Lower limit a
Step-3. Input Upper limit b
Step-4. Input number of sub itervals n
Step-5. h = (b a)/n
Step-6. sum = 0
Step-7. sum = fun(a) + fun (b)
Step-8. for i = 1; i < n; i++
Step-9. if i%3=0:
Step-10. sum + = 2*fun(a + i*h)
Step-11. else:
Step-12. sum + = 3*fun(a+(i)*h)
Step-13. End of loop i
Step-14. result = sum*3*h/8
Step-15. Print Output result
Step-16. End of Program
Step-17. Start of Section fun
Step-18. temp = 1/(1+(x*x))
Step-19. Return temp
Step-20. End of section fun
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 22
PROGRAM: SIMPSONS 3/8th
METHOD OF NUMERICAL INTEGRATION
#include
#include
float fun(int);
void main()
{
int n,a,b,i;
float h, sum=0, result;
//clrscr();
printf("enter range");
scanf("%d",&n);
printf("enter lower limit");
scanf("%d",&a);
printf("enter upper limit");
scanf("%d",&b);
h=(b-a)/n;
sum=fun(a)+fun(b);
for(i=0;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 23
9. Algorithm for Newtons Forward method of interpolation
Step-1. Start of the program
Step-2. Input number of terms n
Step-3. Input the array ax
Step-4. Input the array ay
Step-5. h=ax[1] ax[0]
Step-6. for i=0; i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 24
PROGRAM: NEWTONS FORWORD METHOD OF INTERPOLATION
# include
# include
# include
# include
# include
void main()
{
int n;
int i,j;
float ax[10];
float ay[10];
float x;
float y = 0;
float h;
float p;
float diff[20][20];
float y1,y2,y3,y4;
clrscr();
printf("\n Enter the number of terms - ");
scanf("%d",&n);
printf("Enter the value in the form of x - ");
for (i=0;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 25
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
i=0;
do
{
i++;
}
while(ax[i]
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 26
10. Algorithm for Newtons Backward method of interpolation
Step-1. Start of the program.
Step-2. Input number of terms n
Step-3. Input the array ax
Step-4. Input the array ay
Step-5. h=ax[1]-ax[0]
Step-6. for i=0; i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 27
PROGRAM: NEWOTNS BACKWARD METHOD OF INTERPOLATION
#include
#include
#include
#include
#include
void main()
{
int n,i,j,k;
float mx[10],my[10],x,x0=0,y0,sum,h,fun,p,diff[20][20],y1,y2,y3,y4;
clrscr();
printf("\n enter the no. of terms - ");
scanf("%d",&n);
printf("\n enter the value in the form of x - ");
for(i=0;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 28
}
x0=mx[i];
sum=0;
y0=my[i];
fun=1;
p=(x-x0)/h;
sum=y0;
for(k=1;k
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 29
11. Algorithm of GAUSS FORWARD interpolation formula
Step-1. Start of the program.
Step-2. Input number of terms n
Step-3. Input the array ax
Step-4. Input the array ay
Step-5. h=ax[1]-ax[0]
Step-6. for i=0;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 30
PROGRAM: GAUSSS FORWORD METHOD OF INTERPOLATION
# include
# include
# include
# include
# include
void main()
{
int n;
int i,j;
float ax[10];
float ay[10];
float x;
float nr,dr;
float y=0; float h;
float p;
float diff[20][20];
float y1,y2,y3,y4;
clrscr();
printf(" Enter the number of terms - ");
scanf("%d",&n);
printf("\n Enter the value in the form of x - ");
for (i=0;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 31
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
i=0;
do {
i++;
}
while(ax[i]
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 32
12. Algorithm of Gausss Backward interpolation formula
Step-1. Start of the program.
Step-2. Input number of terms n
Step-3. Input the array ax
Step-4. Input the array ay
Step-5. h=ax[1]-ax[0]
Step-6. for i=0;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 33
PROGRAM: GAUSSS BACKWARD METHOD OF INTERPOLATION.
# include
# include
# include
# include
# include
void main()
{
int n;
int i,j; float ax[10];
float ay[10];
float x;
float y=0;
float h;
float p;
float diff[20][20];
float y1,y2,y3,y4;
clrscr();
printf("\n Enter the number of terms - ");
scanf("%d",&n);
printf("\n Enter the value in the form of x - ");
for (i=0;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 34
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
i=0;
do {
i++;
}
while (ax[i]
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 35
13. Algorithm to implement BESSELS INTERPOLATION FORMULA
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 36
Program to implement BESSELS INTERPOLATION FORMULA in C.
#include
#include
#include
#include
#include
void main()
{
int n; // no. of terms.
int i,j; // Loop variables
float ax[10]; // 'X' array limit 9
float ay[10]; // 'Y' array limit 9
float x; // User Query for what value of X
float y; // Calculated value for coressponding X.
float h; // Calc. Section
float p; // Calc. Section
float diff[20][20]; // to store Y
float y1,y2,y3,y4; // Formulae variables.
clrscr();
printf("\t\t !! BESSELS INTERPOLATION FORMULA!! ");
// Input section.
printf("\n\n Enter the no. of terms -> ");
scanf("%d",&n);
// Input Sequel for array X
printf("\n\n Enter the value in the form of x -> ");
// Input loop for X.
for(i=0;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 37
printf("\n\n Enter the value of x for ");
printf("\n which u want the value of y -> ");
scanf("%f",&x);
// Calculation and processing section.
h=ax[1]-ax[0];
for(i=0;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 38
14. Algorithm of Sterlings Interpolation Formula
Step-1. Start of the program.
Step-2. Input number of terms n
Step-3. Input the array ax
Step-4. Input the array ay
Step-5. h = ax[1]-ax[0]
Step-6. for i = 1;i < n-1; i++
Step-7. diff [i][1] = ay[i + 1]-ay[i]
Step-8. End loop i
Step-9. for j = 2; j < = 4; j++
Step-10. for i = 0; i < n-j; i++
Step-11. diff[i][j] = diff[i + 1][j-1]-diff[i][j-1]
Step-12. End loop i
Step-13. End loop j
Step-14. i = 0
Step-15. Repeat step 16 until ax[i] < x
Step-16. i = i + 1
Step-17. i = i-1;
Step-18. p = (x-ax[i])/h
Step-19. y1= p*(diff[i][1] + diff[i-1][1])/2
Step-20. y2 = p*p*diff[i-1][2]/2
Step-21. y3 = p*(p*p-1)*(diff[i-1][3]+diff[i-2][3])/6
Step-22. y4 = p*p*(p*p-1)*diff[i-2][4]/24
Step-23. y = ay[i]+y1 + y2 + y3 + y4
Step-24. Print output
Step-25. End of program
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 39
PROGRAM: STERLINGS METHOD OF INTERPOLATION.
#include
#include
#include
#include
void main()
{
int n;
int i,j;
float ax[10];
float ax[10];
float h;
float p;
float diff[20][20];
float x,y;
float y1,y2,y3,y4;
clrscr();
printf("\n Enter the value of terms");
scanf("%d",%n);
printf(\n Enter the values for x \n);
for(i=0;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 40
}
i=0;
do {
i++;
}
while(ax[i]
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 41
15. Algorithm to implement Newtons Divided Difference formula.
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 42
Program to implement Newtons Divided Difference formula.
#include
#include
#include
void main()
{
float x[10],y[10][10],sum,p,u,temp;
int i,n,j,k=0,f,m;
float fact(int);
clrscr();
printf("\nhow many record you will be enter: ");
scanf("%d",&n);
for(i=0; i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 43
}
i=0;
do
{
if(x[i]
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 44
16. Algorithm of LAGRANGES INTERPOLATION FORMULA.
Step-1. Start of the program
Step-2. Input number of terms n
Step-3. Input the array ax
Step-4. Input the array ay
Step-5. for i=0; i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 45
PROGRAM: LAGRANGES INTERPOLATION FORMULA.
#include
#include
#define MAX 10
void main()
{
float x[MAX],y[MAX],k=0,z,nr,dr;
int i,j,m;
//clrscr();
printf("\n enter the range ");
scanf("%d",&m);
printf("\n enter the x value ");
for(i=0;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 46
17. Algorithm to implement Numerical Differentiations.
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 47
Program to implement Numerical Differentiations.
#include
#include
#include
float F (float x);
void deriv (float f(float), float x, int n, float h, float **D)
{
int i, j;
unsigned powerof4;
for (i = 0; i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 48
/**********************************************************************/
/* Programmer's comment: */
/* if only one function will be called, there's no need to pass the */
/* function as argument. In that case, the procedure deriv will have */
/* this format: void deriv(float, int, float, float **) */
/* */
/**********************************************************************/
for (i = 0; i < n; i++)
for (j = 0; j
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 49
18. Algorithm to implement Least Square Method for curve fitting.
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 50
Program to implement Least Square Method for curve fitting.
#include
#include
#include
main()
{
float x[10],y[10],a[10][10];
int i,j,k,n,itr;
printf("\n ENTER THE SIZE OF MATRIX n:");
scanf("%d",&n);
printf("\n ENTER MATRIX ELEMENTS AND RHS:\n");
for(i=1;i
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 51
}
goto top;
}
else
continue;
return;
}
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 52
19. Algorithm to draw frequency chart like histogram, frequency curve and pie-chart etc.
Numerical Techniques Lab Manual
Avantika Yadav KEC Page 53
Program to draw frequency chart like histogram, frequency curve and pie-chart etc.