C Program For Newton-Raphson Method

The Newton-Raphson method (also known as Newton’s method) is a way to quickly find a good approximation for the root of a real-valued function f(x)=0. It uses the idea that a continuous and differentiable function can be approximated by a straight line tangent to it.

Newton-Raphson Method Solved Example

Newton-Raphson Method Solved Example

C Program

//Newton-Raphson Method
//This Code is written by Souvik Ghosh
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
/* Defining equation to be solved.
   Change this equation to solve another problem. */
#define f(x) x*x-4*x-7
/* Defining derivative of g(x).
   As you change f(x), change this function also. */
#define g(x) 2*x-4
//main function
void main()
{
	 float x0, x1, f0, f1, g0, e;
	 int step = 1, N;
     /* Inputs */
	 printf("\nEnter initial guess:\n");
	 scanf("%f", &x0);
	 printf("Enter tolerable error:\n");
	 scanf("%f", &e);
	 printf("Enter maximum iteration:\n");
	 scanf("%d", &N);
	 /* Implementing Newton Raphson Method */
	 printf("\nStep\t\tx0\t\tf0\t\tg0\t\tx1\n");
	 do
	 {
	      f0 = f(x0);
		  g0 = g(x0);
		  if(g0 == 0.0)
		  {
			   printf("Mathematical Error.");
			   break;
		  }
		  x1 = x0 - f0/g0;
		  printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,f0,g0,x1);
		  x0 = x1;
		  step++;
		  if(step > N)
		  {
			   printf("Not Convergent.");
			   break;
		  }

		  f1 = f(x1);

	 }while(fabs(f1)>e);

	 printf("\nThe Root is: %0.4f", x1);
}

Output Terminal

Newton-Raphson Method C Program Output

Related Posts

CSS Cheatsheet

Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…

HTML Cheatsheet

Unleash your web development potential with my HTML cheatsheet! Elevate your coding skills, master essential tags and attributes, and build stunning websites with ease. Whether you’re a…

Git&GitHub Cheatsheet

Elevate your Git and GitHub game with my ultimate cheatsheet! Unlock the power of version control, streamline collaboration, and boost productivity. Navigate the complexities of Git commands…

Command Line Cheatsheet

Master the Command Line effortlessly with my comprehensive cheatsheet! Streamline your workflow, boost productivity, and become a command line guru. Unlock essential commands, shortcuts, and tips to…

OpenAI’s APIs Notes

Unlock the potential of OpenAI’s APIs with my comprehensive notes! Dive into the world of cutting-edge artificial intelligence, explore the capabilities of GPT-4 and beyond, and learn…

SQL Notes

Discover the essence of SQL with my comprehensive notes! Dive into the world of Structured Query Language, unraveling the complexities, and mastering database management. Elevate your SQL…

Leave a Reply

Your email address will not be published. Required fields are marked *