Showing posts with label numerical methods. Show all posts
Showing posts with label numerical methods. Show all posts

Saturday, August 25, 2007

Lagrangian Interploation, backward difference and divided difference - C program

Lagrangian Interploation, backward difference and divided difference - C program

Lagrangian Interploation

#include <stdio.h>
#include <conio.h>
//#define MAX 20


void main ()
{
float x[20], y[20],sum,prod,a;
int i,j,n;
clrscr ();
printf("Enter the number of table points(less then 20): ");
scanf("%d",&n);
printf("Enter %d pair of values for (x,y)\n",n);
for (i = 0; i < n; i++)
{
printf("Enter the value for x[%d]",i);
scanf("%f",&x[i]);
printf("Enter the value for y[%d]",i);
scanf("%f",&y[i]);
}
printf("Enter the value of x for interpolation: ");
scanf("%f",&a);
sum = 0;
for (i = 0; i < n; i++)
{
prod = 1;
for (j = 0; j < n; j++)
{
if (i != j)
prod *= (a - x[j]) / (x[i] - x[j]);
}
sum += y[i] * prod;
}
printf("Interpolated value = %f",sum);
getch ();
}

Backward difference


#include <stdio.h>
#include <conio.h>
#define MAX 8
void main ()
{
int i,j,k,n;
float y[MAX],d[MAX-1][MAX-1]={0};
clrscr ();
printf("\nEnter the number of table points: ");
scanf("%d",&n);
printf("\nEnter %d value of y\n",n);
for (i=0;i<n;i++)
{
printf("Enter the value for y[%d]: ",i+1);
scanf("%f",&y[i]);
}
for (j=0;j<n;j++)
{
for (i=j+1;i<n;i++)
{
if (j==0)
d[j][i] = y[i]-y[i-1];
else
d[j][i] = d[j-1][i] - d[j-1][i-1];
printf("y[%d] = %f\n",j+1,d[j][i]);
}
}
clrscr ();
printf("\n\t\t\tBACKWARD DIFFERENCE TABLE\n");
for (i=0;i<80;i++)
printf("-");
printf("\n");
printf(" y\t\t");
for (i=0;i<n-1;i++)
printf(" d^%dy \t",i+1);
printf("\n");
for (i=0;i<80;i++)
printf("-");
printf("\n");
for (j=0;j<n;j++)
{
printf(" %6.4f \t",y[j]);
for (i=0;i<n;i++)
{
if (d[i][j] != 0)
printf(" %5.4f \t",d[i][j]);
}
printf("\n");
}
for (i=0;i<80;i++)
printf("=");
printf("\n");
getch ();
}

Divided Difference

#include <stdio.h>
#include <conio.h>
#define MAX 8
void main ()
{
int i,j,k,n;
float x[MAX],y[MAX],d[MAX-1][MAX-1]={0};
clrscr ();
printf("\nEnter the number of table points: ");
scanf("%d",&n);
printf("\nEnter %d value of y\n",n);
for (i=0;i<n;i++)
{
printf("Enter the value for x[%d]: ",i+1);
scanf("%f",&x[i]);
printf("Enter the value for y[%d]: ",i+1);
scanf("%f",&y[i]);
}
for (j=0;j<n;j++)
{
for (i=j+1;i<n-j-1;i++)
{
if (j==0)
d[j][i] = (y[i+1]-y[i])/(x[i+1]-x[i]);
else
d[j][i] = d[j-1][i+1] - d[j-1][i]/(x[j+i+1]-x[i]);
}
}
clrscr ();
printf("\n\t\t\tDIVIDED DIFFERENCE TABLE\n");
for (i=0;i<80;i++)
printf("-");
printf("\n");
printf(" x\t\t y\t\t");
for (i=0;i<n-1;i++)
printf(" d^%dyi \t",i+1);
printf("\n");
for (i=0;i<80;i++)
printf("-");
printf("\n");
for (j=0;j<n;j++)
{
printf(" %6f \t%6f \t",x[j],y[j]);
for (i=0;i<n;i++)
{
if (d[i][j] != 0)
printf(" %5f \t",d[i][j]);
}
printf("\n");
}
for (i=0;i<80;i++)
printf("=");
printf("NOTE: Consider the default values for the blank spaces 0.0000");
printf("\n");
getch ();
}

Mean, median, mode and standard deviation in C

Mean, median, mode and standard deviation in C
=======================================

Mean
=====


#include <stdio.h>
#include <conio.h>
void main ()
{
int n,i,a[25];
float mean,sum=0.0;
clrscr ();
printf("How many numbers are there for calculating the mean?: ");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("[%d]. ",i+1);
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("\nThe sum is: %f\n",sum);
mean=sum/n;
printf("\nThe mean is: %f",mean);
getch ();
}


Median & Mode
=============


#include <stdio.h>
#include <conio.h>
void main ()
{
float x,m,z;
clrscr ();
printf("Enter the value of Mean: ");
scanf("%f",&x);
printf("Enter the value of median: ");
scanf("%f",&m);
z=3*m-2*x;
printf("The value of Mode is: %f",z);
getch ();
}

Standard deviation
===============


#include <stdio.h>
#include <math.h>
#include <conio.h>
#define MAX 100
void main ()
{
int i,n=0;
float value[MAX],deviation,sum=0.0,sumsqr=0.0,x,v,std;
clrscr ();
printf("\nEnter value of enter -1 to exit:\n");
for (i=0;i<MAX;i++)
{
scanf("%f",&value[i]);
if (value[i] == -1)
break;
sum += value[i];
n++;
}
x = sum/(float)n;
for (i=1;i<=n;i++)
{
deviation = value[i] - x;
sumsqr += deviation*deviation;
}
v = sumsqr/(float)n;
std = sqrt(v);
printf("Number of items: %d\n",n);
printf("Mean: %f\n",x);
printf("Standard Deviation: %f",std);
getch ();
}

Iterative methods in C (Bisection Method, False Position, Newton Raphson, Secant)

Iterative methods in C (Bisection Method, False Position, Newton Raphson, Secant)

Bisection Method
=================

#include <stdio.h>
#include <math.h>
#include <conio.h>
#define EPSILON 0.0001
#define f(x) (x*x*x-4*x-9)
void main ()
{
double x1,x2,x3,x;
clrscr ();
printf("Enter the values between the root of the equation lies:\n");
scanf ("%lf %lf",&x1,&x2);
do
{
x3 = (x1 + x2)/2;
if ((f(x1) * f(x3)) < 0)
x2 = x3;
else
x1 = x3;
} while (labs(x1 - x2) > EPSILON && f(x3) != 0);
printf("The root is %lf",x3);
getch ();
}


False Position
==============

#include <stdio.h>
#include <conio.h>
#include <math.h>
#define EPSILON 0.0001
#define f(x) (x*x*x - 2*x - 5)
void main ()
{
float x1,x2,x3,x,f1,f2,f3;
clrscr ();
printf("Enter the points between which the root lies:\n");
scanf ("%f %f",&x1,&x2);
f1 = f(x1);
f2 = f(x2);
if ((f1 * f2) > 0)
{
printf("Initial approximation are unsuitable");
exit (0);
}
do
{
x3 = (x1*f2 - x2*f1)/(f2 - f1);
f3 = f(x3);
if (f1 * f3 < 0)
{
x2 = x3;
f2 = f3;
}
else
{
x1 = x3;
f1 = f3;
}
} while ((labs(x1 - x2) > EPSILON) && (f3 != 0));
printf("The approximate root is %lf",x3);
getch ();
}

Newton Raphson
==============


#include <stdio.h>
#include <conio.h>
#include <math.h>
#define EPSILON 0.0001
#define f(x) (x*x*x-4*x-9)
#define df(x) (3*x*x-4)
void main ()
{
double x0,x1,x,relative_error;
int n,i;
clrscr ();
printf("Enter the value of x0: ");
scanf("%lf",&x0);
printf("Enter the number of iterations you want to perform: ");
scanf ("%d",&n);
for (i=0;i<n;i++)
{
x1 = x0-f(x0)/df(x0);
relative_error = labs((x1-x0)/x1);
x0 = x1;
if((relative_error <= EPSILON) (f(x1) == 0))
{
printf("%lf as the approximate root",x1);
exit (1);
}
}
printf("Solution does not converge in %d iterations");
getch ();
}

Secant
========


#include <stdio.h>
#include <conio.h>
#include <math.h>
#define EPSILON 0.0001
#define f(x) (cos(x) - x*exp(x))
void main ()
{
double x1,x2,x3,x,epsilon,f1,f2,f3;
int n,i;
clrscr ();
printf ("Enter the value of the two points:\n");
scanf ("%lf %lf",&x1, &x2);
printf("Enter the number of iterations do you want to perform: ");
scanf("%d",&n);
f1 = f(x1);
f2 = f(x2);
for (i=0;i<n;i++)
{
x3 = (x1*f2 - x2*f1)/(f2 - f1);
f3 = f(x3);
if ((labs((x3-x2)/x3)) <= EPSILON (f(x1) == 0))
{
printf("The approximate root is %lf",x3);
exit (1);
}
x1 = x2;
f1 = f2;
x2 = x3;
f2 = f3;
}
printf("Solution does not coverage in %d iterations",n);
}