Using custom fit equation for surface

10 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Linear 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