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?
– Jefferson Quesado
Yes for numerical root calculation.
– Carlos Ferreira
Yes for numerical root calculation
– Carlos Ferreira
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 axisx
?– Jefferson Quesado
If this variation really goes to the point where the line meets the axis
x
, So let me know I’m not showing– Jefferson Quesado
Okay, I saw that this variation will actually find the point of intersection of the line with the axis
x
– Jefferson Quesado
yes x variation is by interaction.
– Carlos Ferreira
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)
andFdxnovo = deriv(xini)
. To test if everything is in accordance, we can test withfunc(x) = x^2 - x
(roots 0 and 1) andderiv(x) = 2 x - 1
. Beginning withx = 4
, should converge quickly to one of the roots– Jefferson Quesado
ixi bro I haven’t studied functions no .
– Carlos Ferreira
Well, you used the
Abs
. And it’s not really returning the absolute value of a positive number. DoAbs(4)
, your result will be-4
– Jefferson Quesado