Positive or zero intercept for linear function
Show older comments
Trying to fit a linear function to a set of points with intercept either 0 or positive.
Code:
yld = [0.016848189
0.01682922
0.019936858
0.027905862
0.03179837
0.028801833
0.028801884
0.044003395
0.048011902
0.048013812
0.043509457
0.043510379
0.059143077
0.044998549
0.04499765
0.044998484
0.044898415
0.04499845
0.044408223
0.044222909
0.049398674
0.052317402
0.054167487
0.054265443
0.048420988
0.04656471
0.04998282];
t = [0.320547945
0.320547945
0.550684932
1.849315068
2.471232877
2.542465753
2.542465753
2.55890411
2.909589041
2.909589041
3.030136986
3.030136986
4.063013699
4.975342466
4.975342466
4.975342466
4.975342466
4.975342466
5.663013699
5.663013699
5.663013699
9.512328767
15.01643836
16.02465753
19.61917808
19.61917808
19.61917808];
x = [ones(length(t), 1) log(t) log(t).^2];
y = log(yld + 1);
betas = x\y;
tau = [0.083 0.25 0.5 1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
yfit = betas(1) + betas(2)*log(tau) + betas(3)*(log(tau).^2);
plot(yfit);
I realize than as t goes to 0 log(t) goes to -inf, however, could there be a workaround, perhaps with a worse fit, but with nonnegative intercept?
Answers (1)
Star Strider
on 30 Nov 2019
Linearising the exponiential function may not be the best option. I am not certain what you are modeling, however using a nonlinear objective function with fminsearch (or any other appropriate optimiser) may be worth considering.
Try this to fit
:
objfcn = @(b,t) b(1) + b(2).*(1 - exp(b(3).*t));
[B,rsdnorm] = fminsearch(@(b) norm(yld - objfcn(b,t)), randn(3,1))
yfit = objfcn(B,tau);
I was able to get an acceptable fit with the parameter set:
B =
0.010429559813784
0.040470293461631
-0.456482676457038
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!