-
Lenguaje
Java
-
Descripción
Método de Newton-Raphson para encontrar la solución de:
f(x) = cos(x) - x³
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
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
import java.util.Scanner;
public class NewtonRaphson {
public static double funcion(double x) {
return Math.cos(x) - x * x * x;
}
public static double derivada(double x) {
return -Math.sin(x) - 3.0 * x * x;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Metodo de Newton-Raphson, C\240lculo de la funci\242n: cos(x) - x\374\n");
System.out.print("Ingrese el valor inicial de x0: ");
double err, x_1, x = in.nextDouble();
int i=0;
System.out.println();
do {
x_1 = x;
x = x - funcion(x) / derivada(x);
err = Math.abs((x - x_1)/x);
System.out.println("x" + i + " = " + x_1 + "\t\terror = " + err);
i++;
} while (x!=x_1 && i<100);
if (i==100)
System.out.println("\nLa soluci\242n no es convergente.\n");
else
System.out.println("\nLa soluci\242n es " + x + "\n");
}
}
public class NewtonRaphson {
public static double funcion(double x) {
return Math.cos(x) - x * x * x;
}
public static double derivada(double x) {
return -Math.sin(x) - 3.0 * x * x;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Metodo de Newton-Raphson, C\240lculo de la funci\242n: cos(x) - x\374\n");
System.out.print("Ingrese el valor inicial de x0: ");
double err, x_1, x = in.nextDouble();
int i=0;
System.out.println();
do {
x_1 = x;
x = x - funcion(x) / derivada(x);
err = Math.abs((x - x_1)/x);
System.out.println("x" + i + " = " + x_1 + "\t\terror = " + err);
i++;
} while (x!=x_1 && i<100);
if (i==100)
System.out.println("\nLa soluci\242n no es convergente.\n");
else
System.out.println("\nLa soluci\242n es " + x + "\n");
}
}