用java写一个代码,满足下面给的条件:使用拉格朗日插值、分段线性插值、样条插值进行插值,绘制插值函数图,并根据结果确定最佳插值方法。
时间: 2024-05-08 08:21:52 浏览: 62
java实现拉格朗日插值法
4星 · 用户满意度95%
以下是使用Java编写的代码,实现了拉格朗日插值、分段线性插值、样条插值,并绘制了插值函数图。最后根据误差比较确定最佳插值方法。
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Interpolation extends JPanel {
private static final int WIDTH = 600;
private static final int HEIGHT = 400;
private static final int POINT_SIZE = 8;
private static final int INTERVALS = 20;
private List<Point> points;
private double[] x;
private double[] y;
private int[] xs;
private int[] ys;
public Interpolation() {
points = new ArrayList<Point>();
addMouseListener(new MouseHandler(this));
}
public void drawLagrange(Graphics g) {
double[] c = lagrangeCoefficients(x, y);
double step = (x[x.length - 1] - x[0]) / (INTERVALS - 1);
double xi = x[0];
xs = new int[INTERVALS];
ys = new int[INTERVALS];
for (int i = 0; i < INTERVALS; i++) {
xs[i] = (int) Math.round(xi);
ys[i] = (int) Math.round(evaluateLagrange(c, x, xi));
xi += step;
}
g.setColor(Color.RED);
drawCurve(g, xs, ys);
}
public void drawLinear(Graphics g) {
double step = (x[x.length - 1] - x[0]) / (INTERVALS - 1);
double xi = x[0];
xs = new int[INTERVALS];
ys = new int[INTERVALS];
for (int i = 0; i < INTERVALS; i++) {
xs[i] = (int) Math.round(xi);
ys[i] = (int) Math.round(evaluateLinear(x, y, xi));
xi += step;
}
g.setColor(Color.BLUE);
drawCurve(g, xs, ys);
}
public void drawSpline(Graphics g) {
List<double[]> coeffs = splineCoefficients(x, y);
double step = (x[x.length - 1] - x[0]) / (INTERVALS - 1);
double xi = x[0];
xs = new int[INTERVALS];
ys = new int[INTERVALS];
for (int i = 0; i < INTERVALS; i++) {
xs[i] = (int) Math.round(xi);
ys[i] = (int) Math.round(evaluateSpline(coeffs, x, xi));
xi += step;
}
g.setColor(Color.GREEN);
drawCurve(g, xs, ys);
}
private void drawCurve(Graphics g, int[] xs, int[] ys) {
for (int i = 0; i < xs.length - 1; i++) {
g.drawLine(xs[i], ys[i], xs[i + 1], ys[i + 1]);
}
}
public static double[] lagrangeCoefficients(double[] x, double[] y) {
int n = x.length;
double[] c = new double[n];
for (int j = 0; j < n; j++) {
double w = 1.0;
for (int k = 0; k < n; k++) {
if (k != j) {
w *= (x[j] - x[k]);
}
}
c[j] = y[j] / w;
}
return c;
}
public static double evaluateLagrange(double[] c, double[] x, double xi) {
double result = 0.0;
for (int j = 0; j < x.length; j++) {
double term = c[j];
for (int k = 0; k < x.length; k++) {
if (k != j) {
term *= (xi - x[k]) / (x[j] - x[k]);
}
}
result += term;
}
return result;
}
public static double evaluateLinear(double[] x, double[] y, double xi) {
int i = search(x, xi);
if (i == -1) {
return 0.0;
}
if (i == x.length - 1) {
return y[i];
}
double slope = (y[i + 1] - y[i]) / (x[i + 1] - x[i]);
return y[i] + slope * (xi - x[i]);
}
public static List<double[]> splineCoefficients(double[] x, double[] y) {
int n = x.length;
double[] h = new double[n - 1];
for (int i = 0; i < n - 1; i++) {
h[i] = x[i + 1] - x[i];
}
double[] alpha = new double[n - 1];
for (int i = 1; i < n - 1; i++) {
alpha[i] = 3.0 / h[i] * (y[i + 1] - y[i]) - 3.0 / h[i - 1] * (y[i] - y[i - 1]);
}
double[] l = new double[n];
double[] mu = new double[n];
double[] z = new double[n];
l[0] = 1.0;
mu[0] = z[0] = 0.0;
for (int i = 1; i < n - 1; i++) {
l[i] = 2.0 * (x[i + 1] - x[i - 1]) - h[i - 1] * mu[i - 1];
mu[i] = h[i] / l[i];
z[i] = (alpha[i] - h[i - 1] * z[i - 1]) / l[i];
}
l[n - 1] = 1.0;
z[n - 1] = 0.0;
double[] b = new double[n - 1];
double[] c = new double[n];
double[] d = new double[n - 1];
for (int j = n - 2; j >= 0; j--) {
c[j] = z[j] - mu[j] * c[j + 1];
b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2.0 * c[j]) / 3.0;
d[j] = (c[j + 1] - c[j]) / (3.0 * h[j]);
}
List<double[]> coeffs = new ArrayList<double[]>();
for (int i = 0; i < n - 1; i++) {
coeffs.add(new double[] { y[i], b[i], c[i], d[i] });
}
return coeffs;
}
public static double evaluateSpline(List<double[]> coeffs, double[] x, double xi) {
int i = search(x, xi);
if (i == -1) {
return 0.0;
}
if (i == x.length - 1) {
i--;
}
double[] c = coeffs.get(i);
double dx = xi - x[i];
return c[0] + c[1] * dx + c[2] * dx * dx + c[3] * dx * dx * dx;
}
public static int search(double[] x, double xi) {
int lo = 0;
int hi = x.length - 1;
if (xi < x[lo] || xi > x[hi]) {
return -1;
}
while (hi - lo > 1) {
int mid = (lo + hi) / 2;
if (xi < x[mid]) {
hi = mid;
} else {
lo = mid;
}
}
return lo;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(0, HEIGHT / 2, WIDTH, HEIGHT / 2);
g.drawLine(WIDTH / 2, 0, WIDTH / 2, HEIGHT);
if (points.size() < 2) {
return;
}
x = new double[points.size()];
y = new double[points.size()];
for (int i = 0; i < points.size(); i++) {
x[i] = points.get(i).x - WIDTH / 2;
y[i] = -points.get(i).y + HEIGHT / 2;
g.fillOval(points.get(i).x - POINT_SIZE / 2, points.get(i).y - POINT_SIZE / 2, POINT_SIZE, POINT_SIZE);
}
drawLagrange(g);
drawLinear(g);
drawSpline(g);
double lagrangeError = computeError(x, y, xs, ys, Interpolation::evaluateLagrange);
double linearError = computeError(x, y, xs, ys, Interpolation::evaluateLinear);
double splineError = computeError(x, y, xs, ys, Interpolation::evaluateSpline);
String bestMethod = "";
if (lagrangeError < linearError && lagrangeError < splineError) {
bestMethod = "Lagrange";
} else if (linearError < splineError) {
bestMethod = "Linear";
} else {
bestMethod = "Spline";
}
g.drawString("Lagrange error: " + lagrangeError, 10, HEIGHT - 30);
g.drawString("Linear error: " + linearError, 10, HEIGHT - 15);
g.drawString("Spline error: " + splineError, 10, HEIGHT);
g.drawString("Best method: " + bestMethod, WIDTH - 150, HEIGHT);
}
public static double computeError(double[] x, double[] y, int[] xs, int[] ys, Function f) {
double error = 0.0;
for (int i = 0; i < xs.length; i++) {
double yi = f.apply(x, y, xs[i]);
double delta = yi - ys[i];
error += delta * delta;
}
return Math.sqrt(error);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.add(new Interpolation());
frame.setVisible(true);
}
}
interface Function {
double apply(double[] x, double[] y, double xi);
}
class MouseHandler extends java.awt.event.MouseAdapter {
private Interpolation interpolation;
public MouseHandler(Interpolation interpolation) {
this.interpolation = interpolation;
}
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
interpolation.points.add(e.getPoint());
Collections.sort(interpolation.points, (p1, p2) -> Integer.compare(p1.x, p2.x));
interpolation.repaint();
}
}
```
运行程序后,可以点击界面上的点,选择插值点。程序会自动计算并绘制三种插值方法的插值函数图,并计算误差,最后确定最佳插值方法。
阅读全文