function [ ] = main1_func( )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
global longth;% 步长
longth=input('请输入步长');
global begginning;%初点
begginning=input('请输入初始点');
global precision;%精度
precision=input('请输入精度');
global way;
global qujian1;
global qujian2;%计算区间
sub_rearchfield;
disp('请从数字1——6中选择输入一个数字作为取极小值小值方法');
disp('1-黄金分割法');
disp('2-平分法');
disp('3-成功失败法');
disp('4-牛顿法');
disp('5-三点二次插值');
disp('6-三次插值法');
way=input('请输入方法所对应的数字');
tic;
if way==1
disp('黄金分割法结果如下');
sub_huangjin;
end
if way==2
disp('平分法结果如下');
sub_pingfen;
end
if way==3
disp('成功失败法法结果如下');
sub_sandf;
end
if way ==4
disp('牛顿法结果如下');
sub_niudun;
end
if way==5
disp('三点二次插值法结果如下');
sub_sandianerci;
end
if way==6
disp('三次插值法结果如下');
sub_sancichazhi;
end
toc;
end
function y = sub_equation(a)
%UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
syms x;
equ(x)=x*x+4*x-7;
y=equ(a);
y=double(y);
end
function y=sub_equation1(a)
syms x;
equ(x)=x*x+4*x-7;
equ1(x)=diff(equ(x));
y=equ1(a);
y=double(y);
end
function y=sub_equation2(a)
syms x;
equ(x)=x*x+4*x-7;
equ1(x)=diff(equ(x));
equ2(x)=diff(equ1(x));
y=equ2(a);
y=double(y);
end
function [ ] = sub_rearchfield( )
global longth;% 步长
global begginning;%初点
global qujian1;
global qujian2;%计算区间
global lastlongth;
x1=begginning;
x2=x1+longth;
y1=sub_equation(x1);
y2=sub_equation(x2);
if y1>y2
x3=x2+longth;
y3=sub_equation(x3);
if y2<y3
qujian1=x1;
qujian2=x3;
end
while y2>y3
longth=2*longth;
x1=x2;x2=x3;
x3=x2+longth;
y3=sub_equation(x3);
y2=sub_equation(x2);
end
qujian1=x1;
qujian2=x3;
end
if y1<=y2
longth=-longth;
t=x1;w=y1;
x1=x2;y1=y2;
x2=t;y2=w;
x3=x2+longth;
y3=sub_equation(x3);
while y2>y3
longth=2*longth;
x1=x2;x2=x3;
x3=x2+longth;
y3=sub_equation(x3);
y2=sub_equation(x2);
end
if y2<y3
qujian1=x1;
qujian2=x3;
end
end
lastlongth=longth;
end
function [ ] = sub_huangjin( )
global longth;% 步长
global begginning;%初点
global precision;%精度
global qujian1;
global qujian2;%计算区间
global x;
global y;
number=0;%迭代次数
if qujian1>qujian2
a=qujian2;
b=qujian1;
end
if qujian1<qujian2
a=qujian1;
b=qujian2;
end
x1 = b - 0.618*(b - a);
x2 = a + 0.618*(b - a);
f1=sub_equation(x1);
f2=sub_equation(x2);
c=abs(a-b);
while c>precision;
number=number+1;
if f1<=f2
b = x2;
x2 = x1;
f2 = f1;
x1 = b - 0.618*(b - a);
f1=sub_equation(x1);
end
if f1>f2
a = x1;
x1 = x2;
f1 = f2;
x2 = a + 0.618*(b - a);
f2= sub_equation(x2);
end
c = abs(a - b);
end
jieguo = (a+b) / 2;
shuzi = sub_equation(jieguo);
disp('程序取极小值点为');
disp(jieguo);
disp('程序在极小值点取到极小值为');
disp(shuzi);
disp('程序迭代次数为');
disp(number);
disp('程序运行时间为');
end%黄金分割法
function [ ]=sub_pingfen( )%平分法
global longth;% 步长
global begginning;%初点
global precision;%精度
global qujian1;
global qujian2;%计算区间
global x;
global y;
global y1;
number=0;%迭代次数
if qujian1>qujian2
a=qujian2;
b=qujian1;
end
if qujian1<qujian2
a=qujian1;
b=qujian2;
end
c=abs(a-b);
while c>precision;
d=(a+b)/2;
e=sub_equation1(d);
disp(a);
disp(b);
disp(e);
if e>0
number=number+1;
b=d;
c=abs(a-b);
end
if e<=0
a=d;
c=abs(a-b);
number=number+1;
end
end
jieguo = (a+b) / 2;
shuzi = sub_equation(jieguo);
disp('程序取极小值点为');
disp(jieguo);
disp('程序在极小值点取到极小值为');
disp(shuzi);
disp('程序迭代次数为');
disp(number);
disp('程序运行时间为');
end
function [ ]=sub_sandf()%成功失败法
global longth;% 步长
global begginning;%初点
global precision;%精度
global qujian1;
global qujian2;%计算区间
global x;
global y;
global y1;
number=0;%迭代次数
if qujian1>qujian2
a=qujian2;
b=qujian1;
end
if qujian1<qujian2
a=qujian1;
b=qujian2;
end
x0=begginning;
fail=0;
wocao=0;
while wocao==0
x1=x0+longth;
number=number+1;
f0=sub_equation(x0);
f1=sub_equation(x1);
if f1<f0
if fail==0
longth=2*longth;
x0=x1;
end
if fail==1
longth=longth;
x0=x1;
end
end
if f1>f0
fail=1;
if abs(longth)<precision
wocao=1;
jieguo=x0;
end
if abs(longth)>=precision
longth=-0.25*longth;
x0=x1;
end
end
if f1==f0
wocao=1;
jieguo=(x0+x1)/2;
end
end
shuzi = sub_equation(jieguo);
disp('程序取极小值点为');
disp(jieguo);
disp('程序在极小值点取到极小值为');
disp(shuzi);
disp('程序迭代次数为');
disp(number);
disp('程序运行时间为');
end
function [ ]=sub_niudun()%牛顿法
global longth;% 步长
global begginning;%初点
global precision;%精度
global qujian1;
global qujian2;%计算区间
global x;
global y;
number=0;%迭代次数
if qujian1>qujian2
a=qujian2;
b=qujian1;
end
if qujian1<qujian2
a=qujian1;
b=qujian2;
end
wocao=0;
a1=b;
while wocao==0
a2=a1-sub_equation1(a1)/sub_equation2(a1);
w=abs(a2-a1);
number=number+1;
if w<=precision
wocao=1;
jieguo=a2;
end
if w>precision
a1=a2;
end
end
shuzi = sub_equation(jieguo);
disp('程序取极小值点为');
disp(jieguo);
disp('程序在极小值点取到极小值为');
disp(shuzi);
disp('程序迭代次数为');
disp(number);
disp('程序运行时间为');
end
function [ ]=sub_sandianerci()%三点二次插值
global longth;% 步长
global begginning;%初点
global precision;%精度
global qujian1;
global qujian2;%计算区间
global x;
global y;
global lastlongth;
number=0;%迭代次数
if qujian1>qujian2
a=qujian2;
b=qujian1;
end
if qujian1<qujian2
a=qujian1;
b=qujian2;
end
a1=a;
a3=b;
a2=(a1+a3)/2;
%if sub_equation(a1)>=sub_equation(a3)
% a2=a3-0.1;
%end
%if sub_equation(a1)<sub_equation(a3)
% a2=a1+0.1;
%end
y1=sub_equation(a1);
y2=sub_equation(a2);
y3=sub_equation(a3);
c1=(y3-y1)/(a3-a1);
c2=[(y2-y1)/(a2-a1)-c1]/(a2-a3);
ap=0.5*(a1+a3-c1/c2);
yp=sub_equation(ap);
wocao=abs((y2-yp)/y2);
disp(wocao);
while wocao>=precision
number=number+1;
wo=(ap-a2)*lastlongth;
if wo>0
if y2>=yp
a1=a2;
y1=y2;
a2=ap;
y2=yp;
end
if y2<yp
a3=ap;
y3=yp;
end
end
if wo<=0
if y2>=yp
a3=a2;
y3=y2;
a2=ap;
y2=yp;
end
if y2<yp
a1=ap;
y1=yp;
end
end
c1=(y3-y1)/(a3-a1);
c2=[(y2-y1)/(a2-a1)-c1]/(a2-a3);
ap=0.5*(a1+a3-c1/c2);
yp=sub_equation(ap);
wocao=abs((y2-yp)/y2);
end
if y2<yp
jieguo=a2;
shuzi=y2;
end
if y2>=yp
jieguo=ap;
shuzi=yp;
end
disp('程序取极小值点为');
disp(jieguo);
disp('程序在极小值点取到极小值为');
disp(shuzi);
disp('程序迭代次数为');
disp(number);
disp('程序运行时间为');
end
function [ ]=sub_sancichazhi()%三次插值法
global longth;% 步长
global begginning;%初点
global precision;%精度
global qujian1;
global qujian2;%计算区间
global x;
global y;
number=0;%迭代次数
if qujian1>qujian2
a=qujian2;
b=qujian1;
end
if qujian1<qujian2
a=qujian1;
b=qujian2;
end
u=sub_equation1(b);
v=sub_equation1(a);
fa=sub_equation(a);
fb=sub_e