forked from mikedurand/SWOTAprimeCalcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalcWHFitsEIV.m
43 lines (36 loc) · 875 Bytes
/
CalcWHFitsEIV.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
function [p,xbreak,iout,hval,wval]=CalcWHFitsEIV(Hobs,Wobs,nReg,stdH,stdW)
%%
x=Hobs;
y=Wobs;
xbreak0=linspace(min(x),max(x)+.1,nReg+1);
p0hat=[];
% find outliers
iout=isoutlier(y) & isoutlier(x);
if any(iout) && all(max(x(iout))>=x) && all(max(y(iout))>=y)
HighOut=true;
xbreak0(nReg)=max(x(~iout));
if xbreak0(2)>xbreak0(3)
xbreak0(2)=mean(xbreak0([1 3]));
end
else
HighOut=false;
end
% calculate preliminary fits
for i=1:nReg
iReg=x>=xbreak0(i) & x<xbreak0(i+1);
if sum(iReg)>2
p0{i}=polyfit(x(iReg),y(iReg),1);
elseif i>1
p0{i}=p0{1};
else
p0{i}=[100 min(y)];
end
p0hat=[p0hat p0{i}];
end
% fit analysis and unpacking
[xbreak,sse,phat]=WH_EIV(x,y,p0hat,stdH^2,stdW^2,xbreak0,nReg,HighOut);
for i=1:nReg
p{i}=phat((i-1)*2+[1 2] );
end
[hval,wval]=TestEvaluation(x,xbreak,p,nReg);
end