?? lagrange.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Lagrange {
/**
* Creates a new instance of <code>Lagrange</code>.
*/
public Lagrange() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new newFrame().init();
}
}
class newFrame extends JFrame
{
public void init()
{
MyPanel hello=new MyPanel();
getContentPane().add(hello);
hello.thread.start();
//以下代碼設置JFrame窗體的外觀
setSize(400,400);
setLocation(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
class MyPanel extends JPanel implements Runnable
{
Thread thread;
double xpoint[],ypoint[];
double x[]={0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0};
//double y[]={1,1.414214,1.732051,2,2.236068,2.449490,2.645751};
double y[]={0.0,5,20,45,80,125,180,245,320};
int N=4;
double t=1.0;
double s=0.0;
int num;
public MyPanel()
{
xpoint=new double[80];
ypoint=new double[80];
setSize(500, 500);
thread=new Thread(this);
////////////////////////以下計算拉格朗日插值
for(int i=0,k=0;i<80;i++)
{
xpoint[i]=k;
k++;
s=0.0;
for(int m=0;m<N;m++)
{
t=1.0;
for(int n=0;n<N;n++)
{
if(n!=m)
{
t=t*(xpoint[i]-x[n])/(x[m]-x[n]);
}
}
s=s+t*y[m];
}
ypoint[i]=s;
}
}
public void run()
{
for(num=2;num<79;num++) //顯示前num個點
{
repaint();
try{
thread.sleep(50);
}
catch(InterruptedException e)
{
}
}
}
public void paintComponent(Graphics g)
{
g.setColor(Color.white);
g.clearRect(0, 0, 400, 440);
g.setColor(Color.red);
g.drawLine(0, 300, 400, 300);
g.drawLine(200, 400, 200, 0);
g.translate(200,300);
g.setColor(Color.blue);
for(int i=0;i<num;i++)
{
g.drawLine((int)xpoint[i],-(int)(ypoint[i]),(int)xpoint[i+1],-(int)(ypoint[i+1]));
}
g.setColor(Color.black);
g.drawString("x[ ]={1.0,2.0,3.0,4.0,5.0,6.0,7.0}",-190,-280);
g.drawString("y[ ]={0.1,0.4,0.9,1.6,2.5,3.6,4.9}",-190,-260);
g.drawString("拉格朗日插值算法",-180,-200);
//g.drawString(""+xpoint[54]+" "+ypoint[54],-180,-50);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -