help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to fit a curve to a graph?


From: James Sherman Jr.
Subject: Re: How to fit a curve to a graph?
Date: Mon, 18 May 2020 23:34:24 -0400


On Mon, May 18, 2020 at 11:11 PM Doron Behar <address@hidden> wrote:
On Mon, May 18, 2020 at 10:30:20PM -0400, James Sherman Jr. wrote:
> On Mon, May 18, 2020 at 10:10 PM Doron Behar <address@hidden> wrote:
>
> > On Mon, May 18, 2020 at 07:38:11PM -0400, Brett Green wrote:
> > > On Mon, May 18, 2020 at 7:30 PM Doron Behar <address@hidden>
> > wrote:
> > >
> > > > Dear octaves,
> > > >
> > > > I'm a bit new to Octave and I'm trying to use it as a free software
> > > > alternative to Matlab which is considered a requirement for my physics
> > > > lab course. I strongly hope I won't have to eventually install Matlab
> > > > just because I can't find the equivalents of certain Matlab features
> > > > which seem missing from Octave. My instructors are most probably not
> > > > aware of GNU octave and free software and all that Jazz so I need help.
> > > >
> > > > I have an x_data and y_data for which I managed to fit a linear
> > > > polynomial function with:
> > > >
> > > > [fit, regression_info] = polyfit(x_data, y_data, 1);
> > > >
> > > > My instructors have asked me to (translated):
> > > >
> > > > 1) Perform a linear regression for test results.
> > > > 2) Add to the graph the regression curve.
> > > >
> > > > I'm mostly puzzled by the term "regression curve" which I hope I got
> > > > translated correctly.
> > > >
> > > > We were given the following code sample with which (If I got it
> > > > right) we can get the regression curve:
> > > >
> > > > ```
> > > > linear_regression = fitlm(x,y);
> > > > R_squared = linear_regression.Rsquared.Ordinary;
> > > > b_coefficient = linear_regression.Coefficients.Estimate(1);
> > > > a_coefficient = linear_regression.Coefficients.Estimate(2);
> > > > b_error = linear_regression.Coefficients.SE(1);
> > > > a_error = linear_regression.Coefficients.SE(2);
> > > > plot(x,y,'b.',x,linear_regression.Fitted,'k');
> > > > ```
> > > >
> > > > But it seems that the function `fitlm` is not available in GNU octave,
> > > > meaning I can't use it as is for my assignment.
> > > >
> > > > What should I do? How can I give them the plot they desire? I don't
> > even
> > > > have a sample of how this should look like. I also wish to eventually
> > > > finish the script in a Matlab compatible manner.
> > > >
> > > > Besides the "regression curve", I think they have also asked me to
> > > > display on the graph the error bar according to the regression_info.
> > > > I've managed to learn I should use:
> > > >
> > > > errorbar(x_data, y_data, err);
> > > >
> > > > But I don't know how to compute `err` out of `regression_info`.
> > > >
> > > > It's likely that I don't have a full understanding of the whole theory
> > > > of curve fitting but I hope I can get the desired results never the
> > > > less.
> > > >
> > > > Regards.
> > > >
> > > >
> > > >
> > >
> > > Octave can certainly do curve fitting and has numerous functions for that
> > > purpose <https://octave.sourceforge.io/optim/overview.html#Data_fitting
> > >,
> > > and in particular, it has a function dedicated to linear regression
> > > <https://octave.sourceforge.io/optim/function/LinearRegression.html>.
> > Have
> > > you checked the documentation and tried adapting the examples?
> >
> > Thanks for the quick reply. I haven't fully dived into these examples
> > yet although they look promising. It seems that these functions are not
> > compatible with Matlab and it's a problem for me because I need to
> > provide back to my instructors a `.m` file for them to run on Matlab :(.
> >
> > I _will_ need to create a report eventually which wouldn't constitute a
> > `.m` file someone will run so I'll use these functions when the time
> > comes.
> >
> >
> >
> Hi Dohan,
>
> Not to put words in your professor's mouth, but I believe they simply mean
> to graph the linear regression line that you get from the coefficients you
> obtained from polyfit.  This can be done simply with using the
> coefficients, the x_data you already have, and the plot function.
>
> Hope this helps,
>
> James Sherman

Mr. Sherman,

The problem is that I DON'T KNOW WHAT A REGRESSION LINE IS (ง •̀_•́)ง.

It's not like we've been through a data analysis course before this lab
course. They just told us to "get a regression curve" using this code:

> > > > linear_regression = fitlm(x,y);
> > > > R_squared = linear_regression.Rsquared.Ordinary;
> > > > b_coefficient = linear_regression.Coefficients.Estimate(1);
> > > > a_coefficient = linear_regression.Coefficients.Estimate(2);
> > > > b_error = linear_regression.Coefficients.SE(1);
> > > > a_error = linear_regression.Coefficients.SE(2);
> > > > plot(x,y,'b.',x,linear_regression.Fitted,'k');

And I don't understand what's going on there and I can't reproduce it
with Octave because this code is not compatible so I feel stuck.

I tried to figure this material out by reading the function descriptions
here: https://octave.sourceforge.io/optim/function/LinearRegression.html
but I feel like I lack basic understanding of the concepts, or perhaps
just the patience to learn this thoroughly.

Excuse me for putting my nerves via ascii emoticons I'm just a bit
frustrated - maybe I'm not expected to deliver that much of analysis,
IDK.

Doron.

 Hi Doron,

Please keep the mailing list in your reply, so that others know what the problem is, and may be able to help (or hopefully when your problem is solved).  From the above code, it looks like all you're plotting is the line of best fit, which is exactly what polyfit gives you (well, to be precise, the coefficients of that line, which is the same as the a_coefficient and b_coefficient).  The rest of those variables are for telling you how well the line fits the data (R_squared) or the confidence in those variables (a_error, and b_error), and don't seem to be part of the plot that your professor wants you to make. 

So, long story short, you seem to only need the equation of the line of best fit (also called the linear regression curve, or regression line) which you can find by using the polyfit function to give you the coefficients of the line (i.e. the slope and y-intercept).  Then plot that line.

Hope this helps,

James Sherman

reply via email to

[Prev in Thread] Current Thread [Next in Thread]