Forcing the best fit line to pass through a certain point (Octave)

In summary: I don't know what you mean by "the underlying model". If you're trying to fit a model where there is a constant term, then you should use polyfit(). If you're trying to fit a model where there is no constant term, then you should use polyval().I know that. I do it only when I am forced to plot a graph of ##y=mx## rather than ##y=mx+c## because of the nature of the underlying data.
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,111
2,691
I have the following code in Octave:
Matlab:
h = [29.3 25.3 19.7 16.0 10.9];
v = [0.53 0.47 0.37 0.29 0.21];
plot(h,v,'obk')
hold on
p = polyfit(h,v,1);
y = polyval(p,h);
plot(h,y,'-bk')
And I get a good graph:
viscosity.jpg


I can extrapolate the best fit line using the following code:
Matlab:
x = -1:0.01:11;
>> y = polyval(p,x);
>> plot(x,y,'--r')
and if I zoom on the graph, I get this:
extrapolated.jpg

Evidently, the line doesn't pass through (0,0).

But I have to make it pass through the Origin. In that case, it will no longer be the best fit line, but nevertheless it will serve my purpose.

Any idea on how to do this?
 
Physics news on Phys.org
  • #3
Find the angle of the line through origin that minimizes mean squared error.
 
  • #4
Wrichik Basu said:
Evidently, the line doesn't pass through (0,0).

But I have to make it pass through the Origin. In that case, it will no longer be the best fit line, but nevertheless it will serve my purpose.

Any idea on how to do this?

Yes. You're fitting a different model. Instead of ##y = mx + b## you want to fit ##y = mx##. I don't know if you can do it with polyfit(), but the math is pretty simple.

Minimize the square error E
##E = \sum_i (y - y_i)^2 = \sum_i (mx_i - y_i)^2 = \sum_i m^2 x_i^2 - 2 \sum_i mx_i y_i + \sum_i y_i^2##

##dE/dm = 0 \\
\Rightarrow 2m \sum_i x_i^2 - 2\sum_i x_i y_i = 0 \\
\Rightarrow m = (\sum_i x_i y_i )/ (\sum_i x_i^2)##

That is the best fit value of ##m## for the model ##y = mx##, and you should interpolate / extrapolate using that model.
 
  • Like
Likes Wrichik Basu
  • #5
In the MATLAB function fitlm, you can specify the desired model that does not have a constant term using a modelspec like 'Y ~ A + B + C – 1' . See https://www.mathworks.com/help/stats/fitlm.html#bt0ck7o-modelspec
I believe that their other linear regression tools have similar capabilities. I don't know about Octave.
 
Last edited:
  • #6
FactChecker said:
I don't know about Octave.
Octave says that fitlm has not yet been implemented.
 
  • #7
@RPinPA Thanks, that works fine. I plotted the function using fplot, and I am getting the desired results.
jedishrfu said:
You could add 0,0 to your collection of points.
Good idea.
 
  • #8
jedishrfu said:
You could add 0,0 to your collection of points.
That would draw the line toward (0,0). You may need to add it many times to get it as close as you want and then all the statistical calculations would be messed up.
 
  • Like
Likes Wrichik Basu and jedishrfu
  • #9
FactChecker said:
That would draw the line toward (0,0). You may need to add it many times to get it as close as you want and then all the statistical calculations would be messed up.

Sometimes cheap solutions work but not as well as one would like that’s why they’re cheap.

Octave apparently doesn’t have the fitlm() function but does have some linear regression methods.

https://octave.sourceforge.io/optim/function/LinearRegression.html
 
  • #10
jedishrfu said:
Sometimes cheap solutions work but not as well as one would like that’s why they’re cheap.
Absolutely, I don't expect any better. How much can one provide for free? There are some major differences between Matlab and Octave. For example, for symbolic math, Octave depends on SymPy, while Matlab was created much before Python.
 
  • Like
Likes jedishrfu
  • #11
I sometimes use freemat when I need to compute a quick plot. It has much of the core Matlab functionality and is easy to install.

More recently, Julia from MIT has come online to challenge Matlab in performance. Much of its syntax is similar to Matlab with notable differences in how arrays are referenced ie parens in Matlab vs square brackets in Julia. Many folks are extending the Julia ecosystem with new packages on github everyday. It’s main weakness is its IDE which is cobbled together using Juno or using Jupyter notebooks. The notebooks are preferred over the IDE but Matlab users have a great IDE that’s hard to give up.
 
  • Like
Likes Wrichik Basu
  • #12
In general forcing a regression line to go through the origin is not a good idea -- it renders the traditional correlation and r^2 values meaningless, for example. You should only do it if you have a valid reason (theoretical or other) do do so.
 
  • Like
Likes jedishrfu
  • #13
statdad said:
In general forcing a regression line to go through the origin is not a good idea -- it renders the traditional correlation and r^2 values meaningless, for example. You should only do it if you have a valid reason (theoretical or other) do do so.
I know that. I do it only when I am forced to plot a graph of ##y=mx## rather than ##y=mx+c## because of the nature of the underlying equation.
 
  • Like
Likes jedishrfu

1. How do I force a best fit line to pass through a specific point in Octave?

In order to force a best fit line to pass through a specific point in Octave, you can use the "polyfit" and "polyval" functions. First, use "polyfit" to find the coefficients of the best fit line for your data. Then, use "polyval" to evaluate the best fit line at the desired point. Finally, plot the original data and the new point along with the best fit line, which will now pass through the desired point.

2. Can I specify the slope of the best fit line in Octave?

Yes, you can specify the slope of the best fit line in Octave by using the "polyfit" function and specifying the desired slope as the second input argument. However, keep in mind that the line will still be adjusted to best fit the data, so the resulting slope may not match the exact input value.

3. What happens if I force the best fit line to pass through more than one point in Octave?

If you force the best fit line to pass through more than one point in Octave, it will try to find a line that minimizes the distance between all of the specified points. This may result in a line that does not fit the original data very well, so it is important to consider the overall accuracy of the fit.

4. Can I force the best fit line to pass through a point using only the equation of the line?

No, the equation of a line only defines the slope and y-intercept, and cannot be used to specify a single point that the line must pass through. However, you can use the equation of the line to calculate the x-value at which the line intersects the desired point and then use that value in the "polyval" function to force the line to pass through the point.

5. Is forcing a best fit line to pass through a point a common practice in scientific analysis?

Yes, forcing a best fit line to pass through a specific point is a common practice in scientific analysis, especially when the point has significant meaning or represents a known value. This can also be useful for making predictions or analyzing the accuracy of the fit in certain areas of the data.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
10K
  • General Math
Replies
1
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
3K
  • Calculus and Beyond Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
Replies
7
Views
21K
Back
Top