fibonacc.cpp
来自「数学系学生的fibonacc算法作业,非线性优化的作业」· C++ 代码 · 共 59 行
CPP
59 行
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
void main()
{ double a ,b,l, u, v ,delt,f(double x);int n=0,k,fp(int i);
cout<<"please input a,b and l;"<<endl;
cin>>a>>b>>l;
while(fp(n)<(b-a)/l)
n++;
cout<<"n is"<<n<<endl;
cout<<" pleaes input delt"<<endl;
cin>>delt;
v=a+fp(n-2)*(b-a)/fp(n); //计算第一次的两个试探点//
u=a+fp(n-1)*(b-a)/fp(n);
for(k=1;b-a>=l;k++)
{ if(f(v)>f(u)) //书中的第三步//
{ a=v;
v=u;
u=a+fp(n-k-1)*(b-a)/fp(n-k);
cout<<"a=*"<<a<<" "<<"b="<<b<<endl;
}
else //书中第四步//
{ b=u;
u=v;
v=a+fp(n-k-2)*(b-a)/fp(n-k);
cout<<"a=//"<<a<<" "<<"b="<<b<<endl; }
if(k==n-2) //在k==n-2时,转向书中的第六步//
{ u=v+delt;
if(f(v)>f(u))
{ a=v; cout<<"a=^"<<a<<" "<<"b="<<b<<endl; }
if(f(v)<=f(u))
{b=v; cout<<"a=%"<<a<<" "<<"b="<<b<<endl; }
cout<<0.5*(a+b)<<"the solution is"<<f(0.5*(a+b))<<endl;
exit(0);
}
}
} double f(double x) //函数f(x)的声明及编制//
{ double y;
y=exp(-x)+x*x;
return(y);
}
int fp(int i) //fibonacci函数(递归函数)//
{ if (i==0||i==1)
return 1;
else
return fp(i-1)+fp(i-2);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?