📄 拉格朗日插值.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 x1,y1;
int n=20;
double t,m;
double px,py;
public MyPanel()
{
new Thread(this).start();
}
public void run()
{
y1=0.0;
for(px = 0; px < 300; px+=0.1)
{
double s = 0;
for(int k = 0; k < 20; k++)
{
double p = y[k];
for(int j = 0; j < 20; j++)
{
if(j != k)
{
p = p * (px - x[j]) / (x[k] - x[j]);
}
}
s += p;
try
{
Thread.sleep(1);
}
catch(InterruptedException Ie1)
{
}
}
py = s;
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)(px*5+100),(int)(-py+200),2,2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -