机械优化设计进退法c语言程序,机械优化设计编程(C语言).pdf

反反复复二分法:

#include

#include"math.h"

#define E 0.01

void main()

{ float a,b,x,erfen(float a,float b);

printf("Enter a,b\n");

scanf("%f,%f",&a,&b);

x=erfen(a,b);

printf("x=%f\n",x);

}

float erfen(float a,float b)

{

float c,df(float x);

while (fabs(a-b)>E)

{

c=(a+b)/2;

if(df(c)==0) break;

else { if(df(a)*df(c)<0) b=c;

else a=c;

}

}

return c;

}

float df(float x)

{return 2*x-6;}

黄金分割法:

#include

#include"math.h"

#define E 0.001

#define H 0.618

void main()

{ float a,b,x;

float goldcut(float a,float b);

printf("Enter a,b\n");

scanf("%f,%f",&a,&b);

x=goldcut(a,b);

printf("x*=%f\n",x);

}

float goldcut(float a,float b)

{ float x1,x2,y1,y2;

float f(float x);

x2=a+H*(b-a);

x1=a+b-x2;

y1=f(x1);

y2=f(x2);

while(fabs(y1-y2)>=E)

{

if(y1>y2)

{ a=x1;

x1=x2;

y1=y2;

x2=a+H*(b-a);

y2=f(x2);

}

else { b=x2;

x2=x1;

y2=y1;

x1=a+(1-H)*(b-a);

y1=f(x1);

}

}

return (x1+x2)/2;

}

float f(float x)

{return x*x-6*x+2;}

进退法:

#include

#include"math.h"

void main()

{

float x0,h,x1,jintui(float x0, float h);

printf("Enter x0,h:\n");

scanf("%f,%f",&x0,&h);

x1=jintui(x0, h);

printf("(%f,%f)\n",x0,x1);

}

float jintui(float x0,float h)

{

float f(float x),x1,march(float x0,float h);

if(f(x0)>f(x0+h))

{ x1=march(x0,h);}

else{ h=-h;

x1=march(x0,h);

}

return x1;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值