二分法与黄金分割法的比较.docx

上传人:b****6 文档编号:7515039 上传时间:2023-01-24 格式:DOCX 页数:12 大小:176.27KB
下载 相关 举报
二分法与黄金分割法的比较.docx_第1页
第1页 / 共12页
二分法与黄金分割法的比较.docx_第2页
第2页 / 共12页
二分法与黄金分割法的比较.docx_第3页
第3页 / 共12页
二分法与黄金分割法的比较.docx_第4页
第4页 / 共12页
二分法与黄金分割法的比较.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

二分法与黄金分割法的比较.docx

《二分法与黄金分割法的比较.docx》由会员分享,可在线阅读,更多相关《二分法与黄金分割法的比较.docx(12页珍藏版)》请在冰豆网上搜索。

二分法与黄金分割法的比较.docx

二分法与黄金分割法的比较

题目:

计算函数

的值并画出图像并分析各种算法的效率

一.二分法及黄金分割法计算函数的程序

1.二分法程序:

importjava.math.*;

importjava.util.Scanner;

publicclassValue1

{

publicstaticvoidmain(String[]args)

{

intcount=0 ;

doubley1=0,y2=0,y3=0,y4=0 ;

doublex1=0.1 ;

doublex2=1 ;

Doublex3=(x2-x1)*0.5+x1 ;//二分中点

doublejd=1 ;//精度

Fa=newF() ;//初始化函数

y1=a.Zhi(x1) ;

y2=a.Zhi(x2) ;

y3=a.Zhi(x3) ;

System.out.println(“ 请输入精度 “) ;

Scanners=newScanner(System.in) ;//获取输入的精度

jd=s.nextInt() ;

longstartTime=System.nanoTime() ;//获取程序开始时间

while(Math.abs(y3)>Math.pow(10,(-jd-1)))

{

if(y3>0)

{

x1=x3;

}

else

x2=x3;

x3=(x2-x1)*0.5+x1;//用二分中点法向值靠近

y3=a.Zhi(x3);

count++;

}

longendTime=System.nanoTime();//获得程序结束时间

longsum=endTime-startTime;

Stringx6=x3.toString();//截取精度

Stringx7=x6.substring(0,(int)(jd+2));

System.out.println(“x的值为:

“+x7);

System.out.println(“y的逼近值为:

“+y3);

System.out.println(“运行程序所用的时间为:

“+sum+”纳纱”);

System.out.println(“二分分割次数为:

“+count);

}

}

classF//计算F(X)的函数

{

doubleZhi(doublex)

{

doubley=0 ;

y=1/x*(1-1/(Math.pow(1+x,5)))-2 ;

returny;

}

}

程序运行结果:

精度为6时的结果

精度为8时的结果

 

2.黄金分割法程序:

importjava.math.*;

importjava.util.Scanner;

publicclassValue2

{

publicstaticvoidmain(String[]args)

{

intcount=0 ;

doubley1=0,y2=0,y3=0,y4=0 ;

doublex1=0.1 ;

doublex2=1 ;

Doublex3=(x2-x1)*0.618+x1 ;//黄金中点

doublejd=1 ;//精度

Fa=newF() ;//初始化函数X

y1=a.Zhi(x1) ;

y2=a.Zhi(x2) ;

y3=a.Zhi(x3) ;

System.out.println(« 请输入精度 ») ;

Scanners=newScanner(System.in) ;//获取输入的精度

jd=s.nextInt() ;

longstartTime=System.nanoTime() ;//获取程序开始时间

while(Math.abs(y3)>Math.pow(10,(-jd-1)))

{

if(y3>0)

{

x1=x3 ;

}

else

x2=x3 ;

x3=(x2-x1)*0.618+x1;//用黄金中点法向值靠近

y3=a.Zhi(x3);

count++;

}

longendTime=System.nanoTime();//获得程序结束时间

longsum=endTime-startTime;

Stringx6=x3.toString();//截取精度

Stringx7=x6.substring(0,(int)(jd+2));

System.out.println(“x的值为:

“+x7);

System.out.println(“y的逼近值为:

“+y3);

System.out.println(“运行程序所用的时间为:

“+sum+”纳纱”);

System.out.println(“黄金分割次数为:

“+count);

}

}

classF//计算F(X)的函数

{

doubleZhi(doublex)

{

doubley=0;

y=1/x*(1-1/(Math.pow(1+x,5)))-2 ;

returny;

}

}

程序运行结果

精度为6时的结果

精度为8时的结果

二.函数的图像及程序

1.图像程序代码

importjavax.swing.*;

importjava.awt.*;

importjava.lang.Math;

publicclassValue3extendsJFrame{

publicValue3(){

add(newX2FunctionPanel());

}

publicstaticvoidmain(String[]args){

Value3frame=newValue3();

frame.setSize(700,400);

frame.setTitle("绘制函数");

frame.setLocationRelativeTo(null);//center

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

classX2FunctionPanelextendsJPanel{

protectedvoidpaintComponent(Graphicsg){

super.paintComponent(g);

//画x轴

g.drawLine(20,150,getWidth()-20,150);

//x箭头

g.drawLine(getWidth()-30,140,getWidth()-20,150);

g.drawLine(getWidth()-30,160,getWidth()-20,150);

//“x”

g.drawString("X",getWidth()-10,150);

//画y轴

g.drawLine(200,getHeight()-20,200,20);

g.drawLine(190,30,200,20);

g.drawLine(210,30,200,20);

g.drawString("Y",220,30);

//画函数图像

Polygonp=newPolygon();

doublescaleFactor=0.02;

for(doublex=0;x<=10;x=x+0.01)

{

if(x==0)

;

else

p.addPoint((int)(x*500)+200,150-(int)(scaleFactor*1000*(1/x*(1-1/(Math.pow(1+x,5)))-2)));

}

g.drawPolyline(p.xpoints,p.ypoints,p.npoints);

}

/*

*publicDimensiongetPreferredSize()57.{58.returnnew

*Dimension(200,200);59.}

*/

}

2.函数图像

 

三.二分法与黄金分割法的效分析

1.二分法效率截图:

 

2.黄金分割法效率截图:

精度为1到13时的算法结果:

 

3.两种算法的分割次数比较

1

2

3

4

5

6

7

8

9

10

11

12

二分法

4

9

13

17

20

22

26

30

33

36

40

42

黄金法

5

10

13

16

19

22

25

31

33

36

40

40

二法法在12个精度的分割总次数为292

黄金分割法在12个精度的分割总次数为290

 

4.两种算法的数学分析:

1.:

二分法的算法最慢速度:

v1=log2 (n)

2.黄金分割算法最慢速度:

v2=log1/0.618(n)

黄金分割算法的最快速度:

v3=log1/0.382(n)

v1>v2v1

结论:

从v1>v2和v1

计算得log2(n)<=log1/0.618 (n),所以二分法的最慢速度在前面的时候较快,黄金分割法的所以二分法的稳定性更高

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1