二元插值
代码:
clear; clc; f1202=inline(‘(x.x - 2).^2/2-1’, ‘x’);; %%%设定区间端点 a = 0; b = 5; %%%分别设定x阈值和函数f阈值 TolX = 1e-5; TolFun = 1e-8; %%%设定迭代次数 MaxIter = 100; %%%用二次插值函数求解 [xoq,foq] = Opt_Quadratic(f1202,[a b],TolX,TolFun,MaxIter) %用matlab内置函数求最小值以及它的函数值 [xob,fob] = fminbnd(f1202,a,b) %MATLAB 内置函数求解 function [xo,fo] = Opt_Quadratic(f,x,TolX,TolFun,MaxIter) % 用二次插值求f(x)最优解 if nargin < 5 MaxIter =100; end if nargin < 4 TolFun= 1e-8; end if nargin < 3 TolX = 1e-5; end %%%根据输入确定三点的初始值,并求出对应的函数值 if length(x) > 2 x012 = x(1:3); else if length(x) == 2 a = x(1); b = x(2); else a = x-10; b = x+10; end x012 = [a (a + b)/2 b]; end f012 = f(x012); %%%%初始化三点值 x0 = x012(1); x1 = x012(2); x2 = x012(3); f0 = f012(1); f1 = f012(2); f2 = f012(3); %%%%%求出插值二项式的取最小值处的x3以及相应的最小值f3 nd = [f0-f2 f1-f0 f2-f1][x1x1 x2x2 x0*x0; x1 x2 x0]’; x3 = nd(1)/2/nd(2); f3 = feval(f,x3); %%%%判断是否停止迭代 if MaxIter<= 0 | abs(x3 - x1) < TolX | abs(f3 - f1) < TolFun xo = x3; fo = f3; else %%%%%根据求出的多项式值与区间中三点值的大小比较确定相应的新的三点 if x3 < x1 if f3 < f1 x012 = [x0 x3 x1]; f012 = [f0 f3 f1]; else x012 = [x3 x1 x2]; f012 = [f3 f1 f2]; end else if f3 <= f1 x012 = [x1 x3 x2]; f012 = [f1 f3 f2]; else x012 = [x0 x1 x3]; f012 = [f0 f1 f3]; end end %%%%%进行新一轮迭代 [xo,fo] = Opt_Quadratic(f,x012,TolX,TolFun,MaxIter-1); end end
结果
最小二乘法曲线拟合
代码:
%by hanlestudy@163.com clear clc x=[2,4,5,6,6.8,7.5,9,12,13.3,15]; y=[-10,-6.9,-4.2,-2,0,2.1,3,5.2,6.4,4.5]; [~,k]=size(x); for n=1:9 X0=zeros(n+1,k); for k0=1:k %构造矩阵X0 for n0=1:n+1 X0(n0,k0)=x(k0)^(n+1-n0); end end X=X0’; ANSS=(X’*X)\X’*y’; for i=1:n+1 %answer矩阵存储每次求得的方程系数,按列存储 answer(i,n)=ANSS(i); end x0=0:0.01:17; y0=ANSS(1)*x0.^n ;%根据求得的系数初始化并构造多项式方程 for num=2:1:n+1 y0=y0+ANSS(num)x0.^(n+1-num); end subplot(3,3,n) plot(x,y,'') hold on plot(x0,y0) end
结果