标签:
Generate a synthetic dataset from a quadratic function
N = 200; %Number of data points
% Generate random x values between -5 and 5
x = 10*(sort(rand(N,1))-0.5);
Define the function and the true parameters
w_0 = 1;
w_1 = -2;
w_2 = 0.5;
Define t and add some noise
t = w_0 + w_1*x + w_2*(x.^2);
t = t + 0.5*randn(N,1);
Plot the data
figure(1);
hold off
plot(x,t,‘k.‘,‘markersize‘,10);
xlabel(‘x‘);
ylabel(‘t‘);
Fit the quadratic model and a linear model for comparison
Using vector matrix notation
X = [];
for k = 0:2
X = [X x.^k];
if k == 1
linear_w = inv(X‘*X)*X‘*t;
end
if k == 2
quad_w = inv(X‘*X)*X‘*t;
end
end
fprintf(‘\n Linear function: t = %g + %g x‘,linear_w(1),linear_w(2));
fprintf(‘\n Quadratic function: t = %g + %g x + %g x^2‘,...
quad_w(1),quad_w(2),quad_w(3));
output
Linear function: t = 5.26187 + -1.98859 x
Quadratic function: t = 0.949436 + -2.00242 x + 0.50068 x^2
Plot the functions
plotx = [-5:0.01:5]‘;
plotX = [];
for k = 0:2
plotX = [plotX plotx.^k];
end
figure(1);hold off;
% Add the data again
plot(x,t,‘k.‘,‘markersize‘,10);
xlabel(‘x‘);
ylabel(‘t‘);
hold on
plot(plotx,plotX*quad_w,‘r‘,‘linewidth‘,2);
plot(plotx,plotX(:,1:2)*linear_w,‘g‘,‘linewidth‘,2);
legend(‘Data‘,‘Quadratic‘,‘Linear‘)
Load the Olympic data and extract the mens 100m data
load data/olympics.mat
x = male100(:,1);
t = male100(:,2);
% Rescale x for numerical reasons
x = x - x(1);
x = x./4;
% Plot the data
figure(1);hold off
plot(x,t,‘bo‘,‘markersize‘,10);
xlabel(‘Olympic number (note, not year!)‘);
ylabel(‘Winning time‘);
Linear model
plotx = [x(1)-2:0.01:x(end)+2]‘;
X = [];
plotX = [];
for k = 0:1
X = [X x.^k];
plotX = [plotX plotx.^k];
end
w = inv(X‘*X)*X‘*t;
% Plot the model
figure(1);hold off
figure(1);hold off
plot(x,t,‘bo‘,‘markersize‘,10);
xlabel(‘Olympic number (note, not year!)‘);
ylabel(‘Winning time‘);
hold on
plot(plotx,plotX*w,‘r‘,‘linewidth‘,2)
Quadratic model
plotx = [x(1)-2:0.01:x(end)+2]‘;
X = [];
plotX = [];
for k = 0:2
X = [X x.^k];
plotX = [plotX plotx.^k];
end
w = inv(X‘*X)*X‘*t;
% Plot the model
figure(1);hold off
figure(1);hold off
plot(x,t,‘bo‘,‘markersize‘,10);
xlabel(‘Olympic number (note, not year!)‘);
ylabel(‘Winning time‘);
hold on
plot(plotx,plotX*w,‘r‘,‘linewidth‘,2)
Quartic model
plotx = [x(1)-2:0.01:x(end)+2]‘;
X = [];
plotX = [];
for k = 0:4
X = [X x.^k];
plotX = [plotX plotx.^k];
end
w = inv(X‘*X)*X‘*t;
% Plot the model
figure(1);hold off
figure(1);hold off
plot(x,t,‘bo‘,‘markersize‘,10);
xlabel(‘Olympic number (note, not year!)‘);
ylabel(‘Winning time‘);
hold on
plot(plotx,plotX*w,‘r‘,‘linewidth‘,2)
标签:
原文地址:http://blog.csdn.net/songzitea/article/details/51996911