最近遇到了线性插值和夹逼准则相关的问题。在网上找了一些东西,整理到这里~
一、线性插值
pom文件
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
调用函数
LinearInterpolator line = new LinearInterpolator(); // 创建线性插值的对象
double[] x = new double[2]; // 创建x轴两个点的数组
double[] y = new double[2]; // 创建y轴两个点的数组
// 将两个点的x,y添加到数组中
x[0] = x0
x[1] = x1
y[0] = y0
y[1] = y1
line.interpolate(x, y).value(x2); // 调用线性插值函数,并预测x2点的y值
二、夹逼准则
java用二分法求方程2x^3 – 4x^2 +3x -6 =0 在(-10,10)之间的根
public class Dichotomie {
public static void main(String[] args)
{
double a = -10, b = 10, delta = 1E-15;
double x1 = a;
double x2 = b;
while(x2 - x1 > delta)
{
double diff = x2 - x1;
if (isArea(x1, x2))
x1 = x1 + diff / 2;
else
{
x2 = x1;
x1 = x1 - diff;
}
}
System.out.println("Solution: " + x1);
}
private static boolean isArea(double x1, double x2)
{
boolean result = true;
if(f(x1) * f(x2) > 0)
result = false;
else if(f(x1) * f(x2) < 0)
result = true;
return result;
}
private static double f(double x)
{
return 2*x*x*x - 4*x*x +3*x -6;
}
}