最速下降法的c语言编程,急求大神

最速下降法的c语言编程,急求大神,第1张

// this program made by lusiyuan

#include<stdio.h>

#include<math.h>

#define N 10

#define eps pow(10,-6)

double f(double x[],double g[],double t)

{

double s

s=pow(x[0]-t*g[0],2)+4*pow(x[1]-t*g[1],2)

return s

}

void sb(double *a,double *b,double x[],double g[])

{

double t0,t1,t,h,alpha,f0,f1

int k=0

t0=10 /*初始值*/

h=1 /*初始步长*/

alpha=2 /*加步系数*/

f0=f(x,g,t0)

t1=t0+h

f1=f(x,g,t1)

while(1)

{

if(f1<f0)

{

h=alpha*ht=t0

t0=t1 f0=f1

k++

}

else

{

if(k==0)

{h=-ht=t1}

else

{

*a=t<t1?t:t1

*b=t>t1?t:t1

break

}

}

t1=t0+h

f1=f(x,g,t1)

}

}

double hjfg(double x[],double g[])

{

double beta,t1,t2,t

double f1,f2

double a=0,b=0

double *c,*d

c=&a,d=&b

sb(c,d,x,g)

printf("\n[a,b]=[%lf,%lf]",a,b)

beta=(sqrt(5)-1.0)/2

t2=a+beta*(b-a)f2=f(x,g,t2)

t1=a+b-t2 f1=f(x,g,t1)

while(1)

{

if(fabs(t1-t2)<eps)

break

else

{

if(f1<f2)

{

t=(t1+t2)/2b=t2

t2=t1 f2=f1

t1=a+b-t2 f1=f(x,g,t1)

}

else

{

a=t1 t1=t2

f1=f2 t2=a+beta*(b-a)

f2=f(x,g,t2)

}

}

}

t=(t1+t2)/2

return t

}

void zsxjf()

{

double x[N],g[N],t=0,f0,mod

int i,n

printf("请输入n(为几元函数)=")

scanf("%d",&n)

printf("\n请输入初始值:\n")

for(i=0i<ni++)

scanf("%lf",&x[i])

f0=f(x,g,t)

g[0]=2*x[0]g[1]=8*x[1]

t=hjfg(x,g)

printf("\nt=%lf",t)

while(1)

{

mod=sqrt(pow(g[0],2)+pow(g[1],2))

printf("\nmod=%lf",mod)

if(mod<eps)

break

else

{

x[0]=x[0]-t*g[0]

x[1]=x[1]-t*g[1]

f0=f(x,g,t)

g[0]=2*x[0]

g[1]=8*x[1]

t=hjfg(x,g)

}

printf("\n-----------------------------------------------")

printf("\nt=%lf",t)

}

printf("\n最优解为:x%d=%lf,x%d=%lf",1,x[0],2,x[1])

printf("\n函数最有值为:f=%lf",f(x,g,t))

}

int main()

{

zsxjf()

}

编译通过,这种代码很好写的。

你得给一个初值,初值不合适的话就解不出来。

程序如下,修改x0就是修改初值了。运行会有个警告,表示迭代次数达到最大。

% 程序开始 test_fsolve.m

function test_fsolve

x0 = [1111]*1e-3

xx = fsolve(@eqns, x0)

function y = eqns(x)

[a,b,c,d]=deal(x(1),x(2),x(3),x(4))

y = zeros(4,1)

y(1) = 77.6*(1/a+(1-exp(-0.5*b/d))/b+0.5/c)-8

y(2) = 77.6*(1/a+(1-exp(-b/d))/b+1/c)-9

y(3) = 77.6*(1/a+(1-exp(-1.75*b/d))/b+1.75/c)-10

y(4) = 77.6*(1/a+(1-exp(-4.25*b/d))/b+4.25/c)-11

% 程序结束 test_fsolve.m

例1 求 f = 2 在0<x<8中的最小值与最大值

主程序为wliti1.m:

f='2*exp(-x).*sin(x)'

fplot(f,[0,8])%作图语句

[xmin,ymin]=fminbnd (f, 0,8)

f1='-2*exp(-x).*sin(x)'

[xmax,ymax]=fminbnd (f1, 0,8)

运行结果:

xmin = 3.9270ymin = -0.0279

xmax = 0.7854 ymax = 0.6448

★(借助课件说明过程、作函数的图形)

例2 对边长为3米的正方形铁板,在四个角剪去相等的正方形以制成方形无盖水槽,问如何剪法使水槽的容积最大?

设剪去的正方形的边长为x,则水槽的容积为: ,建立无约束优化模型为:min y=- , 0<x<1.5

先编写M文件fun0.m如下:

function f=fun0(x)

f=-(3-2*x).^2*x

主程序为wliti2.m:

[x,fval]=fminbnd('fun0',0,1.5)

xmax=x

fmax=-fval

运算结果为: xmax = 0.5000,fmax =2.0000.即剪掉的正方形的边长为0.5米时水槽的容积最大,最大容积为2立方米.

★(借助课件说明过程、作函数的图形、并编制计算程序)

例3

1、编写M-文件 fun1.m:

function f = fun1 (x)

f = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1)

2、输入M文件wliti3.m如下:

x0 = [-1, 1]

x=fminunc(‘fun1’,x0)

y=fun1(x)

3、运行结果:

x= 0.5000 -1.0000

y = 1.3029e-10

★(借助课件说明过程、作函数的图形并编制计算程序)

例4 Rosenbrock 函数 f(x1,x2)=100(x2-x12)2+(1-x1)2 的最优解(极小)为x*=(1,1),极小值为f*=0.试用不同算法(搜索方向和步长搜索)求数值最优解.初值选为x0=(-1.2 , 2).

为获得直观认识,先画出Rosenbrock 函数的三维图形, 输入以下命令:

[x,y]=meshgrid(-2:0.1:2,-1:0.1:3)

z=100*(y-x.^2).^2+(1-x).^2

mesh(x,y,z)

画出Rosenbrock 函数的等高线图,输入命令:

contour(x,y,z,20)

hold on

plot(-1.2,2,' o ')

text(-1.2,2,'start point')

plot(1,1,'o')

text(1,1,'solution')

f='100*(x(2)-x(1)^2)^2+(1-x(1))^2'

[x,fval,exitflag,output]=fminsearch(f, [-1.2 2])

运行结果:

x =1.00001.0000

fval =1.9151e-010

exitflag = 1

output =

iterations: 108

funcCount: 202

algorithm: 'Nelder-Mead simplex direct search'

★(借助课件说明过程、作函数的图形并编制计算程序)

(五)、 作业

陈酒出售的最佳时机问题

某酒厂有批新酿的好酒,如果现在就出售,可得总收入R0=50万元(人民币),如果窖藏起来待来日(第n年)按陈酒价格出售,第n年末可得总收入 (万元),而银行利率为r=0.05,试分析这批好酒窖藏多少年后出售可使总收入的现值最大. (假设现有资金X万元,将其存入银行,到第n年时增值为R(n)万元,则称X为R(n)的现值.)并填下表:


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/yw/11112652.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-13
下一篇2023-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存