📄 牛顿插值.txt
字号:
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class chazhi
{
public static void main(String args[])
{
new CH().init();
}
}
class CH extends JFrame
{
public void init()
{
Container con=getContentPane();
con.add(new MyPanel());
setSize(1000,400);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements Runnable
{
double x[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
double y[]={1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400};
double a[]=new double[20];
double x1,y1;
int n=20;
double t,m;
double nx=0,ny=0;
public MyPanel()
{
new Thread(this).start();
}
public void run()
{
for(int j = 0; j < 20; j++)
{
a[j] = y[j];
}
for(int k = 1; k <20; k++)
{
for(int j = 19; j >= k; j--)
{
a[j] = (a[j] - a[j - 1])/(x[j] - x[j - k]);
}
}
for(nx = 0; nx < 300; nx += 0.1)
{
ny = a[19];
for(int ni = 18; ni >= 0; ni--)
{
ny = ny * (nx - x[ni]) + a[ni];
try
{
Thread.sleep(1);
}
catch(InterruptedException Ie2)
{
}
}
m=ny;
repaint();
}
}
public void paintComponent(Graphics g)
{
g.setColor(Color.red);
g.drawLine(100,0,100,400);
g.drawLine(0,200,200,200);
g.setColor(Color.green);
for (int i=0;i<n;i++)
{
g.fillOval((int)(x[i]*5+100),(int)((-y[i]+200)),3,3);
}
g.setColor(Color.black);
g.fillOval((int)(nx*5+100),(int)(-m+200),2,2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -