Fitting a Surface to a Function of Two Independent Variables
10 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
Steven Lord
2024-2-21
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!