【问题标题】:How to use the data matrix to fit the specific 2D-function in matlab如何使用数据矩阵来拟合matlab中的特定二维函数
【发布时间】:2013-07-24 02:44:58
【问题描述】:

前提是我有 5*5 的 dataArray

    d=    [0.0177104427823448,0.00246661459209512,0.0399831543374395,0.0615494164555707,0.0476204124707652;0.0275276152854314,0.0219153841813084,0.0581144391404502,0.144890028400954,0.157839631316098;0.0622883972729130,0.0716157303159909,0.245482781674067,0.123999612575059,0.177495187746408;0.0200735764542146,0.0573087934038160,0.0636451189717613,0.0160810084568415,0.0484992279558924;0.0185180386159227,0.00841167700273800,0.0372017422726281,0.0173721095082637,0.0459520362441099]

我想使用数据通过最小二乘拟合技术来拟合特定的二维函数。函数是这样的:

    r = alfa*sin(pi*(n1+delta1))*sin(pi*(n2+delta2)) / (25*sin(pi/5*(n1+delta1))*sin(pi/5*(n2+delta2))),

其中alfa,delta1,delta2是需要估计的参数,n1,n2的取值范围是1到5。

函数拟合结果如下:

我不知道如何在 MATLAB 中做这些事情。谁能帮帮我?

【问题讨论】:

  • 你有曲线拟合工具箱吗?如果你这样做了,那么lsqnonlin 是你最好的选择。否则也许看看fmincon
  • 是的,我有。但是lsqnonlin 可以解决二维函数拟合问题吗? @丹
  • 当然,不过需要一点操作:mathworks.com/matlabcentral/newsreader/view_thread/238630
  • 嗯,我已经按照你发布的网站实现了函数拟合过程,我已经发布了代码作为我的问题的答案。但是结果不是很好,因为应该有一个峰值在最终结果中,拟合数据比原始数据更平滑! @丹
  • 在拟合之前尝试插入您的 5x5 矩阵?

标签: matlab data-fitting


【解决方案1】:

好吧,感谢@Dan。我的问题似乎是这样实现的:

[n,m]=size(d);%assumes that d is a n x m matrix
[X,Y]=meshgrid(1:n,1:m);%your x-y coordinates
x(:,1)=X(:); % x= first column
x(:,2)=Y(:); % y= second column
f=d(:); % your data f(x,y) (in column vector)

%--- now define the function in terms of x
%--- where you use x(:,1)=X and x(:,2)=Y
fun = @(c,x) c(1)*sin(pi*(x(:,1)+c(2))).*sin(pi*(x(:,2)+c(3))) ./ (25*sin(pi/5*(x(:,1)+c(2))).*sin(pi/5*(x(:,2)+c(3))));

%--- now solve with lsqcurvefit
options=optimset('TolX',1e-6);
c0=[1 0 0];%start-guess here
cc=lsqcurvefit(fun,c0,x,f,[],[],options);
Ifit=fun(cc,x); 
Ifit=reshape(Ifit,[n m]);%fitting data reshaped as matrix
surf(X,Y,Ifit);
hold on;
plot3(X, Y, dataArray);

【讨论】:

    猜你喜欢
    • 2017-09-10
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    相关资源
    最近更新 更多