% Example 3, Section 6.5 %Make up some noisy data. x=3*pi*rand(40,1); %40 random numbers between 0 and 3*pi x=sort(x); y=-sin(x)+3*sin(2*x)-0.5*cos(x)+4*cos(3*x)+randn(size(x)); %Create matrix A: A=[sin(x) sin(2*x) cos(x) cos(3*x)]; c=inv(A'*A)*A'*y %Computing the error between y and the values on the line: Err=norm(y-A*c); fprintf('The error is: %f\n',Err); xTest=linspace(0,10)'; %Create test data yTest=[sin(xTest) sin(2*xTest) cos(xTest) cos(3*xTest) ]*c; plot(x,y,'k*',xTest,yTest,'k-');