Using custom fit equation for surface

10 ビュー (過去 30 日間)
Xymbu
Xymbu 2022 年 4 月 29 日
コメント済み: Xymbu 2022 年 4 月 29 日
If I do a fittype for a cruve like this
ft = fittype('a + b*x + c *x^2')
it works fine, but if i want to do a surface fit with a custom equation like with y as a variable like this (just an example not the equation im using)
ft = fittype('a + b*x + c x^2 + d*y^2')
it gives me this error
"The name y cannot be used for both a coefficient and the dependent variable"
How do I work around this.

採用された回答

Torsten
Torsten 2022 年 4 月 29 日
ft = fittype('a + b*x + c x^2 + d*y^2','dependent',{'z'},'independent',{'x','y'},'coefficients'{'a','b','c','d'})
  2 件のコメント
Riccardo Scorretti
Riccardo Scorretti 2022 年 4 月 29 日
Torsten is right. The documents of ftitype reads: "fittype assumes x is the independent variable, y is the dependent variable, and all other variables are coefficients of the model. x is used if no variable exists.", Basically, it is mandatory to set a different name for the dependent variable, which is y by default.
Hereis a full example:
% Generate some noisy data
a0 = 1 ; b0 = 3 ; c0 = -2 ; d0 = 2;
[x, y] = meshgrid(-3:3, -3:3);
data = a0 + b0*x + c0*x.^2 + d0*y.^2;
data = data + 2*randn(size(data));
% fittype assumes x is the independent variable, y is the dependent variable,
% and all other variables are coefficients of the model. x is used if no variable exists.
opt = fitoptions('Method','NonlinearLeastSquares', 'StartPoint', [0 0 0 0]);
fun = fittype('a + b*x + c*x^2 + d*y^2', ...
'independent', {'x', 'y'}, ...
'dependent', 'f', ...
'options', opt);
myfit = fit([x(:) y(:)], data(:), fun)
General model: myfit(x,y) = a + b*x + c*x^2 + d*y^2 Coefficients (with 95% confidence bounds): a = 0.5926 (-0.5208, 1.706) b = 3.125 (2.835, 3.416) c = -1.996 (-2.164, -1.828) d = 2.103 (1.935, 2.271)
% Let's check
hndl = plot(myfit, [x(:) y(:)], data(:));
hndl(1).FaceAlpha = 0.5;
Xymbu
Xymbu 2022 年 4 月 29 日
Aw, hey its you again. hahah Thanks. I appreciate it.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by