-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataFit.m
48 lines (40 loc) · 1.12 KB
/
DataFit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function [yPredic, xPredic, yFit,coeff,condNum,rSq,polyOrder]=DataFit(x1,y1,isPlotted,xPredic)
%{
function [yPredic, xPredic, yFit,coeff,condNum,rSq]=DataFit(x1,y1,isPlotted)
creates a fit for the data using a correct order polynomial
calling CorrectPoly and extrapolates the data by calling ExtrapData
Alyssa Rose Final Project 04-29-18
%}
%% checks if vectors meet requirements
if length(x1)~=length(y1)
yFit = NaN;
coeff = NaN;
condNum = NaN;
rSq = NaN;
return
end
% finds first nonzero value to make fit
f = find(y1,1,'first');
x = x1(f:end);
y = y1(f:end);
%% sets up A and solves using correct poly order
%forces x,y to be columns
y = y(:);
x = x(:);
[polyOrder,yFit,rSq] = CorrectPoly(x,y);
%%
z = polyOrder + 1;
A = zeros(length(x), polyOrder+1);
A(:,(1:z)) = x.^(polyOrder:-1:0);
condNum = cond(A);
coeff = A\y;
[yPredic] = ExtrapData(coeff,xPredic,polyOrder);
if isPlotted ==1
PredicGraph = figure(2);
plot(x1,y1,'b-',xPredic(:),yPredic,'mo');
title(sprintf('Poly order %i , R^2 = %1.4f', polyOrder,rSq));
xlabel('Years');
ylabel('Food (in tonnes)');
savefig('PredicGraph');
end
end