Surface fit approximation for x,y and z data

7 views (last 30 days)
I have an x and y vector, of sizes n and m respectively. I also have a z matrix of size n by m.
I am trying to do one of the following, preferably both:
  • interpolate for given x and y values to find the value of z
  • find a 2D function with inputs x and y using fit
In either scenarios, I am not sure whether I can do this since my x and y's are of different sizes. For the second option, which isn't as straight forward, I have the following code:
fitType=fittype(@(a,b,c,d,e,f,g,h,i,x,y) ...
(a+b*x+c*x^2)+(d+e*x+f*x^2)*sin(y)+(g+h*x+i*x^2)*cos(y));
fitZ=fit([M,aoa],Z,fitType,'StartPoint',ones(1,9));
where my fit function, as seen in the code, is: (a+b*x+c*x^2)+(d+e*x+f*x^2)*sin(y)+(g+h*x+i*x^2)*cos(y). The constants a,b,c,d,e,f,g,h,i are the one which I want fit to find me. Unfortunately, this does not work for different sized arrays.
EDIT: I've found a solution that may work but it turns out I cannot call the independent variables as [M,aoa]. The code is now:
myfittype = fittype('(a+b*Mx+c*Mx^2)+(d+e*Mx+f*Mx^2)*sin(aoax)+(g+h*Mx+ii*Mx^2)*cos(aoax)',...
'dependent',{'Z'},'independent',{'Mx','aoax'},...
'coefficients',{'a','b','c','d','e','f','g','h','ii'});
[Mmesh,aoamesh]=meshgrid(M,aoa);
fitCL=fit([M,aoa],Z,myfittype);
This returns the following error:
Operator '<' is not supported for operands of type 'fittype'.
Error in fit (line 7)
if dim_x < m
  4 Comments
Alessandro Maria Laspina
Alessandro Maria Laspina on 21 Mar 2021
I need to correct something, the reason why I said I hav to interpolate no matter what is because my x and y data is not of the same size. This isn't difficult, and I've managed to do this via:
[X,Y]=meshgrid(x,y);
Zint=interp2(x,y,Z',X,Y);
xeq=repmat(Mint,1,prod(size(Z))/length(y))';
yeq=repmat(aoa,1,prod(size(Z))/length(y))';
where x and y are sizes n and m respectively, and Z is size nxm.
Now my objective is to (hopefully) find an approximation to this interpolation. How? Well my idea is that because Y are angles between 0 and 180 degrees, and X is a dimensionless value (Z is also dimensionless), I want to fit the data to this:
f=@(a,x) a(1)+a(2).*x(:,1)+a(3).*x(:,1).^2+...
(a(4)+a(5).*x(:,1)+a(6).*x(:,1).^2).*sind(x(:,2))+...
(a(7)+a(8).*x(:,1)+a(9).*x(:,1).^2).*cosd(x(:,2));%+...
a is a vector containing the coefficients. x is a vector that is (nxm)x2 long, where the first column are the repetitions of x and y. This is not what I posted before, because that method is giving me a method. But with this formulation, I found that using lsqcurvefit I can obtain an approximation by:
options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt');
lb=[];
ub=[];
[Zfit,resnorm,residual,exitflag,output]=lsqcurvefit(f,ones(1,9)',[X(:) Y(:)],Z(:),lb,ub,options);
Whats the problem with this? Well I get a resnorm value of over 635.8113. You can find the x,y,z (single column data) and X,Y,Z (meshgridded data) in the file attached.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 13 Mar 2021
Since your data appears to already be gridded, I'd suggest using interp3.
  3 Comments
Alessandro Maria Laspina
Alessandro Maria Laspina on 20 Mar 2021
I have x, y, and z data. So my set is 3 dimensional, not 4 dimensional. I have query points for x and y, and have to fiind the equivalent values for z.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!