Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Métodos numéricos

Método punto fijo


Código Método punto fijo
public class Punto_Fijo {
public static void main(String[] args) {
int i = 0;
int max_i = 15;
double x0 = 1.0;
double EPS = 1e-4;
double x;
System.out.println("i"+"\t"+"xi"+"\t"+"g(xi)"+"\t"+"EPS");
while (i < max_i) {
//x = Math.sqrt((x0+5)/2);
//x = Math.cos(x0)/3;
x = 20/(x0*x0+2*x0+10);
if(Math.abs(x0-x)<EPS){
System.out.println("Raiz hallada");
System.out.println("x"+i+" = "+String.format("%.5f", x));
System.exit(0);
}
System.out.println(i+"\t"+String.format("%.5f", x0)+"\t"
+String.format("%.5f", x)+"\t"+String.format("%.5f",
Math.abs(x0-x)));
i++;
x0 = x;
}
System.out.println("Raiz no hallada :/");
System.exit(0);
}
}
Método Newton Raphson
public class Newton_Raphson {
@SuppressWarnings("SuspiciousIndentAfterControlStatement")
public static void main(String[] args) {
int i = 0;
int max_i = 15;
double x0 = 1.0;
double EPS = 1e-4;
double x;
while (i < max_i) {
x = x0-(x0*x0*x0+2*x0*x0+10*x0-20)/(3*x0*x0+4*x0+10);
if(Math.abs(x0-x)<EPS){
System.out.println("Raiz hallada");
System.out.println(x);
System.exit(0);
}
i++;
x0 = x;
}
System.out.println("Raiz no hallada :/");
System.exit(0);
}
}
Raiz hallada
1.3688081078213745
Método Secante
public class Secante {
@SuppressWarnings("SuspiciousIndentAfterControlStatement")
public static void main(String[] args) {
int i = 0;
int max_i = 15;
double x0 = 0.0;
double x1 = 1.0;
double EPS = 1e-4;
double x;
while (i < max_i) {
x = x1-((x1-x0)*(x1*x1*x1+2*x1*x1+10*x1-20))/((x1*x1*x1+2*x1*x1+10*x1-
20)-(x0*x0*x0+2*x0*x0+10*x0-20));
if(Math.abs(x0-x)<EPS){
System.out.println("Raiz hallada");
System.out.println(x);
System.exit(0);
}
i++;
x0 = x1;
x1 = x;
}
System.out.println("Raiz no hallada :/");
System.exit(0);
}
}
Raiz hallada
1.368808107821371

You might also like