首页 > 代码库 > Polynomial Curve Fitting
Polynomial Curve Fitting
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‘;endfor 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);endend
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;endpredVal = data*theta;end
Polynomial Curve Fitting
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。