#include<stdio.h>
#include<math.h>
void main()
{
float x[100],y[100],xy[100],xsquare[100],m,sumx,sumy,sumxy,sumxsq,a,b;
int i,j,k;
sumx=sumy=sumxy=sumxsq=0;
printf("\n\t\t\tLeast Square Method To Fit A Staright Line");
printf("\n=========================================================================================================================\n");
printf("\t\t\tEnter the value of m(number of terms) ");
scanf("%f",&m);
printf("\n=========================================================================================================================\n");
printf("\t\t\tEnter all the value of X \n");
for(i=0;i<m;i++)
{
printf("\tx[%d]=",i);
scanf("%f",&x[i]);
xsquare[i]=x[i]*x[i];//xsquare being calculated here
sumx=sumx+x[i];//summation of x
sumxsq=sumxsq+xsquare[i];//summation of x square calculated
}
printf("\n=========================================================================================================================\n");
printf("\t\t\tEnter all the value of Y \n");
for(i=0;i<m;i++)
{
printf("\ty[%d]=",i);
scanf("%f",&y[i]);
xy[i]=x[i]*y[i];//x*y being calculated
sumy=sumy+y[i];// summation of Y calculated
sumxy=sumxy+xy[i];//summation of XY calculated
}
a=((sumy*sumxsq)-(sumx*sumxy))/((m*sumxsq)-(sumx*sumx));
b=((m*sumxy)-(sumx*sumy))/((m*sumxsq)-(sumx*sumx));
printf("\n=========================================================================================================================\n\n");
printf("\t\t\tValue of a=%4.3f\tvalue of b=%4.3f\n",a,b);
printf("\n\t\t\tThus equation will be y=%4.3f + %4.3fx\n\n",a,b);
getchar();
}
Computer Based Numerical & Statistical Techniques
Tuesday, 15 November 2011
Straight Line Fit Using Least Squares Estimate
Sunday, 23 October 2011
Fourth Order Runge-Kutta Method For Differential Equation
#include<stdio.h>
#include<math.h>
float funct(float,float);
void main()
{
float y0,yprime,k1,k2,k3,k4;
float h,x,iter,x0;
printf("\n\t\t\tFourth Order Runge-Kutta Method To Solve Differential Equation");
printf("\n=========================================================================================================================\n");
printf("\t\t\tEnter the value of h(the interval) ");
scanf("%f",&h);
printf("\n=========================================================================================================================\n");
printf("\t\t\tEnter the value of X(for which you need value of y) ");
scanf("%f",&x);
printf("\n=========================================================================================================================\n");
printf("\t\t\tEnter the intital value y0 ");
scanf("%f",&y0);
printf("\n=========================================================================================================================\n");
printf("\t\t\tEnter the intital value x0 ");
scanf("%f",&x0);
for(iter=x0;iter<=x;iter=iter + h)
{
k1=funct(iter,y0);
k2=funct(iter+(h/2),y0+(k1*h)/2);
k3=funct(iter+((h/2)),y0+(k2*h)/2);
k4=funct(iter+h,y0+(k3*h));
yprime=y0+((h/6)*(k1+2*(k2+k3)+k4));
printf("\n\tValue of X=%f then Value of Y=%f ",iter,y0);
y0=yprime;
}
getchar();
}
float funct(float x,float y)
{
return ((x-y)/(x+y));
}
Euler's Method For Numerical Differentiation
#include<stdio.h>
#include<math.h>
float funct(float,float);
void main()
{
float y0,yprime;
float h,x,iter,x0;
printf("\n\t\t\tEuler's Method To Solve Differential Equation");
printf("\n=================================================================================================================\n");
printf("\t\t\tEnter the value of h(the interval) ");
scanf("%f",&h);
printf("\n=================================================================================================================\n");
printf("\t\t\tEnter the value of X(for which you need value of y) ");
scanf("%f",&x);
printf("\n=================================================================================================================\n");
printf("\t\t\tEnter the intital value y0 ");
scanf("%f",&y0);
printf("\n=================================================================================================================\n");
printf("\t\t\tEnter the intital value x0 ");
scanf("%f",&x0);
for(iter=x0;iter<=x;iter=iter + h)
{
yprime=y0+(h*funct(iter,y0));
printf("\nWhen X=%f then Y=%f",iter,y0);
y0=yprime;
}
getchar();
}
float funct(float x,float y)
{
return ((x-y)/(x+y));
}
Tuesday, 18 October 2011
Simpson's 3/8th Rule
#include<stdio.h>
float integrate(float);
void main()
{
float a,b,h,n,sum=0;
int i;
printf("\n\tINTEGRATION THROUGH SIMPSON' 3/8 RULE");
printf("\n\t====================================");
printf("\n\nEnter the upper limit a ");
scanf("%f",&a);
printf("\n\nEnter the lower limit b ");
scanf("%f",&b);
printf("\n\nEnter the number of sub-intervals ");
scanf("%f",&n);
h=(b-a)/n;
sum=integrate(a)+integrate(b);
for(i=1;i<n;i++)
{
if(i%3==0)
sum=sum+(2*integrate(a+i*h));
else
sum=sum+(3*integrate(a+i*h));
}
sum=sum*((3*h)/8);
printf("\nThe value of integral is %f",sum);
}
float integrate(float n)
{
return 1/(1+(n*n));
}
Simpson's 1/3rd Rule
#include<stdio.h>
float integrate(float);
void main()
{
float a,b,h,n,sum=0;
int i;
printf("\n\tINTEGRATION THROUGH SIMPSON' 1/3 RULE");
printf("\n\t====================================");
printf("\n\nEnter the upper limit a ");
scanf("%f",&a);
printf("\n\nEnter the lower limit b ");
scanf("%f",&b);
printf("\n\nEnter the number of sub-intervals ");
scanf("%f",&n);
h=(b-a)/n;
sum=integrate(a)+integrate(b);
for(i=1;i<n;i++)
{
if(i%2==0)
sum=sum+(2*integrate(a+i*h));
else
sum=sum+(4*integrate(a+i*h));
}
sum=sum*(h/3);
printf("\nThe value of integral is %f",sum);
}
float integrate(float n)
{
return 1/(1+(n*n));
}
Trapezoidal Method Of Numerical Integration
#include<stdio.h>
float integrate(float);
void main()
{
float a,b,h,n,i,sum=0;
printf("\n\tINTEGRATION THROUGH TRAPEZOIDAL RULE");
printf("\n\t====================================");
printf("\n\nEnter the upper limit a ");
scanf("%f",&a);
printf("\n\nEnter the lower limit b ");
scanf("%f",&b);
printf("\n\nEnter the number of sub-intervals ");
scanf("%f",&n);
h=(b-a)/n;
sum=integrate(a)+integrate(b);
for(i=1;i<n;i++)
sum=sum+(2*integrate(a+i));
sum=sum*(h/2);
printf("\nThe value of integral is %f",sum);
}
float integrate(float n)
{
return 1/(1+(n*n));
}
Monday, 17 October 2011
Lagrange's Interpolation Formula
#include<stdio.h>
#include<math.h>
void main()
{
float ax[10],y[10];
float x,numsum=1,densum=1,k;
int i,j,n;
printf("Enter the number of terms ");
scanf("%d",&n);
printf("\n=======================================================\n");
printf("Enter the array ax ");
for(i=0;i<n;i++)
{
printf("\nEnter the value of ax[%d] = ",i);
scanf("%f",&ax[i]);
}
printf("\n=======================================================\n");
for(i=0;i<n;i++)
{
printf("Enter the value of y[%d] = ",i);
scanf("%f",&y[i]);
}
printf("\nEnter the interpolation point ");
scanf("%f",&x);
k=0;
for(i=0;i<n;i++)
{
numsum=1;
densum=1;
for(j=0;j<n;j++)
{
if(i!=j)
{
numsum=numsum*(x-ax[j]);
densum=densum*(ax[i]-ax[j]);
}
}
k=k+(numsum/densum)*y[i];
}
printf("\nValue at %f is %f ",x,k);
getchar();
}
Subscribe to:
Posts (Atom)


