Skip to content

Instantly share code, notes, and snippets.

@christian-candido
Created March 18, 2012 20:00
Show Gist options
  • Save christian-candido/2080596 to your computer and use it in GitHub Desktop.
Save christian-candido/2080596 to your computer and use it in GitHub Desktop.
Lista 1 - Cálculo Numérico
/*
* Cálculo do zero da função pelo método da bissecção
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2012 - Christian <christianc.candido@gmail.com>
*
*/
#include <stdio.h>
#include <math.h>
double f ( double );
int main (void) {
double epsilon = 0.000001; // Resíduo aceito
double a = 0.01, b = 0.1; // Extremos do intervalo
double x1, x2, x3; // Pontos no intervalo
double f1, f2, f3; // Valores da função no pontos
double imax = 100; // Limite de iterações
double delta_x; // Resíduo a cada tentativa
int i;
x1 = a;
x3 = b;
f1 = f(x1);
f3 = f(x3);
for (i = 1; i <= imax; i++) {
x2 = (x1 + x3) / 2.0;
f2 = f(x2);
if (f1 * f2 <= 0.0) { // A raíz está na primeira metade do intervalo
delta_x = fabs(x2 - x1) / 2.0;
f3 = f2;
x3 = x2;
} else { // A raíz está na segunda metade
delta_x = fabs(x3 - x2) / 2.0;
f1 = f2;
x1 = x2;
}
if (delta_x < epsilon) {
printf("A raíz é: %.10f\n", x2);
printf("Foi encontrada em %d iterações \n", i);
return 0;
}
}
return 0;
}
double f (double r) {
float M = 6000; // Montante após 5 anos
float n = 5; // Anos
float v = 1000; // Investimento anual
return M - v * ( ( 1 + r ) / r ) * (pow( 1 + r, n ) - 1);
}
/*
* Cálculo do zero da função pelo método de Newton-Raphson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2012 - Christian <christianc.candido@gmail.com>
*
*/
#include <stdio.h>
#include <math.h>
double f ( double );
double f_linha ( double );
int main(void) {
double epsilon = 0.000000000001; // Resíduo aceito
double x_i, x_i1; // Pontos no intervalo
double delta_x; // Resíduo a cada tentativa
int imax = 20; // Limite de iterações
int i = 0;
double x_0 = 0.03; // Chute inicial
x_i = x_0;
for (i = 1; i <= imax; i++) {
x_i1 = x_i - f( x_i ) / f_linha( x_i );
if( fabs(x_i1 - x_i) < epsilon ) {
printf("A raíz é: %.10f\n", x_i1);
printf("Foi encontrada em %d iterações \n", i);
return 0;
}
x_i = x_i1;
}
return 0;
}
double f (double r) {
float M = 6000; // Montante após 5 anos
float n = 5; // Anos
float v = 1000; // Investimento anual
return M - v * ( ( 1 + r ) / r ) * (pow( 1 + r, n ) - 1);
}
double f_linha (double r) { // Derivada analítica de f
return -1000 * (6 * pow( 1+r, 5 ) / r - pow(1 + r, 6) / pow (r, 2) + 1 / pow(r, 2) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment