Skip to content

Instantly share code, notes, and snippets.

@stephenjbarr
Forked from biancagabriela/gist:4736994
Last active December 12, 2015 07:28
Show Gist options
  • Save stephenjbarr/4737061 to your computer and use it in GitHub Desktop.
Save stephenjbarr/4737061 to your computer and use it in GitHub Desktop.
clear all; close all; clc
%% Exercise 3-1A
% find reasonable best-fit polynomial for data1 and data2
data1 = load('data1.txt');
data2 = load('data2.txt');
data3 = load('data3.txt');
x1 = data1(:,1);
y1 = data1(:,2);
x2 = data2(:,1);
y2 = data2(:,2);
P1 = polyfit(x1,y1,1);
P2 = polyfit(x2,y2,2);
save -ASCII A1.dat P1;
save -ASCII A2.dat P2;
% find root mean square error for data1 and data2
poly1 = polyval(P1,x1);
rms1 = sqrt((mean(y1-poly1)).^2);
poly2 = polyval(P2,x2);
rms2 = sqrt((mean(y2-poly2)).^2);
ans3 = [rms1 rms2];
save -ASCII A3.dat ans3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Exercise 3-1B
%Compute the best-fit polynomial for data3 for each polynomial order between 1 and 10. Also compute the RMS error between each polynomial fit and the data. Evaluate the 7-th order polynomial at locations .03:.03:3, and store as a row vector Fvals.
x=zeros(10,11)
for i = 1:10;
## x(i) = polyfit(data3(:,1),data3(:,2),i)
x(i,:) = [polyfit(data3(:,1),data3(:,2),i) zeros(1, 10-i)];
end
%for x = .03:.03:3
% y = polyval(p3_3,x)
%end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment