数值分析 最小二乘 matlab

时间:2022-05-13 15:03:48

1. 已知函数在下列各点的值为

 

-1

-0.75

-0.5

0

0.25

0.5

0.75

 

1.00

0.8125

0.75

1.00

1.3125

1.75

2.3125

分别用一次、二次、三次最小二乘拟合多项式拟合上述数据,画出所给数据和所求最小二乘拟合多项式的图像。

程序:

function f=multifit(x,y,wfunc,n)

syms t

%x,y为给定数据数组,wfunc为权函数,n为要求拟合多项式的次数

N=length(x);

M=length(y);

if(N ~= M)

disp('x与y维数不匹配');

return;

end

var = findsym(sym(wfunc));

w = subs(wfunc,'var',x);

g(1:(2*n+1))=0;

b(1:(n+1))=0;

for j=1:(2*n+1)

for k=1:N

g(j)=g(j)+w(j)*x(k)^(j-1);

if(j<(n+2))

b(j)=b(j)+w(j)*y(k)*x(k)^(j-1);

end

end

end

G(1,:)=g(1:(n+1));

for i=2:(n+1)

G(i,:)=g(i:(n+i));

end

coff=b'\G;

f = coff(1);

l = 1;

for i=1:n

l = l*t;

f = f+coff(i+1)*l;

end

一维:

x=[-1   -0.75   -0.5    0   0.25    0.5     0.75];

y=[1.00 0.8125  0.75    1.00    1.3125  1.75    2.3125];

plot(x,y)

hold on

w=ones(1,7);

syms t

f=multifit(x,y,w,1)

t=-1:0.02:1;

tf=subs(f,'t',t);

plot(t,tf)

f =

1723890370956185/2251799813685248 - (923516587282913*t)/18014398509481984

二维:

f =

(2832971806309577*t^2)/9007199254740992 - (5864710038001133*t)/72057594037927936 + 6907316203181555/9007199254740992

三维:

f =

- (8450352688460543*t^3)/72057594037927936 + (89154935708507*t^2)/281474976710656 - (6164715222057551*t)/72057594037927936 + 6924830714825963/9007199254740992

2. 已知一组实验数据如下,

 

2

3

6

9

10

 

-0.5

1.2

3.1

4.5

7.3

 

0.125

0.125

0.25

0.125

0.375

求拟合上述数据的二次最小二乘拟合多项式。

主程序:

x=[2    3   6   9   10];

y=[-0.5 1.2 3.1 4.5 7.3];

w=[0.125    0.125   0.25    0.125   0.375];

plot(x,y)

hold on

syms t

f=multifit(x,y,w,2)

t=2:0.1:10;

tf=subs(f,'t',t);

plot(t,tf)

legend('已知点','拟合')

图像:

f =

(8318828653518565*t^2)/562949953421312 + (7344269211390441*t)/4503599627370496 + 6836208550152757/36028797018963968

3. 设,分别求次数为2,3,6,8的多项式,使得

达到最小,并画出和的曲线进行比较。

程序:

function f = Legendre(func,n)

%求函数在[-1,1]的关于权函数为1的n次最佳平方逼近多项式f,并计算插值多项式f在数据点x0的函数值f0

syms t;

P(1:n+1) = t;

P(1) = 1;

P(2) = t;

c(1:n+1) = 0.0;

c(1)=int(subs(func,findsym(sym(func)),sym('t'))*P(1),t,0,1)/2;

c(2)=3*int(subs(func,findsym(sym(func)),sym('t'))*P(2),t,0,1)/2;

f = c(1)+c(2)*t;

for i=3:n+1

P(i) = ((2*i-3)*P(i-1)*t-(i-2)*P(i-2))/(i-1);

c(i) = (2*i-1)*int(subs(func,findsym(sym(func)),t)*P(i),t,0,1)/2;

f = f + c(i)*P(i);

if(i==n+1)

f = vpa(f,6);

end

end

n=2:

syms x

fun=sin(pi*x);

t=0:0.1:1;

tf=subs(fun,'x',t);

plot(t,tf)

hold on

f = Legendre(fun,2)

f2=subs(f,'t',t);

plot(t,f2)

f =

- 0.128828*t^2 + 0.477465*t + 0.361253

N=3:

f =

0.863545*t - 0.19304*t*(7.5*t^2 - 2.5) - 0.128828*t^2 + 0.361253

N=6:

f =

0.922023*t - 0.222279*t*(7.5*t^2 - 2.5) - 0.021929*t*(2.25*t*(4.66667*t - 2.33333*t*(7.5*t^2 - 2.5)) + 10.125*t^2 - 3.375) - 0.0383952*t*(2.93333*t*(7.5*t^2 - 2.5) - 5.86667*t + 2.2*t*(2.25*t*(4.66667*t - 2.33333*t*(7.5*t^2 - 2.5)) + 10.125*t^2 - 3.375)) + 0.144211*t*(4.66667*t - 2.33333*t*(7.5*t^2 - 2.5)) + 0.52012*t^2 + 0.144936

N=8:

f =

0.925827*t - 0.224181*t*(7.5*t^2 - 2.5) - 0.0233554*t*(2.25*t*(4.66667*t - 2.33333*t*(7.5*t^2 - 2.5)) + 10.125*t^2 - 3.375) - 0.0568912*t*(2.93333*t*(7.5*t^2 - 2.5) - 5.86667*t + 2.2*t*(2.25*t*(4.66667*t - 2.33333*t*(7.5*t^2 - 2.5)) + 10.125*t^2 - 3.375)) + 0.0158537*t*(6.85714*t - 3.42857*t*(7.5*t^2 - 2.5) - 2.57143*t*(2.25*t*(4.66667*t - 2.33333*t*(7.5*t^2 - 2.5)) + 10.125*t^2 - 3.375) + 2.14286*t*(2.16667*t*(2.93333*t*(7.5*t^2 - 2.5) - 5.86667*t + 2.2*t*(2.25*t*(4.66667*t - 2.33333*t*(7.5*t^2 - 2.5)) + 10.125*t^2 - 3.375)) - 2.70833*t*(4.66667*t - 2.33333*t*(7.5*t^2 - 2.5)) - 12.1875*t^2 + 4.0625)) + 0.167331*t*(4.66667*t - 2.33333*t*(7.5*t^2 - 2.5)) + 0.62416*t^2 + 0.00118873*t*(2.16667*t*(2.93333*t*(7.5*t^2 - 2.5) - 5.86667*t + 2.2*t*(2.25*t*(4.66667*t - 2.33333*t*(7.5*t^2 - 2.5)) + 10.125*t^2 - 3.375)) - 2.70833*t*(4.66667*t - 2.33333*t*(7.5*t^2 - 2.5)) - 12.1875*t^2 + 4.0625) + 0.110256