fminsearch for ligistic fit

1 view (last 30 days)
Mona Berg
Mona Berg on 28 May 2020
Commented: Mona Berg on 2 Jun 2020
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.

Accepted Answer

Walter Roberson
Walter Roberson on 28 May 2020
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);
  1 Comment
Mona Berg
Mona Berg on 2 Jun 2020
With defining ma data above, I assumed the data to be in my workspace.
But I missed the sse computation, thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Problem-Based Optimization Setup in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!