《对分法求解目标函数最优解函数MATLAB实现(共4页).docx》由会员分享,可在线阅读,更多相关《对分法求解目标函数最优解函数MATLAB实现(共4页).docx(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上%对分法求解目标函数最优解函数function t,y= equation_extremum (x,a,b,epsional)%=%函数默认参数:%x为目标函数,输入字符串型,默认最优解求其最小值%a,b为其初始区间,默认闭区间,ab%对初始区间的要求:初始区间是单谷区间,函数求极小值%则a处的导数=0;%=syms t;x_inline=inline(sym(x);func_diff=diff(sym(x);func_diff_inline=inline(func_diff);t=b;high_value=func_diff_inline(t);t=a;low_va
2、lue=func_diff_inline(t);if high_value*low_value0 t=本函数不可解; y=给定初始区间非单谷区间;else if high_value*low_value=0 if high_value=0 t=b; y=x_inline(t); else t=a; y=x_inline(t); end else while abs(high_value-low_value)=epsional; c=1/2*(b+a); t=c; middle_value=func_diff_inline(t); if middle_value0 high_value=midd
3、le_value; b=c; else if middle_value0 low_value=middle_value; a=c; else if middle_value=0 break; end end end end c=1/2*(b+a); t=c; y=x_inline(t); endend运行代码:%=close all;clear all;clc;%目标函数syms t;t,y=equation_extremum(t*(t-3),t,-3,5,0.1);disp(t,y);运行结果:t=1.5y=-2.25%抛物线法求函数最优解function t,y=parabola_extr
4、emum(x,t1,t2,epsional)%=%函数默认参数:%x为目标函数,输入字符串型,例如:x2-sin(x)默认最优解求其最小值%a,b为其初始区间,默认闭区间,t1t2%对初始区间的要求:初始区间是单谷区间,函数求极小值%=syms t;x_inline=inline(sym(x);%生成随机数求出t0;high_value=x_inline(t2);low_value=x_inline(t1);t0=parabola_overture(x,t,t1,t2);%自定义函数,试探法求t0middle_value=x_inline(t0);%拟合曲线,待定系数法%p(t0)=a0+a1
5、*t0+a2*t02=f(t0)%p(t1)=a0+a1*t1+a2*t22=f(t1)%p(t2)=a0+a1*t2+a2*t22=f(t2)%推出:%a1=(t02-t22)*f(t1)+(t22-t12)*f(t0)+(t12-t02)*f(t2)/(t1-t0)*(t0-t2)*(t2-t1)%a2=(t0-t2)*f(t1)+(t2-t1)*f(t0)+(t1-t0)*f(t2)/(t1-t0)*(t0-t2)*(t2-t1)while 1t_ba=0.5*(t02-t22)*low_value+(t22-t12)*middle_value+(t12-t02)*high_value)
6、/(t0-t2)*low_value+(t2-t1)*middle_value+(t1-t0)*high_value);t_ba_value=x_inline(t_ba);if abs(t_ba-t0)t0 if t_ba_value=middle_value t1=t0; t0=t_ba; low_value=middle_value; middle_value=t_ba_value; else t2=t_ba; high_value=t_ba_value; end else if t_ba_value=middle_value t2=t0; t0=t_ba; high_value=midd
7、le_value; middle_value=t_ba_value; else t1=t_ba; low_value=t_ba_value; end endenddisp(t1,t0,t2;low_value,middle_value,high_value)end%试探法求函数低点值,用于抛物线插值法function t0=parabola_overture(x,a,b)%=%函数默认参数:%x为目标函数,输入字符串型,例如:x2-sin(x)%a,b为其初始区间,默认闭区间,ahigh_value|middle_valuelow_value k=k+1; middle_value=x_inline(rand_t0(k); if k50 printf(请重新运行程序); break; endendt0=rand_t0(k);运行代码:close all;clear all;clc;syms t;x=t2-sin(t);x_inline=inline(sym(x);t,y=parabola_extremum(x,t,0,1,1e-4)运行结果:t = 0.5791y =-0.0718专心-专注-专业