Latest Post

Ads

Optimization: Newton Raphson Method

Newton Raphson Method Algorithm in C

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
float myFun(float x);
int main()
{
 float e = 0.01,a,aw = 0,bw = 1,Lw,b,x1,x2,fxa,fxb,fxz,z,j=0;
 int i=1,k;
 float x[100],dx[100],fdx[100],fddx[100];
 x[1] = 1;
 dx[1] = (x[i]*1)/100;
 begin:
 if(j<100)
 {
  dx[i] = (x[i]*1)/100;
  fdx[i] = ((myFun(x[i] + dx[i])) - (myFun(x[i] - dx[i])))/(2*dx[i]);
  fddx[i] = ((myFun(x[i] + dx[i])) - (2*myFun(x[i])) + (myFun(x[i] - dx[i])))/(dx[i]*dx[i]);
  printf("\nf'(%d) = %.4f",i,fdx[i]);
  printf("\nf''(%d) = %.4f",i,fddx[i]);
  k = i+1;
  x[k] = (x[i]  - (fdx[i] / fddx[i]));
  printf("\n\nf(%d) = %.4f",k,x[k]);
  dx[k] = (x[k]*1)/100;
  fdx[k] = ((myFun(x[k] + dx[k])) - (myFun(x[k] - dx[k])))/(2*dx[k]);
  if (fdx[k] < 0)
  {
   fdx[k] = ((-1)*fdx[k]);
  }
   if (fdx[k] > e)
   {
    j = j + 1;
    i = i + 1;
    goto begin;
   }
   else
   {
    printf("\n\nf'(%d) = %.9f is Less than termination factor e = %.3f",k,fdx[k],e);
    goto end;
   }
 }

 end:
 return 0;
}



 float myFun(float x)   {
    float y;
    y = ((x*x)+(54/x));
    return y;                  // return statement
}

Output:
Image of Newton Raphson Output
Newton Raphson Method

Comments

Ads

Popular posts from this blog

VLSI: BCD to Excess 3 and Excess 3 to BCD Dataflow Modelling

Full Subtractor Verilog Code in Structural/Gate Level Modelling with Testbench

VLSI: 4-1 MUX Dataflow Modelling with Testbench

VLSI: Half Subtractor and Full Subtractor Gate Level Modelling

VLSI: 1-4 DEMUX (Demultiplexer) Dataflow Modelling with Testbench