机械优化设计黄金分割法 外推法(6)
时间:2026-01-17
时间:2026-01-17
#include<math.h> #include<stdio.h> #include<stdlib.h> float f(float x,float y) {
float z;
z=(x-2)*(x-2)+(y-1)*(y-1); return z; }
float g1(float x,float y) {
float z; z=x*x-y; return z; }
float g2(float x,float y) {
float z; z=x+y-2; return z; }
void main() {
int i,j; float
k=8,c=0.000001,a0=-3,b0=3,a1=-3,b1=3; float
x[10],x0[10],xl[10],e[10],r[10],d[10],h,fl,f0,fx;
while(g1(x0[0],x0[1])>0||g2(x0[0],x0[1])>0) {
x0[0]=a0+(rand()/32767.00)*(b0-a0); x0[1]=a1+(rand()/32767.00)*(b1-a1); }
fl=f(x0[0],x0[1]); f0=f(x0[0],x0[1]); while(1) { h=0.01; j=1;
r[0]=-1+(rand()/32767.00)*(1-(-1)); r[1]=-1+(rand()/32767.00)*(1-(-1)); e[0]=r[0]/sqrt(r[0]*r[0]+r[1]*r[1]); e[1]=r[1]/sqrt(r[0]*r[0]+r[1]*r[1]); x[0]=x0[0]+h*e[0];
x[1]=x0[1]+h*e[1];
if(g1(x[0],x[1])<=0&&g2(x[0],x[1])<=0)
{ fx=f(x[0],x[1]); if(fx<fl) { fl=fx;
for(i=0;i<2;i++) {d[i]=e[i];xl[i]=x[i];} } }
while(j<=k) { j++;
r[0]=-1+(rand()/32767.00)*(1-(-1));
r[1]=-1+(rand()/32767.00)*(1-(-1));
e[0]=r[0]/sqrt(r[0]*r[0]+r[1]*r[1]);
e[1]=r[1]/sqrt(r[0]*r[0]+r[1]*r[1]); x[0]=x0[0]+h*e[0]; x[1]=x0[1]+h*e[1];
if(g1(x[0],x[1])<=0&&g2(x[0],x[1])<=0) { fx=f(x[0],x[1]); if(fx<fl) { fl=fx;
for(i=0;i<2;i++) {d[i]=e[i];xl[i]=x[i];} } } }
x[0]=xl[0]; x[1]=xl[1]; while(1) {
h=1.3*h;
x[0]=x[0]+h*d[0]; x[1]=x[1]+h*d[1];
if(g1(x[0],x[1])>0||g2(x[0],x[1])>0) break;
fx=f(x[0],x[1]); if(fx<fl) fl=fx; else break; } do
上一篇:平翘舌音练习材料大全
下一篇:电化学理论(李狄)课后答案解析