MATLAB: How to convert an sfit function into a normal function in order to solve it e.g. for x

sfit function solve for x

I have fittet a surface sf_4 onto a cloud of points using sfit. I would like to know how I could solve for a value on this surface. Usually in sfit [x,y] values are mapped to a z value. How can a solve the sfit function sf(x,0) = z_g (given value) for x ? I tried extracting the function or using syms variables but I do not get any result.
syms x y
sf_4(x,y);
solve(0.5 == sf_4(x,0),'x')
Error message
Warning: Do not specify equations and variables as character strings. Instead, create symbolic variables with syms.
> In solve>getEqns (line 445)
In solve (line 225)
ans =
Empty sym: 0-by-1

Best Answer

Don't use solve, use fzero instead.
% Generate sample data
[x, y] = meshgrid(-5:0.1:5);
z = x.^2 + 2*y.^3 - 3*x.*y;
% Fit a surface
f = fit([x(:), y(:)], z(:), 'poly23')
% Find x such that f(x, 1) is equal to 12
x2 = fzero(@(x) f(x, 1)-12, 0)
% Check your work by evaluating the fit
f(x2, 1)
% Check your work again with the actual equation used to generate z
z2 = x2.^2 + 2 - 3*x2;