Newton-Raphson algorithm

Asked

Viewed 899 times

0

consider the Newton-Raphson algorithm for calculating the roots of the equation f(x)=0 with (0.5, 1 , 2 , 3.4556) for each initial x.

f(X)=X 4-12X 3+47x 2-60X I made the same code as the algorithm, but this not finding the roots, someone can help me?

#include <stdio.h>

float Abs( float x ){

  return x>=0? x:-x;
};

int main(){
  float xini = 0, xnovo , Fxnovo, Fdxnovo , E ;     
  int k = 0;

  printf( "Digite o x inicial: " );
  scanf( "%f", &xnovo );
  printf( "Digite a precisao: " );
  scanf( "%f", &E );

  do{

    xini = xnovo;
    Fxnovo = ( xini * xini * xini * xini ) - 12 * (xini * xini * xini) + 47*(xini * xini) - 60 * xini; //inserir sua função principal aqui
    Fdxnovo = 4. * ( xini * xini * xini) - 36. * (xini * xini ) + 94 * xini - 60; //inserir a derivada da função principal aqui
    xnovo = xini - ( Fxnovo / Fdxnovo );
    k += 1;
    printf( "\niteracao = %d", k );
    printf( "\nxini = %f\nxnovo = %f", xini, xnovo, Fxnovo, Fdxnovo );

  } while(Abs(Fxnovo) > E);

  printf( "\n\nxnovo - xini = %f\n", xnovo -(xini) );
  printf( "A solucao final e: %f\n", xnovo );

  return 0;
}
  • What is this Newton-Raphsom algorithm? For approximate root calculus by numerical methods?

  • Yes for numerical root calculation.

  • Yes for numerical root calculation

  • The algorithm predicts that the variation of x is -f(x)/f'(x) by iteration? Should not jump to the point where the line formed by the derivative of that point intersects the axis x?

  • If this variation really goes to the point where the line meets the axis x, So let me know I’m not showing

  • Okay, I saw that this variation will actually find the point of intersection of the line with the axis x

  • yes x variation is by interaction.

  • Do you know how to define functions in C? If so, we can test the validity of your algorithm with low cost. Instead of putting the resolution of the polynomial and the resolution of its derived function directly, do Fxnovo = func(xini) and Fdxnovo = deriv(xini). To test if everything is in accordance, we can test with func(x) = x^2 - x (roots 0 and 1) and deriv(x) = 2 x - 1. Beginning with x = 4, should converge quickly to one of the roots

  • ixi bro I haven’t studied functions no .

  • Well, you used the Abs. And it’s not really returning the absolute value of a positive number. Do Abs(4), your result will be -4

Show 5 more comments

1 answer

0


The error is in the definition of Abs. Currently, Abs(4) will return -4. So just find the first f(x) positive that will come out of the loop.

The correction is as follows:

float Abs( float x ){
  return x >=0? x: -x;
}
  • How is it float, can make a joke only with the signal bit, not needing therefore any comparison.

  • Bro worked out really thank you.

  • From what you’re saying, it seems to be the case mark this reply as accepted. Here we do not write "solved" in the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

  • I just don’t know how to do it. I’m new here.

  • This link can help you with this: https://pt.meta.stackoverflow.com/a/1079/64969

  • 1

    Jefferson, to make operator & reset the signal bit requires the float to be moved from the float register to the memory or whole register to apply the operation, correct? From what I’ve tested, doing this kind of thing takes performance. It is better to use the "abs" function in its intrinsic version, depending even apply the operator & SSE technology.

  • @RHERWOLF I tried without using abs intrinsic. It seemed that the intention was to elaborate all the operations related to the calculation of the roots. But this part of the & need first go to a register of whole via memory was ignorance of mine, I thought I could do directly via register float. Thanks for showing me wrong =)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.