0% found this document useful (0 votes)
49 views

Import Java - math.BigDecimal

The document contains code for implementing the Regula Falsi method to find the root of a function. The method takes initial lower and upper bounds for the root, the maximum number of iterations, and an acceptable error. It iteratively calculates a new midpoint between the bounds and evaluates the function until the relative error is below the threshold or the maximum iterations is reached, returning the root or empty string. The code includes helper methods to round values and evaluate the sample function. The main method calls the solver on the function x^3 - 2x^2 + 3x - 8 between initial bounds of 2.2 and 2.5, finding a root of approximately 2.2482 in 3 iterations.

Uploaded by

RS Yavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Import Java - math.BigDecimal

The document contains code for implementing the Regula Falsi method to find the root of a function. The method takes initial lower and upper bounds for the root, the maximum number of iterations, and an acceptable error. It iteratively calculates a new midpoint between the bounds and evaluates the function until the relative error is below the threshold or the maximum iterations is reached, returning the root or empty string. The code includes helper methods to round values and evaluate the sample function. The main method calls the solver on the function x^3 - 2x^2 + 3x - 8 between initial bounds of 2.2 and 2.5, finding a root of approximately 2.2482 in 3 iterations.

Uploaded by

RS Yavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.math.

BigDecimal;

/**
* @see https://www.jc-mouse.net/
* @author mouse
*/
public class Regula_Falsi {

public Regula_Falsi(){}

/**
* Función a resolver
* @param x valor de incognita
* @return double
*/
public double fx(double x){
//x^3 - 2x^2 + 3x -8
return Math.pow(x, 3) - 2*Math.pow(x, 2) + 3*x - 8;
}

/**
* @param x1 Limite inferior
* @param x2 Limite superior
* @param iterations numero de iteraciones
* @param err Error permitido
* @return String resultado encontrado
* NULL en caso de encontrar algun error
* EMPTY en caso de no hallar convergencia al limite de
iteraciones permitidas
*/
public String resolver(double x1, double x2, int iterations, double err){

int numDec=5;

double x3, y3;


double xp=x1;
double y1=fx(x1);
double y2=fx(x2);

//Valores iniciales
System.out.println( " x1 = " + round(x1,numDec));
System.out.println( " x2 = " + round(x2,numDec));
System.out.println( " y1: fx("+x1+")= " + round(y1,numDec));
System.out.println( " y2: fx("+x2+")= " + round(y2,numDec));

for(int i=1; i<=iterations;i++){


System.out.println("Iteración #"+i);
if ( (y2-y1) == 0 ){
System.err.println("Error: no converge x=NaN; iteración="+i);
return null;
}

x3=(y2*x1-y1*x2)/(y2-y1);
System.out.println(" x3 = (y2*x1-y1*x2)/(y2-y1)");
System.out.println(" x3 = ((" + round(y2,numDec)+")*("+round(x1,numDec)
+")-("+round(y1,numDec)+")*("+round(x2,numDec)+"))/(("+round(y2,numDec)+")-
("+round(y1,numDec)+")) = " + round(x3,numDec) );

System.out.println("Error |"+round(xp,numDec)+"/"+round(x3,numDec)+"-1|
<"+round(err,numDec) + " " + (Math.abs(xp/x3-1)<err));
if (Math.abs(xp/x3-1)<err){
System.out.println(round(Math.abs(xp/x3-1),numDec)
+"<"+round(err,numDec) + " -> termina programa");
return "Iteración:" + i + " Valor x=" + String.valueOf(x3);
}

y3 = fx(x3);

System.out.println(" y1 = " + round(y1,numDec));


System.out.println(" y3 = fx(x3) = fx("+round(x3,numDec)+") = " +
round(y3,numDec));
System.out.println(" y1*y3 < 0 " + (y1*y3 < 0));
if (y1*y3<0) {
System.out.println(" x2=x3=" + round(x3,numDec));
System.out.println(" y2=y3=" + round(y3,numDec));
x2=x3;
y2=y3;
} else {
System.out.println(" x1=x3=" + round(x3,numDec));
System.out.println(" y1=y3=" + round(y3,numDec));
x1=x3;
y1=y3;
}
xp=x3;
}
return "";

/**
* Redondea un numero al inmediato superior
* @param valor numero a redondear
* @param decimales cantidad de decimales a mostrar
* @return double numero redondeado
*/
private double round(double valor, int decimales){
BigDecimal valueBD = new BigDecimal(valor);
valueBD = valueBD.setScale(decimales, BigDecimal.ROUND_HALF_UP);
return valueBD.doubleValue();
}

public static void main(String[] args){


Regula_Falsi regula_Falsi = new Regula_Falsi();
//String res = regula_Falsi.resolver(2.2, 6.5, 20, 0.0003);
String res = regula_Falsi.resolver(2.2, 2.5, 20, 0.0003);
System.out.println(res);
}
}
Ejecutamos y tenemos en consola:

run:
x1 = 2.2
x2 = 2.5
y1: fx(2.2)= -0.432
y2: fx(2.5)= 2.625
Iteración #1
x3 = (y2*x1-y1*x2)/(y2-y1)
x3 = ((2.625)*(2.2)-(-0.432)*(2.5))/((2.625)-(-0.432)) = 2.24239
Error |2.2/2.24239-1|<3.0E-4 false
y1 = -0.432
y3 = fx(x3) = fx(2.24239) = -0.05398
y1*y3 < 0 false
x1=x3=2.24239
y1=y3=-0.05398
Iteración #2
x3 = (y2*x1-y1*x2)/(y2-y1)
x3 = ((2.625)*(2.24239)-(-0.05398)*(2.5))/((2.625)-(-0.05398)) = 2.24758
Error |2.24239/2.24758-1|<3.0E-4 false
y1 = -0.05398
y3 = fx(x3) = fx(2.24758) = -0.00654
y1*y3 < 0 false
x1=x3=2.24758
y1=y3=-0.00654
Iteración #3
x3 = (y2*x1-y1*x2)/(y2-y1)
x3 = ((2.625)*(2.24758)-(-0.00654)*(2.5))/((2.625)-(-0.00654)) = 2.24821
Error |2.24758/2.24821-1|<3.0E-4 true
2.8E-4<3.0E-4 -> termina programa
Iteración:3 Valor x=2.2482118149584385

You might also like