# Fitting a Surface to a Function of Two Independent Variables

7 views (last 30 days)

Show older comments

### Accepted Answer

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)

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.

##### 0 Comments

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!