Monthly Archives: March 2015

Pract: Develop a C program to implement Simpsons 1/3rd Rule.

#include<stdio.h>
void main()
{
int i,n;
float x[20], y[20], h, sum, integral, s2, s4;
clrscr();
printf(“Enter number of intervals: “);
scanf(“%d”,&n);
printf(“Enter size(height) of interval: “);
scanf(“%f”,&h);
printf(“Enter %d pair of (x,y)\n”,n+1);

for(i=1; i<=(n+1); i++) {
printf(“x[%d]= “,i);
scanf(“%f”,&x[i]);

printf(“y[%d]= “,i);
scanf(“%f”,&y[i]);
}
sum = y[1]+y[n+1];
s2 = s4 = 0.0;
for(i=2; i<=n; i+=2) {
s4 = s4+y[i];
}
for(i=3; i<=(n-1); i+=2) {
s2 = s2+y[i];
}
integral = (h/3.0)*(sum+2*s2+4*s4);
printf(“\nValue of Integral = %.4f\n”,integral);
getch();
}

 

Pract: Develop a C program to compute derivatives of a tabulated function at a specified value using the Newton interpolation approach. (Newton’s Divided Interpolation Formula)

#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(“Enter total number of values: “);
scanf(“%d”,&n);
for(i=0; i<n; i++)
{
printf(“enter the value of x[%d]: “,i+1);
scanf(“%f”,&x[i]);
printf(“enter the value of y[%d]: “,i+1);
scanf(“%f”,&y[k][i]);
}
printf(“\nEnter X for finding y(x): “);
scanf(“%f”,&p);

for(i=1;i<n;i++)
{
k=i;
for(j=0;j<n-i;j++)
{
y[i][j]=(y[i-1][j+1]-y[i-1][j])/(x[k]-x[j]);
k++;
}
}
printf(“\n_____________________________________________________”);
printf(“\n  x(i)\t   y(i)\t    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]);
}
printf(“\n”);
}

i=0;
do
{
if(x[i]<p && p<x[i+1])
k=1;
else
i++;
}while(k != 1);
f=i;

sum=0;
for(i=0;i<n-1;i++)
{
k=f;
temp=1;
for(j=0;j<i;j++)
{
temp = temp * (p – x[k]);
k++;
}
sum = sum + temp*(y[i][f]);
}
printf(“\n f(%.2f) = %f”,p,sum);
getch();
}

 

Pract: C program to compute the interpolation value using Newton’s Backward Difference formula.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int n,i,j,k,f,fact=1;
float xn,x[5],y[5],h,sum=0,prod=1,u,d[5][5];
clrscr();

printf(“\n\t******************************************************************”);
printf(“\n\t*            Newton’s Backward Interpolation               *”);
printf(“\n\t******************************************************************\n\n”);

printf(“Enter n: “);
scanf(“%d”,&n);

printf(“Enter x:”);
scanf(“%f”,&xn);

for(i=1;i<=n;i++)
{
printf(“x[%d]= “,i);
scanf(“%f”,&x[i]);

printf(“y[%d]= “,i);
scanf(“%f”,&y[i]);
}
if((xn<x[1])||(xn>x[n]))
{
printf(“Value out of range”);
getch();
exit(0);
}
i=2;
while(xn>x[i])
i++;
k=i;
u = (xn – x[k])/(x[k]-x[k-1]);
printf(“\n”);
for(j=1;j<=n-1;j++)
{
for(i=j+1;i<=n;i++)
{
if(j==1)
d[i][j]=y[i]-y[i-1];
else
d[i][j]=d[i][j-1]-d[i-1][j-1];
}
}

sum=y[k];

for(i=1;i<=k-1;i++)
{
prod=1;
for(j=0;j<=i-1;j++)
{
prod=prod*(u+j);
}
fact=1;
for(f=1;f<=i;f++)
{
fact=fact*f;
}
sum=sum+(d[k][i]*prod)/fact;
}
printf(“\nInterpolated value : %f”,sum);
getch();
}

 

Pract: C program to compute the interpolation value using Newton’s Forward Difference formula.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int n,i,j,k,f,fact=1;
float xn,x[5],y[5],sum=0,prod=1,u,d[5][5];
clrscr();

printf(“\n\t******************************************************************”);
printf(“\n\t*            Newton’s Forward Interpolation               *”);
printf(“\n\t******************************************************************\n\n”);

printf(“Enter n: “);
scanf(“%d”,&n);

printf(“Enter x:”);
scanf(“%f”,&xn);

for(i=1;i<=n;i++)
{
printf(“x[%d]= “,i);
scanf(“%f”,&x[i]);

printf(“y[%d]= “,i);
scanf(“%f”,&y[i]);
}
if((xn<x[1])||(xn>x[n]))
{
printf(“Value out of range”);
getch();
exit(0);
}
i=2;
while(xn>x[i])
i++;
k=i-1;
u=(xn-x[k])/(x[k+1]-x[k]);
printf(“\n”);
for(j=1;j<=n-1;j++)
{
for(i=1;i<=n-j;i++)
{
if(j==1)
d[i][j]=y[i+1]-y[i];
else
d[i][j]=d[i+1][j-1]-d[i][j-1];
}
}

sum=y[k];

for(i=1;i<=n-k;i++)
{
prod=1;
for(j=0;j<=i-1;j++)
{
prod=prod*(u-j);
}
fact=1;
for(f=1;f<=i;f++)
{
fact=fact*f;
}
sum=sum+(d[k][i]*prod)/fact;
}
printf(“\nInterpolated value : %f”,sum);
getch();
}

 

Pract: C Program for finding root of non-linear equation using Newton-Raphson Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<dos.h>
#define f(x) (x*x*x-4*x-9)
#define ff(x) (3*x*x-4)
void main()
{
float x0,x1,f1,f2,epsilon,delta,re;
int n,i;
clrscr();
printf(“\n\t\t Finding Root using Newton Rapson Method “);
printf(“\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n”);
printf(“\nf(x)=(x*x*x-4*x-9) = 0\n”);
printf(“Enter x0 : “);
scanf(“%f”,&x0);
printf(“Enter e : “);
scanf(“%f”,&epsilon);
printf(“Enter d : “);
scanf(“%f”,&delta);
printf(“Enter n : “);
scanf(“%d”,&n);
printf(“\n==========================================================”);
printf(“\niter’s        x0            f(x0)             ff(x0)         x1          re \n”);
printf(“===========================================================\n”);
for(i=1;i<=n;i++)
{
if(fabs(f2)<delta)
{
printf(“\n\nSLOPE OF THE FUNCTION BECOMES TOO SMALL………”);
exit(1);
}
f1 = f(x0);
f2 = ff(x0);
x1 = x0 – (f1 / f2);
re = fabs((x1-x0)/x1);
printf(“\n %d        %.4f          %.4f          %.4f         %.4f        %.4f\n”,i,x0,f1,f2,x1,re);
printf(“……………………………………………………………..”);

x0 = x1;
if(re<=epsilon)
{
printf(“\nRoot := %.4f”,x1);
}
}
printf(“\nSOLUTIONS DOES NOT CONVERGE IN %d ITERATIONS “,n);
getch();
}