# How to fit custom equation in a linear regression?

10 ビュー (過去 30 日間)
John Carlo 2022 年 11 月 18 日
コメント済み: Rik 2022 年 11 月 18 日
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 件のコメント表示非表示 6 件の古いコメント
Rik 2022 年 11 月 18 日
And the other questions I asked in my comment? Did you try the code generation tool? What fit options did you use?

サインインしてコメントする。

### 回答 (1 件)

cr 2022 年 11 月 18 日

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 件のコメント表示非表示 1 件の古いコメント
cr 2022 年 11 月 18 日

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.

サインインしてコメントする。

### カテゴリ

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by