This preview shows page 1. Sign up to view the full content.
Unformatted text preview: %checks that vectors are the same length disp('Error: the number of elements in x must be the same as in y') else Sx = sum(x); %Calculates sums of x, y x*y and x^2 Sy = sum(y); Sxy = sum(x.*y); Sxx = sum(x.^2); a1 = (nx*Sxy-Sx*Sy)/(nx*Sxx-Sx^2); a0 = (Sxx*Sy-Sxy*Sx)/(nx*Sxx-Sx^2); end for i = 1:nx %calculates squared error per point E(i)=(y(i)-(a1*x(i)+a0))^2; end Err=sum(E)/nx; %sums squared errors and divides by # points...
View Full Document
- Spring '07