Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created April 17, 2020 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/2b0624916aed0d63e40604fe300da850 to your computer and use it in GitHub Desktop.
Save parzibyte/2b0624916aed0d63e40604fe300da850 to your computer and use it in GitHub Desktop.
/*
____ _____ _ _ _
| _ \ | __ \ (_) | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___|
__/ | __/ |
|___/ |___/
Blog: https://parzibyte.me/blog
Ayuda: https://parzibyte.me/blog/contrataciones-ayuda/
Contacto: https://parzibyte.me/blog/contacto/
Copyright (c) 2020 Luis Cabrera Benito
Licenciado bajo la licencia MIT
El texto de arriba debe ser incluido en cualquier redistribución
===========================================
Método de la bisección en C++
===========================================
*/
#include <iostream>
#include <cmath>
using namespace std;
float resolverEcuacion(float valor) {
// Con la ecuación: X^3 + 4X ^2 -10
return pow(valor, 3) + 4 * (pow(valor, 2)) - 10;
}
int main() {
int iterador = 1;
/*
a = inferior
b = superior
*/
float a = 1;
float b = 2;
float xi_xi = 0;
float ultimo_xi = 0;
printf(
"|-----------------------------------------------------------------------"
"--------------------------------------------\n");
printf(
"| i | a | b | f(a) | f(b) | xi "
" | f(xi) |f(a) * f(xi) | xi-xi_ |\n");
printf(
"|-----------------------------------------------------------------------"
"--------------------------------------------\n");
while (1) {
float fa = resolverEcuacion(a);
float fb = resolverEcuacion(b);
float xi = (a + b) / 2;
float fxi = resolverEcuacion(xi);
float fa_fxi = fa * fxi;
xi_xi = abs(xi - ultimo_xi);
printf(
"|%3d |%12.8f |%12.8f |%12.8f |%12.8f |%12.8f |%12.8f |%12.8f "
"|%12.8f\n",
iterador, a, b, fa, fb, xi, fxi, fa_fxi, xi_xi);
iterador++;
if (fa_fxi > 0) {
a = xi;
} else {
b = xi;
}
ultimo_xi = xi;
if (xi_xi == 0) {
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment