How to fix one point with no error while curve fitting

I have a data to fit linear plot. But I want to fit the first point with no error. I mean, the fitted curve should fix on the first point and then fit to the rest of the point in 'least square fit' method. How can I do that?

 Accepted Answer

[Edited] removed least squares comment
You could give more importance to the first observation using weights, I do this using the fitlm function in the statistics toolbox:
x = 1:10; y = randn(10,1);
lm1 = fitlm(x,y);
lm2 = fitlm(x,y,'weights',[100, ones(1,length(y)-1)]);
scatter(x,y), hold on
plot(x, lm1.Fitted,'r')
plot(x, lm2.Fitted,'b')

9 Comments

I don't understand how it can't be a least squares problem. All you have to do is optimize as a function of the slope only.
Ok.. here we give maximum weightage to the first point (100?) right? So it is weighted linear square fit. Am I right? Thank you for the suggestion and it seems worth enough to do like this. I want to understand a little bit more so could you please explain me a little bit about the weight here? I see that
[100, ones(1,length(y)-1)]
is giving weight of 100 to the first point and 1 to the rest of the points. If we are not using this weight, by default, matlab will choose the weight 1 for all points? The weight should be between 1 and 100?
Putting a lot of weight on the first point does not guarantee that the line will pass through it.
It can also slow convergence to the solution.
As it was rightly pointed out, this does not guarantee that the line will pass though it. fitlm by default weights all observations by 1. You may choose to weight observations differently and by any number.
Sorry, but YES, you can do a least squares fit with a model with no constant term. Effectively, this is what was asked for. I think you misunderstand the idea of least squares. Least squares does NOT require a constant term to be estimated! You might as well say it is not least squares if there is no quadratic term in the model!
Anyway use of a high weight is a poor way to implement the solution. It causes numerical issues if the weight is too large. And even then, you won't get an exact solution that fits the requirements!
To forestall the inevitable argument that the problem as requested asked for a model that passes through a given point is not equivalent to a model with no constant term, a model that passes through the point (x0,y0) is best estimated as
y - y0 = m*(x - x0)
Where x0 and y0 are known constants. That is, a model with NO constant term.
You can estimate that model as:
m = (x(:) - x0))\(y(:) - y0);
This is the OLS estimate, in the MATLAB way.
Of course, lsqlin could also have been used.
Can you use lsqlin in a similar situation where the problem is trying to maintain fixed extrema for a sin5 model?
If there are intrinsically nonlinear parameters to estimate, then no, lsqlin will not suffice. It solves only problems that are linear in the parameters. However, you CAN construct a model which will pass through a point exactly for a sinusoidal model, or have a variety of fixed properties. I assume your question relates to the one you posed recently on answers. I'm sorry, but that was a confusing question that I chose not to answer, partly because it was not at all clear what form your data was in, what form the model took on, etc. It looked like you had only a blurry image, and needed to extract a model of some form from that.
Yeah, sorry about that. It does have to do with the question asked but I didn't realize the images were so blurry. I do just want to 'construct a model which will pass through a point exactly for a sinusoidal model' although it may be several points and multiple sine terms if that's possible.
The data is just in a form according to the indices of the image.

Sign in to comment.

More Answers (1)

It depends on the model function for your curve. Ideally, you would choose the formula for the curve to be such that it passes through the desired point. For example, the following linear curve function has unknown slope parameter m, but is written so that it always passes through the known point (x0,y0)
y=m*(x-x0)+y0
If the formula for your model is not tractable enough for this, you may need to use fmincon to do the curve-fitting and specify your constraint using the nonlcon argument.

Categories

Asked:

on 9 Jun 2014

Commented:

on 14 Jan 2015

Community Treasure Hunt

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

Start Hunting!