码迷,mamicode.com
首页 > 其他好文 > 详细

Polynomial Curve Fitting

时间:2014-12-20 19:35:23      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

pcf.m

inputSamp = (0:0.1:1);
result = sin(2*pi*inputSamp);
gsNoise = randn(11,1)*0.1;
observation = result + gsNoise;
x=(0:0.01:1);
y=sin(2*pi*x);
%{
figure;
plot(inputSamp, observation, *, x, y, --r.);
set(gca, XLim, [-0.2 1.2]);
set(gca, YLim, [-1.5 1.5]);
%}

%Y = w0+w1*x+w2*(x^2)+.....
theta = zeros(4,1);
theta = CalcParameter(inputSamp,observation, theta);
predVal = CalValue(x, theta);
figure;
plot(inputSamp, observation, *, x, y, --r., x, predVal, --g.);
set(gca, XLim, [-0.2 1.2]);
set(gca, YLim, [-1.5 1.5]);

CalcParameter.m

function theta = CalcParameter(inputSamp,observation, theta)
MaxIteration = 8000;
alpha = 1;
m = length(theta);
n = length(inputSamp);
data = ones(m, n);
data(1,:)=ones(1, n);
for i = 2:m
    data(i,:) = data(i-1,:).*inputSamp;
end

for i = 1:MaxIteration
    predVal = data*theta;
    error = predVal - observation;
    grad = data*error;
    theta = theta  - alpha*grad/n;
    er = mean(abs(error));
    fprintf(Iter %d: error : %f\n, i, er);
end

end

CalValue.m

function predVal = CalValue(x, theta)
m = length(theta);
n = length(x);
data = ones(n, m);
for i = 2:m
    data(:,i) = data(:,i-1).*x;
end
predVal = data*theta;
end

 

Polynomial Curve Fitting

标签:

原文地址:http://www.cnblogs.com/doublemystery/p/4175704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!