1
#include<stdio.h>
#include<conio.h>
#include<math.h>
double valOfFuncAt(double );
double derivativeAt(double );
double a,b,c,d;

int main(){
    double xo;
    double x1;
    double fx,f_x;

    printf("Enter The Coefficients Of The Cubic Equation");
    scanf("%f%f%f%f",&a,&b,&c,&d);
    printf("Enter The First Approximate Root");
    scanf("%f",&xo);
    fx=valOfFuncAt(xo);
    f_x=derivativeAt(xo);
    x1= xo-(fx/f_x);
    while(valOfFuncAt(x1) >= 0.0001){
        fx=valOfFuncAt(x1);
        f_x=derivativeAt(x1);
        x1= x1 - (fx/f_x);

    }
    printf("\nApproximate Root Of The Equation Is : %f",x1);
    getch();
    return 0;
}

double valOfFuncAt(double x){
    double fx1;
    fx1 = (a*x*x*x) + (b*x*x) + (c*x) +d;
    return fx1;
}
double derivativeAt(double x){
    double f_x1;
    f_x1 = (3*a*x*x) + (2*b*x) + c;

    return f_x1;
}

When I am running it for any cubic equation in code blocks, for instance x^3-3x+1=0, and giving 0.3 be the first approximate root, it is producing the wrong output and I am not getting what I did wrong in the logic!

halfer
  • 19,824
  • 17
  • 99
  • 186
  • `%lf` format specifier you have to use in `scanf`. – user2736738 Jan 26 '18 at 04:24
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jan 26 '18 at 10:44

3 Answers3

1

The Newton-Raphson equation is

equation

You find a new value of x after each iteration. Then you got to use the new value to find the output of the function and its derivative.

You can do this with just x. No need of 2 variables. Like

double x;
printf("Enter The First Approximate Root");
scanf("%lf",&x);
while( valOfFuncAt(x)>=0.0001 )
{
    x = x-valOfFuncAt(x)/derivativeAt(x);
}

You are using double variables for which format specifier should be %lf and not %f(which is for float).

And you have no need of math.h here.

Also, conio.h and getch() are not standard and are better avoided.

J...S
  • 5,079
  • 1
  • 20
  • 35
0

The correct format specifier for double in scanf would be %lf. Otherwise you are having undefined behavior using wrong format specifier for the double variable. Correct one would be

scanf("%lf",&xo);

From standard §7.21.6.2p11

l (ell) Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to long int or unsigned long int; that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer to double; or ...

user2736738
  • 30,591
  • 5
  • 42
  • 56
0

You have used %f as format specifier in scanf() to enter the root but %f is used to input float type data and as the data type of xo is double use %lf as format specifier in scanf() and also in printf() function which prints the result.

J...S
  • 5,079
  • 1
  • 20
  • 35