#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <limits>
#include <cmath>
float E = 0;
using namespace std;
float f (float x)
{
float fx1;
fx1 = -1/pow(x, 3) - 1/pow(x, 2) - E;
return (fx1);
}
float df (float x)
{
float fx1;
fx1 = (2*x + 3)/pow(x, 4);
return (fx1);
}
void falseMethod(float i_range, float e_range) {
cout << "metodo falso valor no intervalo [" << i_range << ", " << e_range << "]" << endl;
float a = e_range;
float b = i_range;
float output = 0;
float c = 0;
float c0 = b;
int i = 0;
while (true) {
c = a - ((b - a)/(f(b) - f(a)))*(f(a));
i++;
if (abs(c - c0) < numeric_limits<float>::epsilon()*fmax(1.0, abs(c))) {
output = c;
break;
} else {
c0 = c;
if (f(a)*f(c) > 0)
a = c;
else
b = c;
}
}
cout << "interacoes: " << i << endl;
cout << "saida: " << c << endl;
}
void bisectionMethod(float i_range, float e_range) {
cout << "metodo bisecante no intervalo [" << i_range << ", " << e_range << "]" << endl;
int count = 0;
float x3 = 0;
float x1 = i_range;
float x2 = e_range;
do
{
count++;
x3 = (x1 + x2)/2;
if( f(x1) * f(x3) < 0 )
{
x2 = x3;
}
else
{
x1 = x3;
}
}
while (abs(x1 - x2) > numeric_limits<float>::epsilon()*fmax(1.0, abs(x3)) || f(x3) == 0);
cout << "interacoes: " << count << endl;
cout << "saida: " << x3 << endl;
}
void newtonMethod(float init_value) {
cout << "metodo newton no ponto [" << init_value << "]" << endl;
float x0 = init_value;
float h, x1;
int count = 0;
while(true) {
count++;
float allerr = numeric_limits<float>::epsilon()*fmax(1.0, abs(x1));
h=f(x0)/df(x0);
x1=x0-h;
if (fabs(h) < allerr)
{
break;
}
x0=x1;
}
cout << "interacoes: " << count << endl;
cout << "saida: " << x1 << endl;
}
int main() {
for (int i=3;i<=5;i++){
cout << "iteracao para E = -" << i <<endl;
E = -i;
falseMethod(0.01, 1);
cout << endl;
bisectionMethod(0.01, 1);
cout << endl;
newtonMethod(1.0);
cout << "-----------------------------------" << endl;
cout << endl;
}
}
iteracao para E = -3
metodo falso valor no intervalo [0.01, 1]
interacoes: 350406
saida: 0.863511
metodo bisecante no intervalo [0.01, 1]
interacoes: 23
saida: 0.851383
metodo newton no ponto [1]
interacoes: 5
saida: 0.851383
-----------------------------------
iteracao para E = -4
metodo falso valor no intervalo [0.01, 1]
interacoes: 348222
saida: 0.769684
metodo bisecante no intervalo [0.01, 1]
interacoes: 23
saida: 0.76069
metodo newton no ponto [1]
interacoes: 6
saida: 0.76069
-----------------------------------
iteracao para E = -5
metodo falso valor no intervalo [0.01, 1]
interacoes: 320419
saida: 0.704765
metodo bisecante no intervalo [0.01, 1]
interacoes: 23
saida: 0.697628
metodo newton no ponto [1]
interacoes: 8
saida: 0.697628
-----------------------------------