Using custom fit equation for surface

4 views (last 30 days)
Xymbu
Xymbu on 29 Apr 2022
Commented: Xymbu on 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.

Accepted Answer

Torsten
Torsten on 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 Comments
Riccardo Scorretti
Riccardo Scorretti on 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
Xymbu on 29 Apr 2022
Aw, hey its you again. hahah Thanks. I appreciate it.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!