# How to fit a curve to a graph?

5 messages
Open this post in threaded view
|

## How to fit a curve to a graph?

 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.
Open this post in threaded view
|

## Re: How to fit a curve to a graph?

 On Mon, May 18, 2020 at 7:30 PM Doron Behar <[hidden email]> 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, and in particular, it has a function dedicated to linear regression.  Have you checked the documentation and tried adapting the examples?
Open this post in threaded view
|

## Re: How to fit a curve to a graph?

 On Mon, May 18, 2020 at 07:38:11PM -0400, Brett Green wrote: > On Mon, May 18, 2020 at 7:30 PM Doron Behar <[hidden email]> 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 , > and in particular, it has a function dedicated to linear regression > .  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.