fminsearch for ligistic fit
1 次查看(过去 30 天)
显示 更早的评论
I'm trying to use fminsearch to fit a logistic model to my data. My problem is that I'm rreally confused about how to define the input for the fminsearch function.
My data:
x = [0.5 2.7 6 8.8 9.7 12.8 16 20 23];
y = [0.8 1.1 3.4 7 9 13 13.4 13.2 18.8];
I want to fit the parameters of following function to my data:
Now I don't know how to define the right functions for the optimization.
What I've tried so far:
f = @(p,x)p(1)*(0.5-(1./(1+exp(p(2)*(x-p(3))))))+p(4)*x+p(5);
f2 = @(p)f(p,x);
p0 = [0.1 0.1 0.1 0.0001 0.1];
p = fminsearch(f2,p0);
But this is not the right way.
0 个评论
采纳的回答
Walter Roberson
2020-5-28
f2 = @(p)f(p,x);
That relies on x existing in your workspace. If you are going to do that you might as well assume it on the previous line, and use cleaner code.
But your bigger problem is that you are failing to calculate a sum-of-squared errors
f = @(p) p(1) * (0.5 - (1 ./ (1 + exp(p(2)*(x-p(3)))))) + p(4) .* x + p(5);
f2 = @(p) sum((f(p) - y).^2);
p0 = [0.1 0.1 0.1 0.0001 0.1];
p = fminsearch(f2, p0);
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!