⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hill.java

📁 数值算法,爬山算法源码,动态演示过程,JAVA源码
💻 JAVA
字号:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class hill
{
	/**
     * Creates a new instance of <code>Newton</code>.
     */
    public hill() {
    }
    
    /**
     * @param args the command line arguments
     */
   public static void main(String[] arg)
   {
      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 z,step=0.5,e=0.00001;
    
    double x=-3.0,y=-3.0;
    
    int hh=0,k=1;
    
    public MyPanel() 
    {
    	xpoint=new double[145];
    	ypoint=new double[145];
        setSize(500, 500);                   
        thread=new Thread(this); 
    }
    double f(double x,double y)
    {
    	return(1.0/(x*x+y*y+2));
    }
    
    double fx(double x,double y)
    {
    	return(-2.0*x/((x*x+y*y+2)*(x*x+y*y+2)));
    }
    
    double fy(double x,double y)
    {
    	return(-2.0*y/((x*x+y*y+2)*(x*x+y*y+2)));
    }
    
    double fxy2(double x,double y)
    {
    	return((4*(x*x+y*y))/((x*x+y*y+2)*(x*x+y*y+2)*(x*x+y*y+2)*(x*x+y*y+2)));
    }

    public void run()  
    {
    	z=f(x,y);
    	repaint();
    	xpoint[0]=x;
    	ypoint[0]=z;
    	try
    	{
    		thread.sleep(500);
    	}
    	catch(InterruptedException e)
    	{
    	}
    	while(fxy2(x,y)>e)
    	{
    	
    		x = x + step * fx(x, y);
			y = y + step * fy(x, y);
			z = f(x,y);
			xpoint[k]=x;
			ypoint[k]=z;
			k++;
			repaint();
			try
    	    {
    	    	thread.sleep(50);
    	    }
    	    catch(InterruptedException e)
    	    {
    	    }
    		
    	}
    	hh=1;
    	repaint();
    }

     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);   //x轴
         g.drawLine(200, 400, 200, 0);  //y轴
         
         g.translate(200,300);
         
         g.setColor(Color.blue);
         //g.drawLine((int)(x*60),-(int)(z*200),0,-(int)(z*200));
         g.fillOval((int)(x*60-2),-(int)(z*200+2),4,4);
         
         if(hh==1)
         {
         	for(int i=0;i<144;i++)
         	{
         		g.drawLine((int)(xpoint[i]*60),-(int)(ypoint[i]*200),(int)(xpoint[i+1]*60),-(int)(ypoint[i+1]*200));
         	}
         	g.setColor(Color.red);
         	g.drawLine(0,0,0,-200);
         	g.setColor(Color.black);
         	g.drawString("发现极值:x="+x,-180,-280);
            g.drawString("y="+y,-180,-260);
         	g.drawString("f(x,y)="+z,-180,-240);
         
         }
     }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -