How to fit custom equation in a linear regression?

I'd like to fit a custom equation for linear regression. For example y = a1 + a2 * x^4 + a3 * x^5. How do you do this with fitlm function?

7 Comments

I don't think this is a linear regression anymore. Are you sure it is?
You can fit equations to your data with many functions. Do you specifically need fitlm?
I forgot to add, I do have a set of data for x and y. But I need to fit a custom equation on the data. It would be better if someone could help me using fitlm but other fit functions are good too.
If you attach example data (it doesn't have to be real data, just vaguely similar) we can help you directly fit your equation to the data.
If you have the curve fitting toolbox, you can also use cftool to explore your options.
Here's a sample data, A is x and B is y. I already got results using cftool but I wanna know how to obtain the same results manually, through codes.
Why didn't you attach the data to your post? Do you have problems with the licencing terms if you would do so?
You should have the option to generate code from the cftool. Have you tried to do so? What options did you use to get the fit you wanted?
This is actually the first time I posted a question so I don't really know how things work. And yes, I've tried cftool and got results through the custom equation fit option. But I also want to know how to do it manually or through codes using fit functions.
And the other questions I asked in my comment? Did you try the code generation tool? What fit options did you use?

Sign in to comment.

Answers (1)

cr
cr on 18 Nov 2022
Edited: cr on 18 Nov 2022
Presuming you are trying to estimate a1, a2, a3 you can just to a lmdivide. You will need data for x & y with atleast 3 to solve (and 4 for a linear regression).
e.g. with the n observations, you will have n equations a1 + a2xi^4 + a3xi^5 = yi
do
A = X\Y
where A is the 3 x 1 vector of unknowns a1 a2 a3
X is n x 3 matrix of observations x1, x2, x3,...xn
[1 x1^4 x1^5;
1 x2^4 x2^5; ....;
1 xn^4 xn^5]
Y is n x 1 vector
[y1; y2;..;yn]
Regards.

2 Comments

Thanks. And I do have a set of data for x and y (30 points). But I also need to fit a custom equation on the data so a fit function would be needed.
cr
cr on 18 Nov 2022
Edited: cr on 18 Nov 2022
You may use fitlm(X,Y). Note that \ or fitlem can be used only for linear regression and not any equation in general. You will need other functions like nlinfit etc for any equation in general.
Regards.

Sign in to comment.

Categories

Products

Asked:

on 18 Nov 2022

Commented:

Rik
on 18 Nov 2022

Community Treasure Hunt

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

Start Hunting!