BCSL-058 Computer Oriented Numerical Techniques Lab
BCSL-058 Computer Oriented Numerical Techniques Lab
1
Exercise 1.
Write a program in C to calculate the value of ex by using its series expansion, given below :
Evaluate ex only up to first three terms. Also find the value of ex by
using the inbuilt function in C. Compare the results produced by inbuilt function and the result
produced by you, to find the error.
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float e, diff;
int x=1;
printf("\n This program compares value of e^x using:");
printf("\n 1. Built in funtion");
printf("\n 2. Series evaluation upto first three terms.");
e=1+x+(pow(x,2)/2)+(pow(x,3)/6);
printf("\n\n Using built in function the value of e^x = %f",exp(x));
printf("\n\n Using program, the value of e^x= 1+x+(x^2)/2!+(x^3)/3! = %f", e);
diff=exp(x)-e;
printf("\n\n Difference in both values = %f", diff);
printf("\n\n Relative error : %f % ",exp(x)*diff) ,
getch();
}
OUTPUT
2
Exercise 2.
Write a program in C, to calculate the value of cos (x) by using its series expansion given below :
cos (x) . Evaluate cos (x) only up to first three terms. Also find the value
of cos (x) by using the inbuilt function in C. Compare the results i.e the result produced by your
program for series and that produced by inbuilt function. Based on comparison, determine the error.
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a, diff;
int x=1;
printf("\n This program compares value of cos(x) using:");
printf("\n 1. Built in funtion");
printf("\n 2. Series evaluation upto first three terms.");
a=1-(pow(x,2)/2)+(pow(x,4)/24) -(pow(x,6)/720);;
printf("\n\n Using built in function the value of cos(x) = %f",cos(x));
printf("\n\n Using program, the value of cos(x)=x= 1-(x^2)/2!+(x^4)/4! -(x^6)/6! = %f", a);
diff=cos(x)-a;
printf("\n\n Difference in both values = %f", diff);
printf("\n\n Relative error : %f %",cos(x)*diff) ,
getch();
}
OUTPUT
3
Exercise 3.
Write a program to calculate the sin of a value given in radians, using the formula:
sin (x) Evaluate sin(x) only up to first three terms. Also find the value of sin(x)
by using the inbuilt function in C. Compare the results i.e the result produced by your programme for
series and that produced by inbuilt function. Based on comparison, determine the error.
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a, diff;
int x;
printf("\n This program compares value of sin(x) using:");
printf("\n 1. Built in funtion");
printf("\n 2. Series evaluation upto first three terms.");
printf("\n Enter the value of x: ");
scanf("%d", &x);
a=x-(pow(x,3)/6)+(pow(x,5)/120)-(pow(x,7)/5040);
printf("\n\n Using built in function the value of sin(x) = %f",sin(x));
printf("\n\n Using program, the value of sin(x)= x-(x^3)/3!+(x^5)/5!-(x^7)/7! = %f", a);
diff=sin(x)-a;
printf("\n\n Difference in both values = %f", diff);
printf("\n\n Relative error : %f %",sin(x)*diff) ,
getch();
}
OUTPUT
4
Exercise 4.
Write a program in C, to find the solution of following system of equations, by using GUASS
ELIMINATION METHOD.
4x + y + z = 4
x + 4y -2z = 4
—x + 2y - 4z =2
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k,n;
float a[10][10],x[10];
float s,p;
printf("\n -----GAUSSIAN ELIMINATION METHOD-----\n");
// Entering variables
printf(" Enter number of equations : ");
scanf("%d",&n) ;
printf("\n Enter corresponding co-efficients :\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" Enter element at: a[%d][%d]= ",i+1,j+1);
scanf("%f",&a[i][j]);
}
printf(" Enter: d[%d]= ",i+1);
scanf("%f",&a[i][n]);
printf("\n");
}
//Lower triangularisation
for(k=0;k<n-1;k++)
{
for(i=k+1;i<n;i++)
{
p = a[i][k] / a[k][k] ;
for(j=k;j<n+1;j++)
a[i][j]=a[i][j]-p*a[k][j];
}
}
//Back substitution
x[n-1]=a[n-1][n]/a[n-1][n-1];
5
for(i=n-2;i>=0;i--)
{
s=0; for(j=i+1;j<n;j++)
{
s +=(a[i][j]*x[j]);
x[i]=(a[i][n]-s)/a[i][i];
}
}
//Displaying results:
printf("\n THE RESULTS ARE:\n");
for(i=0;i<n;i++)
printf("\n x[%d]= %f",i+1,x[i]);
getch();
}
OUTPUT
6
Exercise 5.
Write a program in C, to find the root of following equation by using "BISECTION METHOD".
Equation : x3 — 5x +1= 0
Solution:
#include<stdio.h>
#include <math.h>
#include<conio.h>
#define ESP 0.001
#define F(x) (x)*(x)*(x) - 5*x + 1
void main()
{
int i = 1;
float x0,x1,x2;
double f1,f2,f0,t;
clrscr();
printf("\n ===BISECTION METHOD===");
printf("\n This program evaluates y=x^3-5x+1");
printf("\n Enter the value of x0: ");
scanf("%f",&x0);
printf("\n Enter the value of x1: ");
scanf("%f",&x1);
printf("\n_________________________________________________________________\n");
printf("\n ITN.\t x0\t x1\t x2\t f0\t f1\t f2");
printf("\n_________________________________________________________________\n");
do
{
x2=(x0+x1)/2;
f0=F(x0);
f1=F(x1);
f2=F(x2);
printf("\n %d %f %f %f %lf %lf %lf", i, x0,x1,x2,f0,f1,f2);
if(f0*f2<0)
{
x1=x2;
}
else
{
x0=x2;
}
i++;
}
while(fabs(f2)>ESP);
printf("\n_________________________________________________________________\n");
printf("\n\n Approximate root = %f",x2);
getch();
}
7
OUTPUT
8
Exercise 6:
Write a program in C, to find the root of equation x3 — 5x +1 = 0 by using NEWTON RAPHSON
METHOD.
Solution:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int user_power,i=0,cnt=0,flag=0;
int coef[10]={0};
float x1=0,x2=0,t=0;
float fx1=0,fdx1=0;
void main()
{
clrscr();
printf("\n\n\ -----PROGRAM FOR NEWTON RAPHSON METHOD-----");
printf("\n\n\n ENTER HIGHEST POWER : ");
scanf("%d",&user_power);
for(i=0;i<=user_power;i++)
{
printf("\n Enter coefficient of x^%d: ",i);
scanf("%d",&coef[i]);
}
printf("\n");
printf("\n THE POLYNOMIAL IS : ");
for(i=user_power;i>=0;i--)//printing coeff.
{
printf("+(%d)x^%d",coef[i],i);
}
printf("=0");
printf("\n\n ENTER ONE INTIAL ROOT: ");
scanf("%f",&x1);
printf("\n =======================================");
printf("\n ITERATION x1 f(x1) f'(x1) ");
printf("\n =======================================");
do
{
cnt++;
fx1=fdx1=0;
for(i=user_power;i>=1;i--)
{
fx1+=coef[i] * (pow(x1,i)) ;
}
fx1+=coef[0];
for(i=user_power;i>=0;i--)
{
9
fdx1+=coef[i]* (i*pow(x1,(i-1)));
}
t=x2;
x2=(x1-(fx1/fdx1));
x1=x2;
printf("\n %d %.3f %.3f %.3f ",cnt,x2,fx1,fdx1);
}
while((fabs(t - x1))>=0.0001);
printf("\n =======================================");
printf("\n\n RESULT: THE ROOT OF EQUATION IS %f",x2);
getch();
}
OUTPUT:
10
Exercise 7:
Write a program in C that implements TRAPEZOIDAL RULE for approximating the value of a definite
integral. Use it to approximate the value of x3 + 5x – 3 with h = 1.0.
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x=3,y[10],sum=0,h=1;
int i,n,j,l=3,u=8;
clrscr();
printf("\n =====TRAPIZOIDAL RULE=====");
printf("\n This program evaluates integral y=2x^3+5x-3");
n=(u-l)/h;
for(i=0; i<=n; i++)
{
y[i]=(2*(x*x*x))+(5*x)-3;
printf("\n The value of f(x%d) : at x=%d is %f ",i,i+3,y[i]);
x+=h;
}
for(i=1;i<n;i++)
{
sum = sum + 2 * y[i];
}
sum = sum + y[0]+y[n];
sum = sum * (h/2);
printf("\n\n RESULT: I = %f ",sum);
getch();
}
OUTPUT:
11
Exercise 8:
Write a program in C that implements SIMPSONS 1/3 FORMULA to approximate the value of a
definite integral. Further, use the program to approximate the value of dx, using h=0.2
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x=1.3,y[10],sum=0,h=0.2, l=1.3, u=1.7;
int i,n,j,;
clrscr();
printf("\n =====SIMPSON’S 1/3 RULE=====");
printf("\n This program evaluates integral y=e^x");
n=(u-l)/h;
for(i=0; i<=n; i++)
{
y[i]=exp(x);
printf("\n The value of f(x%d) : at x=%f is %f ",i,x,y[i]);
x+=h;
}
for(i=1;i<n;i++)
{
sum = sum + 4 * y[i];
}
sum = sum + y[0]+y[n];
sum = sum * (h/3);
printf("\n\n RESULT: I = %f ",sum);
getch();
}
OUTPUT:
12
Exercise 9:
Write a program in C to implement SECANT METHOD for finding out an approximate root of the
equation x3 + x —6=0.
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define F(x) ((x)*(x)*(x)) + (x) – 6
void main()
{
float x1,x2,x3,f1,f2,t;
clrscr();
printf("\n ==SECANT METHOD== ");
printf("\n This program evaluates y=x^3+x-6");
printf("\n Enter the value of x1: ");
scanf("%f",&x1);
printf("\n Enter the value of x2: ");
scanf("%f",&x2);
printf("\n_____________________________________________________\n");
printf("\n x1\t x2\t x3\t f(x1)\t f(x2)");
printf("\n_____________________________________________________\n");
do
{
f1=F(x1);
f2=F(x2);
x3=x2-((f2*(x2-x1))/(f2-f1));
printf("\n%f %f %f %f %f",x1,x2,x3,f1,f2);
x1=x2;
x2=x3;
if(f2<0)
t=fabs(f2);
else
t=f2;
}
while(t>ESP);
printf("\n_____________________________________________________\n");
printf("\n\nApp.root = %f",x3);
getch();
}
13
OUTPUT:
14
Exercise 10:
Write a C program to demonstrate EULER’S METHOD to find y’=(y-x)/(y+x) and determine the value of
y’ at x=2 for an interval 1.2 to 2.4. Given h=0.2, y at x=1.2 is 2.4
Solution:
/*Euler method */
# include <stdio.h>
# include <conio.h>
int main()
{
clrscr();
int c=0;
float xi,xf,y=2.4,xp,h,dy,i,n,xt,x;
float f(float,float);
printf("\n EULER'S METHOD: \n");
printf(" Enter initial Boundary value of x : ");
scanf("%f",&xi);
printf(" Enter final Boundary value of x : ");
scanf("%f",&xf);
printf(" Enter Value of X at which Y is required : ");
scanf("%f",&xp);
printf(" Enter Interval (h) : ");
scanf("%f",&h);
printf(" No. \t X\t f(x,y) \t Y\n");
printf(" ----------------------------------------------------------\n");
printf("%2d\t %2.3f\t %5.5f\t %5.6f\n",c,xi,f(i,y),y);
for(i=1.4;i<=2.4;i=i+h)
{
c++;
n=y+h*f(i,y);
x=i;
printf("%2d\t %2.3f\t %5.5f\t %5.6f\n",c,i,f(i,y),n);
y=n;
if(x==xp)
{
xt=n;
}
}
printf(" ----------------------------------------------------------\n");
printf(" Value of Y @ X=%f is %f",xp,xt);
getch();
}
float f(float x,float y)
{
return (y-x)/(y+x);
}
15
OUTPUT:
16
Exercise 11:
Write a C program that constructs Lagrangian polynomials, for which at most four nodes are
given (hence interpolating polynomial will be at most cubic). Using the program, find
Lagrangian polynomial that approximates f(x) = x4 and the nodes given are x0 =1, x1 = 2, x2 = 3,
x3 = 4,. Use the polynomial to approximate value at x = 3.5.
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],temp=1,f[10],sum,p;
int i,n,j,k=0,c;
clrscr();
printf("\n -----LAGRANGE'S INTERPOLATION PROGRAM-----");
printf("\n\n\n To find value of f(x) = x^4 ");
printf("\n Enter number of nodes : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\n\n ENTER VALUE OF x%d: ",i);
scanf("%f",&x[i]);
y[i]=x[i]*x[i]*x[i]*x[i];
printf(" Value of f(x%d)= %f ........calculated",i,y[i]);
}
printf("\n\n Enter X for finding f(x): ");
scanf("%f",&p);
OUTPUT:
18
Exercise 12:
Write a program in C that constructs Newton’s interpolating polynomials, for which at most four
nodes are given (hence interpolating polynomial will be at most cubic). Using the program, find
Newton’s interpolation polynomial that approximates f(x) = x4 and the nodes given are x0 =1, x1 = 2,
x2 = 3, x3 = 4,. Use the polynomial to approximate value at x = 3.5.
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
main()
{
clrscr();
float x[20],y[20],f,s,h,d,p;
int j,i,n;
printf("\n -----NEWTON'S INTERPOLATION PROGRAM----- ");
printf("\n\n This program finds value of f(x)=x^4");
printf("\n Enter number of inputs : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n\n Enter the elements of x%d: ",i-1);
scanf("%f",&x[i]);
y[i]=x[i]*x[i]*x[i]*x[i];
printf(" Calculated value of y%d = %f ......",i-1,y[i]);
}
h=x[2]-x[1];
printf("\n\n Enter the value of x to find:");
scanf("%f",&f);
s=(f-x[1])/h;
p=1;
d=y[1];
for(i=1;i<=(n-1);i++)
{
for(j=1;j<=(n-i);j++)
{
y[j]=y[j+1]-y[j];
}
p=p*(s-i+1)/i;
d=d+p*y[1];
}
printf(" For the value of x=%6.5f ,the value of f(%6.5f) is %6.5f",f,f,d);
getch();
}
19
OUTPUT:
20
Exercise 13:
Write a C program to find the approximate root of the equation 4x3 + 2x -6 using REGULA FALSI
method
Solution:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define e 0.0001
int user_power,i=0,cnt=0,flag=0;
int coef[10]={0};
float x1,x2,x3=0,t=0;
float fx1=0,fx2=0,fx3=0,temp=0;
int check()
{
printf("\n\t ENTER INTIAL VALUE : X1---->");
scanf("%f",&x1);
printf("\n\t ENTER INTIAL VALUE : X2---->");
scanf("%f",&x2);
fx1=fx2=fx3=0.0;
for(i=user_power;i>=1;i--)
{
fx1+=coef[i] * (pow(x1,i)) ;
fx2+=coef[i] * (pow(x2,i)) ;
}
fx1+=coef[0];
fx2+=coef[0];
if( (fx1*fx2)>0)
{
printf("\n\t INTIAL VALUES ARE NOT PERFECT.");
return(1);
}
return(0);
}
void main()
{
clrscr();
printf("\n\t\t\t PROGRAM FOR REGULAR-FALSI GENERAL");
printf("\n\t ENTER HIGHEST POWER VALUE : ");
scanf("%d",&user_power);
for(i=0;i<=user_power;i++)
{
printf("\n\t ENTER COEFFICIENT OF x^%d::",i);
scanf("%d",&coef[i]);
}
21
printf("\n");
printf("\n\t THE POLYNOMIAL IS : ");
for(i=user_power;i>=0;i--)//printing coeff.
{
printf(" %dx^%d",coef[i],i);
}
while(1)
{
if(check()==0)
{
flag=1;
break;
}
check();
}
printf("\n ******************************************************");
printf("\n ITERATION X1 FX1 X2 FX2 X3 FX3 ");
printf("\n **********************************************************");
if(flag==1)
{
do
{
cnt++;
fx1=fx2=fx3=0;
for(i=user_power;i>=1;i--)
{
fx1+=coef[i] * (pow(x1,i)) ;
fx2+=coef[i] * (pow(x2,i)) ;
}
fx1+=coef[0];
fx2+=coef[0];
temp=x3;
x3=((x1*fx2)-(x2*fx1))/(fx2-fx1);
for(i=user_power;i>=1;i--)
{
fx3+=coef[i]*(pow(x3,i));
}
fx3+=coef[0];
printf("\n %d %.4f %.4f %.4f %.4f %.4f %.4f",cnt,x1,fx1,x2,fx2,x3,fx3);
t=fx1*fx3;
if(t>0)
{
x1=x3;
}
if(t<0)
{
x2=x3;
}
fx3=0;
22
}
while((fabs(temp-x3))>=e);
printf("\n\t ROOT OF EQUATION IS : %f",x3);
}
getch();
}
OUTPUT:
23
Exercise 14:
Write a C program to find unknown f(x) for the value of X at1949 using NEWTON’S FORWARD
DIFFERENCE FORMULA for the following data:
X: 1931 1941 1951 1961 1971
Y: 40.62 60.80 79.95 103.56 132.65
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
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("\n TO FIND VALUE OF f(x)");
printf("\n USING NEWTON'S FORWARD DIFFERENCE FORMULA");
printf("\n Enter the number of records to input : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\n Enter the value of x%d: ",i);
scanf("%f",&x[i]);
printf(" Enter the value of f(x%d): ",i);
scanf("%f",&y[k][i]);
}
printf("\n Enter X for finding f(x): ");
scanf("%f",&p);
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
y[i][j]=y[i-1][j+1]-y[i-1][j];
}
}
printf("\n __________________________________________________________\n");
printf("\n x(i) y(i) y1(i) y2(i) y3(i) y4(i)");
printf("\n __________________________________________________________\n");
for(i=0;i<n;i++)
{
printf("\n %.3f",x[i]);
for(j=0;j<n-i;j++)
{
printf(" ");
printf(" %.3f",y[j][i]);
}
24
printf("\n");
}
i=0;
do
{
if(x[i]<p && p<x[i+1])
k=1;
else
i++;
}
while(k != 1);
f=i;
u=(p-x[f])/(x[f+1]-x[f]);
printf("\n\n u = %.3f ",u);
n=n-i+1;
sum=0;
for(i=0;i<n-1;i++)
{
temp=1;
for(j=0;j<i;j++)
{
temp = temp * (u - j);
}
m=fact(i);
sum = sum + temp*(y[i][f]/m);
}
printf("\n\n f(%.2f) = %f ",p,sum);
getch();
}
float fact(int a)
{
float fac = 1;
if (a == 0)
return (1);
else
fac = a * fact(a-1);
return(fac);
}
25
OUTPUT:
26
Exercise 15:
Write a C program to find unknown f(x) for the value of X at 25 using NEWTON’S BACKWARD
DIFFERENCE FORMULA for the following data:
X: 15 18 21 24 27
Y: 3.873 4.123 4.359 4.583 4.796
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
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("\n TO FIND THE VALUE OF f(x)");
printf("\n USING NEWTON'S BACKWARD DIFFERENCE FORMULA");
printf("\n Enter number of records to input: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\n Enter the value of x%d: ",i);
scanf("%f",&x[i]);
printf(" Enter the value of f(x%d): ",i);
scanf("%f",&y[k][i]);
}
printf("\n Enter X for finding f(x): ");
scanf("%f",&p);
for(i=1;i<n;i++)
{
for(j=i;j<n;j++)
{
y[i][j]=y[i-1][j]-y[i-1][j-1];
}
}
printf("\n ____________________________________________________________\n");
printf("\n x(i) y(i) y1(i) y2(i) y3(i) y4(i) ");
printf("\n ____________________________________________________________\n");
for(i=0;i<n;i++)
{
printf("\n %.3f",x[i]);
for(j=0;j<=i;j++)
{
printf(" ");
printf(" %.3f",y[j][i]);
}
27
printf("\n");
}
i=0;
do
{
if(x[i]<p && p<x[i+1])
k=1;
else
i++;
}
while(k != 1);
f=i+1;
u=(p-x[f])/(x[f]-x[f-1]);
printf("\n u = %.3f ",u);
n=n-i+1;
sum=0;
for(i=0;i<n;i++)
{
temp=1;
for(j=0;j<i;j++)
{
temp = temp * (u + j);
}
m=fact(i);
sum = sum + temp*(y[i][f]/m);
}
printf("\n f(%.2f) = %f ",p,sum);
getch();
}
float fact(int a)
{
float fac = 1;
if (a == 0)
return (1);
else
fac = a * fact(a-1);
return(fac);
}
28
OUTPUT:
29
Exercise 16:
Using MS Excel demonstrate GAUSSIAN ELIMINATION METHOD to solve system of linear equations:
Given equations:
x + 2y + z = 3
2x + 3y + 3z = 10
3x – y + 2z =13
30
Exercise 17:
Using MS Excel demonstrate BISECTION METHOD to find the root of an equation:
f(x) = x3-x-1
31
Exercise 18:
Using MS Excel demonstrate NEWTON-RAPHSON METHOD to find the root of an equation:
f(x) = 2x3-3x-6
32
Exercise 19:
Using MS Excel demonstrate REGULA-FALSI METHOD to find the root of an equation:
f(x) = x3-4x+1
33
Exercise 20:
Using MS Excel demonstrate LAGRANGE’S INTERPOLATING POLYNOMIAL equation and unknown
value of x. Given f(x) = x4 and the nodes given are x0 =1, x1 = 2, x2 = 3, x3 = 4,. Use the polynomial to
approximate value at x = 3.5.
34
Exercise 21:
Using MS Excel demonstrate NEWTON’S INTERPOLATING POLYNOMIAL equation and unknown value
of x. Given f(x) = x4 and the nodes given are x0 =1, x1 = 2, x2 = 3, x3 = 4,. Use the polynomial to
approximate value at x = 3.5.
35