Showing posts with label Numerical Method. Show all posts
Showing posts with label Numerical Method. Show all posts

Saturday, 4 November 2017

C Programme for Euler Modified Methd

#include<stdio.h>
#include<math.h>
#include<string.h>
float fun(float,float);
main()
    {
        int i,j,c;
        float x[100],y[100],h,m[100],m1,m2,a,s[100],w;
        printf("\n     C program for Modified Euler Method \n\n");
        printf("  Enter the initial value of x:");
        scanf("%f",&x[0]);
        printf("\n  Enter the value of increment h:");
        scanf("%f",&h);
        printf("\n  Enter the final value of x:");
        scanf("%f",&a);
        printf("\n  Enter the initial value of the variable y :");
        scanf("%f",&y[0]);
        s[0]=y[0];
        for(i=1;x[i-1]<a;i++)
            {
                w=100.0;
               x[i]= x[i-1]+h;
               m[i]=fun(x[i-1],y[i-1]);
               c=0;
               while(w>0.0001)
                {
                    m1=fun(x[i],s[c]);
                    m2=(m[i]+m1)/2;
                    s[c+1]=y[i-1]+m2*h;
                    w=s[c]-s[c+1];
                    w=fabs(w);
                    c=c+1;
                }
              y[i]=s[c];
            }
        printf("\n\n The respective values of x and y are\n     x  \t     y\n\n");
        for(j=0;j<i;j++)
            {
                printf("  %f\t%f",x[j],y[j]);
                printf("\n");
            }
    }
float fun(float a,float b)
    {
        float c;
        c=a*a+b;
        return(c);
    }

Input/Output:


C Programme for Euler Method

#include<stdio.h>
float fun(float x,float y)
{
    float f;
    f=x+y;
    return f;
}
main()
{
    float a,b,x,y,h,t,k;
    printf("\nEnter x0,y0,h,xn: ");
    scanf("%f%f%f%f",&a,&b,&h,&t);
    x=a;
    y=b;
    printf("\n  x\t  y\n");
    while(x<=t)
    {
        k=h*fun(x,y);
        y=y+k;
        x=x+h;
        printf("%0.3f\t%0.3f\n",x,y);
    }
}


Input/Output:


Thursday, 2 November 2017

C Programme for Simpson 1/3 Rule

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{ return (1/(1+pow(x,2)));
}
void main()
{int i,n;
 float a,b,h,I,sum=0.0;
 clrscr();
 printf("enter the values of lower and upper limits 'a' and 'b'");
 scanf("%f%f",&a,&b);
 printf("enter the no. of subinetrvals n");
 scanf("%d",&n);
 h=(b-a)/n;
 sum=f(a)+f(b);
 for(i=1;i<n;i++)
 {if(i%2==0)
 sum=sum+2*f(a+i*h);
 else
 sum=sum+4*f(a+i*h);
 }
 I=(h/3)*sum;
 printf("the value is %f:",I);
 getch();
}

Input/Output:


C Programme for Trapezoidal Rule

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{ return (1/(1+pow(x,2)));
}
void main()
{int i,n;
 float a,b,h,I,sum=0.0;
 clrscr();
 printf("enter the values of lower and upper limits 'a' and 'b'");
 scanf("%f%f",&a,&b);
 printf("enter the no. of subinetrvals n");
 scanf("%d",&n);
 h=(b-a)/n;
 sum=f(a)+f(b);
 for(i=1;i<n;i++)
 {sum=sum+2*f(a+i*h);
 }
 I=(h/2)*sum;
 printf("the value is %f:",I);
 getch();
}

Input/Output:


C Programme for Newton Forward Interpolation

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,n;
float x[20],y[20],f,s,h,d,p;
clrscr();
printf("enter the value of n:");
scanf("%d",&n);
printf("enter the element of x:");
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
}
printf("enter the element of y:");
for(i=1;i<=n;i++)
{
scanf("%f",&y[i]);
}
h=x[2]-x[1];
printf("enter the value of x to find value of y:");
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-1);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=%fthe value is:%f",f,d);
getch();
}

Input/Output:


C Programme for Lagrange's Interpolation Formula

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],temp=1,f[10],sum,p;
int i,j,k=0,c,n;
clrscr();
printf("how many record will you enter");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("enter the value of x:%d",i);
scanf("%f",&x[i]);
printf("enter the value of f(x%d):",i);
scanf("%f",&y[i]);
}
printf("enter x for finding f(x):");
scanf("%f",&p);
for(i=0;i<n;++i)
{
temp=1;
k=i;
for(j=0;j<n;++j)
{
if(k==j)
continue;
else
temp=temp*((p-x[j])/(x[k]-x[j]));
}
f[i]=y[i]*temp;
}
for(i=0;i<n;i++)
{
sum=sum+f[i];
}
printf("\n\nf(%f)=%f",p,sum);
getch();
}

Input/Output:


C Programme for Gauss Seidal Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{

float x[15],a[15][15],t,sum;
int i,j,n,itr,maxitr;
clrscr();
printf("\n enter the size of matrix:");
scanf("%d",&n);
printf("\nenter matrix element\n:");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
scanf("%f",&a[i][j]);
}
printf("Enter maximum iteration:");
scanf("%d",&maxitr);
for(i=1;i<=n;i++)
{
x[i]=0;
}
for(itr=1;itr<=maxitr;itr++)
{
for(i=0;i<=n;i++)
{
sum=0.0;
for(j=1;j<=n;j++)
{
if(j!=i)
sum=sum+a[i][j]*x[j];
}
x[i]=(a[i][n+1]-sum)/a[i][i];
}
printf("Roots after iteration%d\n ",itr);
for(i=1;i<=n;i++)
printf("x%d=%f\n",i,x[i]);
}
getch();
}

Input/Output:




C Programme for Gauss Elimination Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,k,n;
float a[10][10],x[10],sum,t;
clrscr();
printf("enter the value of n:");
scanf("%d",&n);
printf("enter the value of matrix in row:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
scanf("%f",&a[i][j]);
}
}
printf("matrix is ....\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("%f\t",a[i][j]);
}
printf("\n");
}
for(j=1;j<=n-1;j++)
{
for(i=j+1;i<=n;i++)
{
t=a[i][j]/a[j][j];
for(k=1;k<=n+1;k++)
{
a[i][k]=a[i][k]-t*a[j][k];
}}}
printf("upper triangular matrix is ....\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("%f\t",a[i][j]);
}
printf("\n");
}
printf("solution of x by using back word substitution....\n");
for(i=n;i>=1;i--)
{
sum=0;
x[i]=0;
for(j=1;j<=n;j++)
{
sum=sum+a[i][j]*x[j];
}
x[i]=(a[i][n+1]-sum)/a[i][i];
}
for(i=1;i<=n;i++)
{
printf("x%d=%f",i,x[i]);
printf("\n");
}
getch();
}

Input/Output:

Enter the value of n:3


C Programme for Newton Raphson Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(x*x*x-5*x+1);
}
float df(float x)
{
return(3*x*x-5);
}
void main()
{
int maxitr,itr;
float x0,x1,h,err;
clrscr();
printf("\nEnter values of x0,err,and maxitr:");
scanf("%f%f%d",&x0,&err,&maxitr);
itr=1;
begin:
h=f(x0)/df(x0);
x1=x0-h;
printf("\n the root of equation %d=\t%f\n",itr,x1);
if(fabs(h)<err)
{
printf("\n the root of equation after %d=\t%f\n",itr,x1);
}
else
{
x0=x1;
itr++;
if(itr<maxitr)
goto begin;
else
printf("\n the number of iteration are not sufficient");
}
getch();
}


Input/Output:





C Programme for Regular-Falsi Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
float F(float x)
{
return(x*x*x-(5*x)+1);
}
void main()
{
int i=0;
float a,b,c,err,temp;
clrscr();
printf("enter permissive error\n");
scanf("%f",&err);
do
{
printf("enter value of a & b\n");
scanf("%f%f",&a,&b);
}
while(F(a)*F(b)>0);
c=0;
do
{
temp=c;
c=(a*F(b)-b*F(a))/(F(b)-F(a));
if(F(a)*F(b)<0)
{
b=c;
}
else
{
a=c;
}
i++;
printf("iteration %d:",i);
printf("%f\n",c);
}
while(fabs(temp-c)>err);
printf("root of equation is %f",c);
getch();
}

Input/Output:



C Programme for Bisection Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
float F(float x)
{
return(x*x*x-(5*x)+1);
}
void main()
{
int i=0;
float a,b,c,err,temp;
clrscr();
printf("enter permissive error\n");
scanf("%f",&err);
do
{
printf("enter value of a & b\n");
scanf("%f%f",&a,&b);
}
while(F(a)*F(b)>0);
c=0;
do
{
temp=c;
c=(a+b)/2;
if(F(a)*F(b)<0)
{
b=c;
}
else
{
a=c;
}
i++;
printf("iteration %d:",i);
printf("%f\n",c);
}
while(fabs(temp-c)>err);
printf("root of equation is %f",c);
getch();
}



Input/Output: