Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- float funcao(int x){
- float func;
- func = pow(x, 2) - 1;
- return func;
- }
- float derivada(int x){
- float func_derivada;
- func_derivada = 2*x;
- return func_derivada;
- }
- void newton(int x_in, double erro_desejado, int max_it){
- int it=0 ;
- double erro=1;
- double x_at =0;
- while((erro > erro_desejado)||(it >= max_it)){
- x_at = x_in - (funcao(x_in)/derivada(x_in));
- erro = (abs(x_at - x_in))/x_at;
- it = it +1;
- x_in = x_at;
- printf("Erro relativo %.20f\n", erro);
- printf("Numero de interacoes %d\n", it);
- printf("Raiz da equacao %.20f\n", x_at);
- printf("*********************************\n\n");
- }
- }
- int main(){
- int valor;
- double precisao = 0.00001;
- int interacoes = 30;
- printf("Informe o chute inicial(x0): ");
- scanf("%d", &valor);
- newton(valor, precisao, interacoes);
- return 0;
- }
Add Comment
Please, Sign In to add comment