用于两个变量的Polyfit [英] Polyfit for two variables
本文介绍了用于两个变量的Polyfit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种数据,想找到给定数据的方程式(poly coeff).例如,给定样本数据的方程式很简单a^2*b+10
I have a kind of data and want to find the equation(poly coeff) of given data. For example equation for given sample data is simple a^2*b+10
a\b 5 10 15
________________________
3| 55 100 145
4| 90 170 250
5| 135 260 385
6| 190 370 550
我检查了polfit
,但它仅适用于一个变量.
I checked forpolfit
but It only works for one variable.
推荐答案
如达斯蒂·坎贝尔指出,您可以使用fit
函数.为此,您必须使用数据构建网格
As Dusty Campbell pointed out you can use the fit
function. To do this you have to build a mesh with your data
a = [3 4 5 6];
b = [5 10 15];
[A, B] = meshgrid(a, b);
C = (A.^2).*B + 10;
,然后使用自定义公式调用fit
and then call fit
with a custom equation
ft = fittype('p1*a^2*b + p2', 'independent',{'a','b'}, 'dependent','c');
opts = fitoptions('Method','NonlinearLeastSquares', 'StartPoint',[0.5,1]);
[fitresult, gof] = fit([A(:), B(:)], C(:), ft, opts);
您将看到求解器收敛到正确的解决方案p1 = 1
,p2 = 10
.
As you'll see the solver converges to the correct solution p1 = 1
, p2 = 10
.
这篇关于用于两个变量的Polyfit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文