Solution of Non – linear equation in single variable using the Regula – Falsi, Newton –Raphson method. - Computer Programming

# Computer Programming

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

## Tuesday, December 13, 2011

Aim: - Solution of Non – linear equation in single variable using the Regula – Falsi, Newton –Raphson method.

ALOGRITHM:: -

Newton – Raphson Method: -
1.  Assign an initial value to x, say x0
2.  Evaluate f(x0) & (x0)
3.  Find the improved estimation of x0

X1 = x0 – f(x1)/f (x0)
4.  Check the accuracy of the latest estimate.
Compare relative error to a predefined value E. if [(x1 – x0)/x1]<=E
Stop; otherwise continue.
5.  Replace x0 by x1 and repeat step 3 and 4.

PROGRAM

/* Regula falsi method*/

#include< stdio.h >
#include< conio.h >
#include<math.h>
float f(float x)
{
return cos (x) – x*exp(x);
}
void regula (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);
}

main()
{
int itr=0,maxitr;
float x0,x1,x2, aerr, x3;
printf("enter the values for x0,x1,allowed error ,maxinum iterations\n");
scanf("%f %f %f %d ",&x0,&x1,&aerr,&maxitr);
regula(&x2,x0,x1,f(x0),f(x1),&itr);
do
{
if (f(x0)*f(x2)< 0)

5

x1=x2;
else
x0=x2;
regula(&x3,x0,x1,f(x0),f(x1),&itr);
if(fabs(x3-x2) < aerr)
{
printf("after %d iterations,root = %6.4f\n",itr,x3);
getch();
return 0;
}
x2=x3;

}
while(itr<maxitr);
printf("solution doesnt converge,iterations not sufficient");
return 1;
}

Result: -
Enter the value for x0, x1, allowed error, maximum iterations.
……….
Iteration No.1 x=
Iteration No.2 x=
……….
………
After iterations, root=