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 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