0% found this document useful (0 votes)
74 views1 page

Newton-Raphson Method in C Code

The document provides a C program that implements the Newton-Raphson method to find the root of a cubic function. It prompts the user for an initial guess, allowed error, and maximum iterations, then iteratively calculates the root until the desired accuracy is achieved or the maximum iterations are reached. If the solution does not converge, it returns a notification indicating insufficient iterations or failure to converge.

Uploaded by

sdas76426
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views1 page

Newton-Raphson Method in C Code

The document provides a C program that implements the Newton-Raphson method to find the root of a cubic function. It prompts the user for an initial guess, allowed error, and maximum iterations, then iteratively calculates the root until the desired accuracy is achieved or the maximum iterations are reached. If the solution does not converge, it returns a notification indicating insufficient iterations or failure to converge.

Uploaded by

sdas76426
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include<stdio.

h>
#include<math.h>
float f(float x)
{
return x*x*x-4*x-9;
}
float df (float x)
{
return 3*x*x-4;
}
void main()
{
int itr, maxmitr;
float h, x0, x1, allerr;
printf("\nEnter x0, allowed error and maximum iterations\n");
scanf("%f %f %d", &x0, &allerr, &maxmitr);
for (itr=1; itr<=maxmitr; itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1);
if (fabs(x1-x0) < allerr)
{
printf("After %3d iterations, root = %8.6f\n", itr, x1);
return 0;
}
x0=x1;
}
printf("The required solution does not converge or iterations are insufficient\n");
return 1;
}

REMARK: Both in Bisection method and in Newton Raphson, “return 1” is used to represent the
notification that "The required solution does not converge or iterations are insufficient". This implies
that the program did not run as it should and/or you did not get the desired result.

You might also like