LAB ASSIGNMENT
NAME : SOMBIT CHOWDHURY
ROLL NO : 136
SEC: IT- B(Y)
SUB : NUMERICAL METHODS AND PROGRAMMING LAB
PAPER CODE : M(IT)392
NEWTON FORWARD AND BACKWARD METHOD:
// C program to demonstrate
// both Forward and Backward
// Newton's Interpolation
#include <stdio.h>
void forward(float x[4], float y[4][4], int n);
void BackWard(float x[4], float y[4][4], int n);
int main()
int i, j;
int n = 4; // number of arguments
float x[4] = { 0, 1, 2, 3 };
float y[4][4] = {
{ 1, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 1, 0, 0, 0 },
{ 10, 0, 0, 0 },
};
forward(x, y, n);
BackWard(x, y, n);
return 0;
void forward(float x[4], float y[4][4], int n)
int i, j;
float a = 0.5; // interpolation point
float h, u, sum, p;
for (j = 1; j < n; j++) {
for (i = 0; i < n - j; i++) {
y[i][j] = y[i + 1][j - 1] - y[i][j - 1];
printf("\n The forward difference table is:\n");
for (i = 0; i < n; i++) {
printf("\n");
for (j = 0; j < n - i; j++) {
printf("%f\t", y[i][j]);
p = 1.0;
sum = y[0][0];
h = x[1] - x[0];
u = (a - x[0]) / h;
for (j = 1; j < n; j++) {
p = p * (u - j + 1) / j;
sum = sum + p * y[0][j];
}
printf("\nThe value of y at x=%0.1f is %0.3f", a, sum);
void BackWard(float x[4], float y[4][4], int n)
int i, j;
float a = 0.5; // interpolation point
float h, u, sum, p;
for (j = 1; j < n; j++) {
for (i = j; i < n; i++) {
y[i][j] = y[i][j - 1] - y[i - 1][j - 1];
printf("\nThe backward difference table is:\n");
for (i = 0; i < n; i++) {
printf("\n");
for (j = 0; j <= i; j++) {
printf("%f\t", y[i][j]);
p = 1.0;
sum = y[n - 1][0];
h = x[1] - x[0];
u = (a - x[n - 1]) / h;
for (j = 1; j < n; j++) {
p = p * (u + j - 1) / j;
sum = sum + p * y[n - 1][j];
printf("\nThe value of y at x=%0.1f is %0.3f", a, sum);
}
LAGRANGE’S INTERPOLATION:
#include<stdio.h>
#include<conio.h>
void main()
float x[100], y[100], xp, yp=0, p;
int i,j,n;
clrscr();
/* Input Section */
printf("Enter number of data: ");
scanf("%d", &n);
printf("Enter data:\n");
for(i=1;i<=n;i++)
printf("x[%d] = ", i);
scanf("%f", &x[i]);
printf("y[%d] = ", i);
scanf("%f", &y[i]);
printf("Enter interpolation point: ");
scanf("%f", &xp);
/* Implementing Lagrange Interpolation */
for(i=1;i<=n;i++)
p=1;
for(j=1;j<=n;j++)
if(i!=j)
p = p* (xp - x[j])/(x[i] - x[j]);
yp = yp + p * y[i];
printf("Interpolated value at %.3f is %.3f.", xp, yp);
getch();
OUTPUT:
TRAPEZOIDAL RULE :
#include<stdio.h>
#include<math.h>
/* Define the function to be integrated here: */
double f(double x){
return x*x;
/*Program begins*/
main(){
int n,i;
double a,b,h,x,sum=0,integral;
/*Ask the user for necessary input */
printf("\nEnter the no. of sub-intervals: ");
scanf("%d",&n);
printf("\nEnter the initial limit: ");
scanf("%lf",&a);
printf("\nEnter the final limit: ");
scanf("%lf",&b);
/*Begin Trapezoidal Method: */
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
sum=sum+f(x);
integral=(h/2)*(f(a)+f(b)+2*sum);
/*Print the answer */
printf("\nThe integral is: %lf\n",integral);
OUTPUT:
SIMPSONS 1/3 RULE:
#include<stdio.h>
#include<conio.h>
float f(float x)
{
return(1/(1+x));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\n Refined value of n and h are:%d %f\n",n,h);
printf("\n Y values: \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n %f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}
}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\n Final integration is %f",ans);
getch();
}
OUTPUT:
BISECTION METHOD:
#include <stdio.h>
// funciton definition
void bisect (float *mid_pt, float int_st, float int_end, int *iter_cnt);
double get_fun (double res);
int main ()
// declaration of the variables
int iter_cnt, mx_iter_cnt;
float mid_pt, int_st, int_end, err_all, root;
printf (" \n Enter the first starting point: ");
scanf (" %f", &int_st);
printf (" \n Enter the second ending point: ");
scanf (" %f", &int_end);
// declare no. of iteration to be allowed
printf (" \n Enter the maximum iteration to be allowed: ");
scanf (" %d", &mx_iter_cnt);
// allow no. of error point
printf (" Input the no. of allowed error point: ");
scanf (" %f", &err_all);
// call bisect() function
bisect (&mid_pt, int_st, int_end, &iter_cnt);
// use for loop to print the max iteration
for (iter_cnt = 0; iter_cnt < mx_iter_cnt; mid_pt = root)
// chcck initial num * mid_pt is less than 0
if ( get_fun (int_st) * get_fun (mid_pt) < 0)
int_end = mid_pt; // assign the mid_pt to int_end
else
int_st = mid_pt; // else it assign the mid_pt to int_st
bisect ( &root, int_st, int_end, &iter_cnt); // get the address
if ( fabs (root - mid_pt) < err_all)
printf (" \n The approximation root is: %f \n", root);
return 0;
// print insufficient
printf (" The iterations are insufficient: ");
return 0;
// function definition
void bisect (float *mid_pt, float int_st, float int_end, int *iter_cnt)
{
*mid_pt = (int_st + int_end) / 2; // get the middle value
++(*iter_cnt); // increment the iteration value
printf ( " Iteration \t %d: \t %f \n", *iter_cnt, *mid_pt);
double get_fun (double res)
return (res * res * res - 4 * res - 9);
OUTPUT:
REGULA FALSI:
#include<stdio.h>
#include<math.h>
float f(float x)
return cos(x) - x*exp(x);
void regula (float *x, float x0, float x1, float fx0, float fx1, int *itr)
*x = x0 - ((x1 - x0) / (fx1 - fx0))*fx0;
++(*itr);
printf("Iteration no. %3d X = %7.5f \n", *itr, *x);
void main ()
int itr = 0, maxmitr;
float x0,x1,x2,x3,allerr;
printf("\nEnter the values of x0, x1, allowed error and maximum iterations:\n");
scanf("%f %f %f %d", &x0, &x1, &allerr, &maxmitr);
regula (&x2, x0, x1, f(x0), f(x1), &itr);
do
if (f(x0)*f(x2) < 0)
x1=x2;
else
x0=x2;
regula (&x3, x0, x1, f(x0), f(x1), &itr);
if (fabs(x3-x2) < allerr)
printf("After %d iterations, root = %6.4f\n", itr, x3);
return 0;
x2=x3;
while (itr<maxmitr);
printf("Solution does not converge or iterations not sufficient:\n");
return 1;
OUTPUT:
NEWTON RAPHSON METHOD:
#include<stdio.h>
#include<math.h>
float f(float x)
{
return x*log10(x) - 1.2;
}
float df (float x)
{
return log10(x) + 0.43429;
}
void main()
{
int itr, maxmitr;
float h, x0, x1, allerr;
printf("\nEnter x0, allowed error and maximum iterations\n");
scanf("%f %f %d", &x0, &allerr, &maxmitr);
for (itr=1; itr<=maxmitr; itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1);
if (fabs(h) < allerr)
{
printf("After %3d iterations, root = %8.6f\n", itr, x1);
return 0;
}
x0=x1;
}
printf(" The required solution does not converge or iterations are insufficient\n");
return 1;
}
OUTPUT:
THANK YOU