Saturday, June 11, 2011

Solving Numerical Methods using C language

Solution to Euler's Method, Lagrange's Method ,Bisection Method RK4 Method and many more



1.Euler's Method

 ///solution to diferential equation dy/dx=(1-y)

#include<stdio.h>
float slope(float x,float y)
{
    return(1-y);
}
int main()
{
    printf("\nEnter intitial values of x and y :");
    float xx[10],yy[10];
    scanf("%f%f",&xx[0],&yy[0]);
    printf("\nEnter Interval value:");
    float h;
    scanf("%f",&h);
    printf("\nEnter limits:");
    float lim1,lim2;
    scanf("%f%f",&lim1,&lim2);
    int i;
    for(i=0;lim1+i*h<lim2;i++)
    {
        xx[i]=xx[0]+i*h;
        yy[i+1]=yy[i]+h*slope(xx[i],yy[i]);

    }

    printf("\n\n\nx : ");
    for(i=0;lim1+i*h<lim2;i++)
    {
        printf("%f  ",xx[i]);
    }
    printf("\n\n\ny : ");
    for(i=0;lim1+i*h<lim2;i++)
    {
        printf("%f  ",yy[i]);
    }
    return 0;
}



2.RK4 Method


/* solution for differential equation
  Runge kutta 4th order method
  dy/dx=f(x,y) ,y(x[0])=y[0]
   y[i+1]=y[0]+i*k
   k1=h*f(x[0]*y[0])
   k2=h*f(x[0]+h/2,y[0]+h/2)
   k3=h*f(x[0]+h/2,y[0]+k2/2)
   k4=h*f(x[0]+h,y[0]+k3
   k=(k1+2*k2+2*k3=k4)/6

 */

 ///solve for dy/dx=x*y+y*y



#include<stdio.h>
float f(float xx,float yy)
{
    return (xx*yy+yy*yy);
}

int main()
{
    printf("Enter initial values for  X and Y: ");
    float x[6],y[6],h,lim1,lim2;
    scanf("%f",&x[0]);
    scanf("%f",&y[0]);
    printf("\nEnter interval :");
    scanf("%f",&h);
    printf("\nEnter limits:");
    scanf("%f%f",&lim1,&lim2);
    int i;
    float k1[6],k2[6],k3[6],k4[6],k[6];
    for(i=1;lim1+i*h<lim2;i++)
    {
    k1[i-1]=h*f(x[i-1],y[i-1]);
    k2[i-1]=h*f(x[i-1]+h/2,y[i-1]+h/2);
    k3[i-1]=h*f(x[i-1]+h/2,y[i-1]+k2[i-1]/2);
    k4[i-1]=h*f(x[i-1]+h,y[i-1]+k3[i-1]);
    k[i-1]=(k1[i-1]+2*k2[i-1]+2*k3[i-1]+k4[i-1])/6;
    x[i]=x[0]+i*h;
    y[i]=y[i-1]+k[i-1];
    }
    printf("\n\nx:  ");
    for(i=1;lim1+i*h<lim2;i++)
    {
        printf("\t%f",x[i-1]);
    }
    printf("\n\ny:  ");
    for(i=1;lim1+i*h<lim2;i++)
    {
        printf("\t%f",y[i-1]);
    }
    return 0;
}


3.Bisection Method


/* Find The real roots of x(pow,5)-3x(pow,3)+7
   correct upto 3 decimal digits using the bisection method
*/

#include<stdio.h>
#include<math.h>
float func(float a)
{
    return(pow(a,5)-3*pow(a,3)+7);
}
int main()
{
    float a,b;

    while(1)
    {
        printf("\nEnter valid value of a and b ");
        scanf("%f%f",&a,&b);
        if(func(a)==0)
          {
              printf("\nThe solution for x is: %f",a);
              break;
          }
          if(func(b)==0)
          {
              printf("\nThe solution for x is: %f",a);
              break;
          }
         if(func(a)*func(b)>0)
          {
              printf("\nInvalid Input\n");
          }
          else
             break;
    }
    float c;
    while(fabs(func(c))>0.0005)
    {
        c=(a+b)/2;
        if(func(c)<0)
           {
               b=c;
           }
        else
          {
              a=c;
          }

    }
    printf("\nyour solution is: %f",c);
   return 0;
}


4.Lagrange's Method

#include<stdio.h>
#include<stdlib.h>
int main()
{
    float x[15],y[15],xx,yy,a,sol;
    char res;
    int i,j,k;
    i=0,j=0,k=0,sol=0;
    printf("\nEnter values of x for which you want to find y: ");
    scanf("%f",&xx);
    for(i=0;res!='n';i++)
    {
        system("cls");
        printf("\nEnter values of x%d, y%d: ",i,i);
        scanf("%f%f",&x[i],&y[i]);
        printf("\nDo you want to enter more (y or n) ??");
        scanf("%s",&res);
    }
    for(j=0;j<i;j++)
    {
        yy=1,a=1;
        for(k=0;k<i;k++)
        {
            if(j==k)
              continue;
            else
              a*=1/(x[j]-x[k]);
              yy*=(xx-x[k]);
        }
        sol+=y[j]*a*yy;
    }
    printf("\nSolution for given value of x is:  %f",sol);
    return 0;
}

No comments:

Post a Comment