How to fit my data with a custom equation meant to come out as a parabola?

5 views (last 30 days)
I have some data that I'm trying to fit with an equation.
That equation is:
Where y is the variance of my data and x is the average of my data. I'm trying to get out what i and N are. The values should fit to a parabola (as seen in previous literature), but some of my data is not working with what I currently have.
I've been trying to fit this with the lsqcurvefit function, by doing this: (bg is the background variance which I subtract from y before trying to fit to the equation.)
constant = lsqcurvefit(@f3,[min(average);min(variance);max(bg)/2],x,y);
function y = f3(constant,x)
y = (constant(1)* x)- ((x.^2)/constant(2));
end
I've picked the least squares method since I've seen lots of research in my field fit it this way, but the way this code runs I get numbers for i and N that look very off and the constant is giving me back three values, not two.
Any thoughts?
  1 Comment
Torsten
Torsten on 6 Oct 2022
Edited: Torsten on 6 Oct 2022
Maybe you could explain the part
That equation is:
y = i*x - x^2/N
where y is the variance of my data and x is the average of my data.
It sounds as if you want to fit two parameters to one value pair (mean(data),variance(data)). But I think this is not what you mean, is it ?

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 6 Oct 2022
The ‘constant’ (parameter vector) is giving back three values because you are giving it three values to begin with:
[min(average); min(variance); max(bg)/2]
It has no idea what to do with the third one (and I have no idea what it does with three values when it only uses two). See if just giving it the first two (or the two that are most appropriate) produces a better result.
  2 Comments
Rebecca Somach
Rebecca Somach on 6 Oct 2022
This hasn't solved my problems, but the code doesn't fall apart when I only give it two values, so that's at least a start. Thank you for answering!
Star Strider
Star Strider on 6 Oct 2022
My pleasure!
I have no idea what you’re doing.
You may not need lsqcurvefit anyway, since your function is essentially linear, and is equivalent to:
B = [x(:) x(:).^2] \ y(:)
constant(1) = B(1)
constant(2) = -1/B(2)
x = 1:10;
y = 2*x(:) - x(:).^2/5;
B = [x(:) x(:).^2] \ y(:)
B = 2×1
2.0000 -0.2000
const(1) = B(1)
const = 2.0000
const(2) = -1/B(2)
const = 1×2
2.0000 5.0000
I’m not certain what you’re doing, so this is simply a guess.

Sign in to comment.


Matt J
Matt J on 6 Oct 2022
Edited: Matt J on 6 Oct 2022
If there are 3 coefficients, you should just use polyfit(x,y,2) and forget about lsqcurvefit. No reason an iterative solution is required.
If you are deliberately modeling with only 2 unknown coefficients, you should still use polyfit(x,y,2) or maybe polyfit(x,y./x, 1) to derive the initial guess.
Other than that, there's not much to go on, without runnable code.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!