Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C Language create a root solver for a function f(x). The Newton-Raphson recur

ID: 3760296 • Letter: I

Question

In C Language

create a root solver for a function f(x). The Newton-Raphson recursive formula to find a root of a function is: xn+1 = xn f(xn) /f '(xn) Where f'(x) is the derivative of f(x), which is f(x+dx)f(x) dx as dx approaches 0. Your assignment is to create a program that finds a root of a C function double f(double x); 1. define two constants ACCURACY and DX. They should be defined with the values 1.0E-6 and 1.0E-8 respectively. 2. Create three versions of the function f(x) – note that at any time , two of those versions should be commented out (Hint: use /* */). (a) f(x) = x 3 125 (b) f(x) = e x x; note that the math.h function exp(x) raises e x . (c) f(x) = any function, polynomial, etc that you wish. Each version of your function should print a descriptive line that gives the definition of the function, the value it is being called with and the value returned. E.g., for the first version of f(x) it should print out a line like: For x=1.00000, f(x) = x cubed - 125 = -124.00000. Note that this line may be printed several times with the same value of x, since f(x) may be called multiple times in each step of the recursion. 3. Create a function derivative which takes a single argument x and returns the derivative for the function f(x) at the point x. Your single derivative function should work for all three (and any other) definition of f(x). 4. Create a recursive function that takes a single argument x and returns a root a value for xn such that f(xn) = 0. Your function should be recursive such that it checks to see if |f(x)| is smaller than ACCURACY, and if it is not it uses the Newton-Raphson method to call itself with a new potential value for the root. Your function should check the to make sure f'(x) 6= 0. If it is, your function should print out a message that the derivative is 0 and that it cannot find a root, and then it should return. 5. Your main function should print out a root value for f(x) using the value 1.0 as the value of x0.

Explanation / Answer

1)

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

int
max_power,i=0,cnt=0,flag=0;
int
coef[10]={0};
float
x1=0,x2=0,t=0;
float
fx1=0,fdx1=0;

int
main()
printf(" C PROGRAM FOR NEWTON RAPHSON METHOD");
printf(" ENTER THE MAXIMUM POWER OF X = ");
  
scanf("%d",&max_power);
for(i=0;i<=max_power;i++)
  
{
      
printf(" x^%d = ",i);
scanf("%d",&coef[i]);
  
}
printf(" ");
printf(" THE POLYNOMIAL IS = ");
  
for(i=max_power;i>=0;i--)

{
printf(" %dx^%d",coef[i],i);
}

  
printf(" First approximation x1 ----> ");
  
scanf("%f",&x1);
printf(" ITERATION x1 F(x1) F'(x1) ");

do
  
{
          
cnt++;
          
fx1=fdx1=0;
          
for
(i=max_power;i>=1;i--)
          
{
              
fx1+=coef[i] * (
pow
(x1,i)) ;
          
}
          
fx1+=coef[0];
          
for
(i=max_power;i>=0;i--)
          
{
              
fdx1+=coef[i]* (i*
pow
(x1,(i-1)));
          
}
          
t=x2;
          
x2=(x1-(fx1/fdx1));

          
x1=x2;

          
printf
(
" %d %.3f %.3f %.3f "
,cnt,x2,fx1,fdx1);

  
}
while
((
fabs
(t - x1))>=0.0001);
  
printf
(
" THE ROOT OF EQUATION IS = %f"
,x2);
  
getch();
}

2)