# Using custom fit equation for surface

6 visualizzazioni (ultimi 30 giorni)
Xymbu il 29 Apr 2022
Commentato: Xymbu il 29 Apr 2022
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.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Torsten il 29 Apr 2022
ft = fittype('a + b*x + c x^2 + d*y^2','dependent',{'z'},'independent',{'x','y'},'coefficients'{'a','b','c','d'})
##### 2 CommentiMostra NessunoNascondi Nessuno
Riccardo Scorretti il 29 Apr 2022
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 il 29 Apr 2022
Aw, hey its you again. hahah Thanks. I appreciate it.

Accedi per commentare.

### Categorie

Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by