ProgIng - Programación en Ingeniería
Loading...
Searching...
No Matches
Ejemplo013.c File Reference

Aproximación de ln(x) con serie tipo atanh usando recurrencia entre términos. More...

#include <stdio.h>
Include dependency graph for Ejemplo013.c:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])

Detailed Description

Aproximación de ln(x) con serie tipo atanh usando recurrencia entre términos.

Se usa la misma identidad que en Ejemplo012: ln(x) = 2 * sum_{i=0}^{∞} f^{2i+1}/(2i+1), f = (x - 1) / (x + 1), x > 0.

A diferencia del Ejemplo012, aquí NO se recalcula la potencia desde cero. Se usa la recurrencia de términos: t_i = f^{2i+1}/(2i+1) t_{i+1} = t_i * f^2 * (2i+1)/(2i+3)

Entrada
  • Un entero n >= 1.
  • Un real x > 0.
Salida
Imprime:
ln(x) = valor
Precondiciones
  • x > 0.
Complejidad
Tiempo: O(n). Memoria: O(1).
Note
Converge más rápido cuando x está cerca de 1 (|f| pequeño).

Definition in file Ejemplo013.c.

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 37 of file Ejemplo013.c.

38{
39 (void)argc;
40 (void)argv;
41
42 int n, i;
43 float x; /* valor para ln(x) */
44 float lnx; /* acumulador de la serie */
45 float fct; /* f = (x-1)/(x+1) */
46 float fct2; /* f^2 */
47 float suma; /* término actual t_i = f^{2i+1}/(2i+1) */
48
49 /* Leer n >= 1 */
50 do {
51 printf("Ingres el numero de terminos: ");
52 scanf("%d", &n);
53 } while (n < 1);
54
55 /* Leer x > 0 */
56 do {
57 printf("Ingrese el valor de x: ");
58 scanf("%f", &x);
59 } while (x <= 0);
60
61 /*
62 Inicialización:
63 f = (x-1)/(x+1)
64 f2 = f^2
65 t0 = f^(1)/1 = f
66 lnx = 0
67 */
68 for (i = 0, fct = (x - 1) / (x + 1), lnx = 0, fct2 = fct * fct, suma = fct;
69 i < n;
70 i++)
71 {
72 /* Acumular término t_i */
73 lnx += suma;
74
75 /*
76 Actualizar al siguiente término:
77 t_{i+1} = t_i * f^2 * (2i+1)/(2i+3)
78 (evita recalcular potencias desde cero)
79 */
80 suma *= (fct2 * (2*i + 1)) / (2*i + 3);
81 }
82
83 /* ln(x) = 2 * serie */
84 lnx *= 2;
85
86 printf("ln(%f) = %f\n", x, lnx);
87 return 0;
88}
double fct(double x, int n)
Definition Ejemplo029.c:34
Here is the call graph for this function: