-
Notifications
You must be signed in to change notification settings - Fork 0
/
raiz.c
70 lines (63 loc) · 1.88 KB
/
raiz.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "raiz.h"
#include <stdio.h>
#include <math.h>
int bissecao (double a, double b, int p, double (*f) (double x), double* r){
double fa = f(a);
double fb = f(b);
double fc, c;
double ErroAvaliado = (b - a)/2.0;
double Tol = 0.5*pow(10.0,-p);
int numInt = 0;
if(fa*fb < 0){ // Se tiver sinal invertido
while(ErroAvaliado > Tol){
c = (a+b)/2;
fc = f(c);
if(fc == 0){
printf("%d é a raiz da funcao.\n", c);
break;
}
else if(fa*fc < 0){
b = c;
}
else if(fc*fb < 0){
a = c;
}
ErroAvaliado *= 0.5;
numInt++;
printf("%d interacao:\nc = %.5lf\nb = %.5lf\na = %.5lf\nErroAvaliado = %.5lf\n\n", numInt, c, b, a, ErroAvaliado);
}
*r = c;
}
else{
printf("Sinais nao invertidos nesse intervalo.\n");
return 0;
}
return numInt;
}
int IQI (double x0, double x1, double x2, int p, double (*f) (double x), double* r){
double coefPar = det(f,x0,x1,x2);
double Tol = 0.5*pow(10.0,-p);
double fx0 = f(x0), fx1 = f(x1), fx2 = f(x2);
int tentativas = 0,maxTent = 40;
while(abs(fx2) > Tol){
x0 = x1;
x1 = x2;
x2 = coefPar;
coefPar = det(f,x0,x1,x2);
printf("Coeficiente do parametro = %.5lf\nx0 = %.5lf\nx1 = %.5lf\nx2 = %.5lf\n\n",coefPar,x0,x1,x2);
tentativas++;
if(tentativas > maxTent){
return maxTent;
}
*r = coefPar;
return tentativas;
}
double det(double (*f) (double x), double x0, double x1, double x2){
double fx0 = f(x0), fx1 = f(x1), fx2 = f(x2);
double diagPA, diagSA, diagPAc, diagSAc; //Diagonal Principal de A, Diagonal Secundária de A, Diagonal Principal de Ac, Diagonal Secundária de Ac
diagPA = (fx0*fx0*fx1*1) + fx0*1*fx2*fx2 + fx1*fx1*fx2;
diagSA = fx2*fx2*fx1 + fx2*fx0*fx0 *fx1*fx1*fx0;
diagPAc = fx0*fx0*fx1*x2 + fx0*x1*fx2*fx2 + fx1*fx1*fx2*x0;
diagSAc = fx2*fx2*fx1*x0 + fx2*fx0*fx0*x1 *fx1*fx1*fx0*x2;
return (diagPAc - diagSAc)/(diagPA - diagSA);
}