请matlab高手根据给出的数据编写一个拟合二元函数的程序

2025-02-22 23:13:50
推荐回答(1个)
回答1:

在不知道原理的情况下,函数的选择本身就有些随意性。我选了一种函数来拟合,程序和结果你运行后就能看见。


clear all


clc


x=[50 100 150 200 250 300 350 400 450 500];


y=[40 80 120 160 200];


[X,Y]=meshgrid(x,y);


z=[0.05 0.05 0.05 0.05 0.05


0.25 0.15 0.11 0.05 0.05


0.5 0.2 0.2 0.04 0.04


1.6 1 0.5 0.11 0.07


2.5 2.4 1.14 0.34 0.12


3.2 2.7 1.54 0.7 0.21


3.4 3.1 2.1 1.15 0.3


3.75 3.5 2.56 1.4 0.6


4 3.8 2.95 2 0.9


4.2 4 3.2 2.2 1.1]';


DF=@(x) sum(sum((z-(x(1)*X.^2./(x(2)*X.^2+x(3)*Y.^3+x(4)))).^2)); 


[coef,fval]=fminsearch(DF,[1,1,1,1]);


[X1,Y1]=meshgrid(0:20:500,0:20:200);


Fs=coef(1)*X1.^2./(coef(2)*X1.^2+coef(3)*Y1.^3+coef(4));


surf(X,Y,z,'FaceColor','none','Marker','.','MarkerSize',30,'MarkerFaceColor',[0 0 0])


hold on


surf(X1,Y1,Fs,'FaceAlpha',0.5,'FaceColor','interp') 

xlabel('x')


ylabel('y')


zlabel('z')


syms x y a b c d


Fitfun=a*x^2/(b*x^2+c*y^3+d);


disp('The fitting function is ')


pretty(Fitfun)


disp(['where a=',num2str(coef(1)),', b=',num2str(coef(2)),', c=',num2str(coef(3)),', d=',num2str(coef(4))])


disp(['The sum of least-square errors :',num2str(fval)])