Fitting a Surface to a Function of Two Independent Variables

7 views (last 30 days)
Now I have two independent variables, x and y, and one dependent variable z, but I do not know the functional relationship between them. How can I fit the functional relationship between these two independent variables x, y, and the dependent variable z? For example, z = f(x, y), and I do not know the form of f.
  1 Comment
KSSV
KSSV on 21 Feb 2024
Do you have the values of x, y and z? If so give the values. HAve a look in the function fit

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 21 Feb 2024
Let me ask you a simpler question. Here are two variables. What is the functional relationship between them? If y = f(x) what is f?
x = [1 2 3 4 5 6];
y = [0 0 0 0 0 0];
Constant "curve"
I'm guessing you'd say that f(x) = 0 is the relationship. It goes through all the points, right?
f1 = @(x) zeros(size(x));
xfine = 1:0.125:6;
figure
plot(x, y, 'o', xfine, f1(xfine), 'x-')
But you're incorrect, that's not the curve I had in mind.
Polynomial
Your next guess might be that the true form of f(x) is a polynomial whose roots are the values in x.
p = poly(x)
p = 1x7
1 -21 175 -735 1624 -1764 720
f2 = @(x) polyval(p, x);
figure
plot(x, y, 'o', xfine, f2(xfine), 'x-')
You'd be incorrect again, still not what I had in mind.
Sine curve
How about a sine curve as the curve?
f3 = @(x) sinpi(x);
figure
plot(x, y, 'o', xfine, f3(xfine), 'x-')
Sorry, but no.
"The" curve is not unique
As you can probably guess from the fact that I've bolded the word the when I refer to the curve, and from the fact that I've shown three curves (admittedly the first one is trivial) that pass through those six points, the curve that passes through those points is not unique. You can have an unbounded number of different curves that pass through those points; multiply any curve by either the polynomial or the sinpi curve and it'll go through those points. [Assuming it's defined at those points.]
f4 = @(x) f2(x).*(sin(x)+cos(x).*exp(x)); % polynomial times other stuff
figure
plot(x, y, 'o', xfine, f4(xfine), 'x-')
Without more information, how can you tell whether f1, f2, f3, f4, or some other curve I haven't shown is the correct curve to fit the data? You can't.
So what can you do?
There are tools in MATLAB and several other MathWorks products for doing curve fitting: see here and here for two documentation pages in MATLAB, or see the Curve Fitting Toolbox or the Statistics and Machine Learning Toolbox or the Optimization Toolbox .... But generally speaking they all require you to know something about the curve you're trying to fit.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!