matlab拟合非线性函数已知x,y.比如y=[y0 y1 y2 y3 y4 y5 y6 ] x=[x0 x1 x2 x3 x4 x5 x6],想求函数y=a0+a1cosx+a2cos2x+a3cos3x……ancosnx该怎么编写程序?忘说了,需要求出的是a0 a1 a2……an
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/17 04:26:16
matlab拟合非线性函数已知x,y.比如y=[y0 y1 y2 y3 y4 y5 y6 ] x=[x0 x1 x2 x3 x4 x5 x6],想求函数y=a0+a1cosx+a2cos2x+a3cos3x……ancosnx该怎么编写程序?忘说了,需要求出的是a0 a1 a2……an
matlab拟合非线性函数
已知x,y.比如y=[y0 y1 y2 y3 y4 y5 y6 ] x=[x0 x1 x2 x3 x4 x5 x6],想求函数y=a0+a1cosx+a2cos2x+a3cos3x……ancosnx该怎么编写程序?
忘说了,需要求出的是a0 a1 a2……an
matlab拟合非线性函数已知x,y.比如y=[y0 y1 y2 y3 y4 y5 y6 ] x=[x0 x1 x2 x3 x4 x5 x6],想求函数y=a0+a1cosx+a2cos2x+a3cos3x……ancosnx该怎么编写程序?忘说了,需要求出的是a0 a1 a2……an
线性模型、非线性模型是以待优化参数W为参考(而不是看X).
y = w' * x ('表示转置)描述的是线性模型.题目中要拟合的函数是:y = w0 + w1 cos a + ... + wn cosna,它同样符合y = w' * x,其中x = (1 cosa ... cosna)’.设x构成的集合是X,y构成的集合是Y,可依据最小二乘法求出W = (XX')^-1 *X*Y.
所以程序是:
n = 3; \x09 % 拟合到cos 3x
x = linspace(0,2*pi,100); \x09 % 数据集
y = 1+2*cos(x)-3*cos(3*x) + rand(1,length(x)); % 加噪声
X = cos((0:n)'*x);
Y = y';
W = (X*X')^-1*X*Y; %
plot(x,y,'o',x,W'*cos((0:n)'*x)); % 画图查看
PS:MATLAB问题提问请设置问题分类为编程.
% By lyqmath
clc; clear all; close all;
x = 1979:2000;
y = [4.69,24.54,24.11,10.62,12.86,0.74,-7.29,9.52,-7.11,3.25,23.09,5.26,-0.26,23.99,25.55,0.45,14.54,-5.86,18.07,2.73,27.49,23.54];
全部展开
% By lyqmath
clc; clear all; close all;
x = 1979:2000;
y = [4.69,24.54,24.11,10.62,12.86,0.74,-7.29,9.52,-7.11,3.25,23.09,5.26,-0.26,23.99,25.55,0.45,14.54,-5.86,18.07,2.73,27.49,23.54];
xt = linspace(min(x), max(x));
yt = spline(x, y, xt);
figure; hold on; box on;
plot(x, y, 'r+')
plot(xt, yt, 'r-')
收起