在matlab中使用矩阵拟合2维函数

最近要解决用2维矩阵拟合2维函数的问题,貌似在中文网站中没有找到方法,最后在stack overflow才问到了答案,解答如下。


假设有5*5数组:

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]

想用最小均方法拟合2维函数:

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


可以在matlab中这样实现:
[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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陆业聪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值