How do I get a symbolic differentiation of cfit/sfit functions?

Hi everyone,
I am trying to get the symbolic partial derivative of a sfit function. Best case would be having the differentition as an sfit function, too.
Thanks in advance!
EDIT:
Here's an example:
%data
x = [0.5850;0.0734;0.8223;0.7229;0.9259;0.4926];
y = [0.6549;0.8901;0.5385;0.2822;0.9760;0.0364];
z = [0.3262;0.9730;0.3650;0.3091;0.1209;0.9158];
%surface fit
sf = fit([x, y],z,'poly22')
plot(sf)
% Linear model Poly22:
% sf(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2
% Coefficients:
% p00 = 5.048
% p10 = -11.04
% p01 = -1.326
% p20 = 5.179
% p11 = 5.682
% p02 = -3.135
Now I could use the differentiate method to get the derivative at specific points but I would like to have the derived function, like this (witthout having to derive it manually):
% sf_x(x,y) = p10 + 2*p20*x + p11*y

3 Comments

Did you try diff function?
Yes, I did. diff does not take sfit as input argument, unfortunately.
Can you provide an example please?

Sign in to comment.

 Accepted Answer

When sf is the fit object,
f = subs(str2sym(formula(sf)),coeffnames(sf),num2cell(coeffvalues(sf).'));
vars = sym(indepnames(sf));
diff(f, vars(1))

5 Comments

That works well, thanks!
Do you now if there is some way to create a sfit function of the derivative?
In particular, I also want to store the other information saved in the original sfit, such as the range of values that was used for the fitting.
It appears to me that to do that you would use a different approach:
vars = sym(indepnames(sf));
df = diff( str2sym(formula(sf)), vars(1)) %or appropriate variable number
dfun = matlabFunction(df, 'vars', sym(argnames(sf)));
ft = fittype(dfun, 'independent', indepnames(sf), 'dependent', dependnames(sf), 'options', fitoptions(sf));
However, this does not construct an sfit object, as it is not the result of fitting. You could use it to fit something.
More subtly, the fittype() call will fail, because it tries to use all of the coefficient names from sf to provide a direct correspondance, but the differential formula omits some of the coefficients because they vanish in the derivative. The fix using the used coefficient names is
vars = sym(indepnames(sf));
df = diff( str2sym(formula(sf)), vars(1)) %or appropriate variable number
used_coeffs = sym(intersect(symvar(df), sym(coeffnames(sf))));
var_order = [used_coeffs; sym(indepnames(sf))];
dfun = matlabFunction(df, 'vars', var_order);
ft = fittype(dfun, 'independent', indepnames(sf), 'dependent', dependnames(sf), 'options', fitoptions(sf));
Great, thanks!
Would you know a way, how to extract the range boundaries of the fit sf?
It must be stored somewhere in the object since plot(sf) plots exactly the range of the fitted data.
The upper and lower bounds are in the fit options, which I have it add in via 'options', fitoptions(sf)

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!