Showing posts with label Newton. Show all posts
Showing posts with label Newton. Show all posts

Wednesday, 5 December 2012

Newton-Raphson Method


       §       

PROGRAMMING IN C: ASSIGNMENT


PRATYUSH DAS
BEE 3RD YEAR
Electrical Engineering Department, Jadavpur University

-------Contents-------

         §         Gauss-elimination method
         §         Gauss-Seidel method
         §         Bisection method
         §         False position method
         §         Secant method
         §         Newton-Raphson method  ...............................................................................
......................................................................... 

/* Newton Raphson Method*/

#include<stdio.h>
#include<math.h>
#define f(x) (x*x*x-4*x-9) // any equation can be taken here
#define f1(x) (3*x*x-4)

void main()
{
      int itr,N;
      float h,x0,x1,err;
         printf("------------NEWTON-RAPHSON METHODOLOGY------------\n\n\n");
      printf("enter x0,allowed error,maximum iterations\n");
      scanf("%f %f %d",&x0,&err,&N);
         printf("\n\n");
      for(itr=1;itr<=N;itr++)
      {
      h=f(x0)/f1(x0);
      x1=x0-h;
      printf("iteration no. %3d  gives ,x= %9.6f\n",itr,x1);
      if(fabs(h)<err)
      {
      printf("after %3d iterations,""root is  \n    x=%8.6f\n",itr,x1);
      getch();
      exit(1);
      }
      x0=x1;
      }
      printf("iterations not sufficient, solution does not converge\n");
      getch();
}

ª       Results: (Newton-Raphson method)
 ------------NEWTON-RAPHSON METHODOLOGY-------
enter x0,allowed error,maximum iterations
2    .005    66
iteration no.   1  gives ,x=  3.125000
iteration no.   2  gives ,x=  2.768530
iteration no.   3  gives ,x=  2.708196
iteration no.   4  gives ,x=  2.706529
after   4 iterations,root is
    x=2.706529                                                                                                  

Saturday, 1 December 2012

False Position Method/Regula Falsi






       §       /* False position method*/


#include<stdio.h>
#include<math.h>

#define f(x) (x*x*x-4*x*x-9*x-12) // any equation can be used

void main()
{
       void false(float*,float,float,float,float,int*);
    int itr=0,N;
       float x,x0,x1,x2,x3,err;
       printf("----------METHOD OF FALSE POSITION FOR SOLVING NONLINEAR EQUATIONS-----------\n\n\n");
       printf("enter starting values,max error,max iteration\n\n");
       scanf("%f%f%f%d",&x0,&x1,&err,&N);
       false(&x2,x1,x0,f(x0),f(x1),&itr);
       do
       {
         if(f(x0)*f(x2)<0)
      x1=x2;
      else
      x0=x2;
         false(&x3,x0,x1,f(x0),f(x1),&itr);
         if(fabs(x3-x2)<err)
      {
         printf("after %d th iterations root=%6.4f\n",itr,x3);
      getch();
      return 0;
      }
      x2=x3;
        }while(itr<N);
      printf("solution has more error for given number of iteration");
      return 1;
      getch();
}



// now function false//

void false(float *x,float x0,float x1,float fx0,float fx1,int *itr)
{
     *x=x0-((x1-x0)/(fx1-fx0))*fx0;
     ++(*itr);
     printf("iteration no.%3d x=%7.5f\n",*itr,*x);
}



ª       Results: (False-position Method)
----------METHOD OF FALSE POSITION FOR SOLVING NONLINEAR EQUATIONS-----------
enter starting values,max error,max iteration
2 9 .00005 99
iteration no.  1 x=8.24000
iteration no.  2 x=2.98913
iteration no.  3 x=3.99729
iteration no.  4 x=4.81304
iteration no.  5 x=5.33790
iteration no.  6 x=5.62234
iteration no.  7 x=5.76140
iteration no.  8 x=5.82585
iteration no.  9 x=5.85496
iteration no. 10 x=5.86796
iteration no. 11 x=5.87373
iteration no. 12 x=5.87629
iteration no. 13 x=5.87743
iteration no. 14 x=5.87793
iteration no. 15 x=5.87815
iteration no. 16 x=5.87825
iteration no. 17 x=5.87829
after 17 th iterations root=5.87828970