Surface fit approximation for x,y and z data

17 Ansichten (letzte 30 Tage)
Alessandro Maria Laspina
Alessandro Maria Laspina am 12 Mär. 2021
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 Kommentare
Alessandro Maria Laspina
Alessandro Maria Laspina am 13 Mär. 2021
Bearbeitet: Alessandro Maria Laspina am 13 Mär. 2021
@Cris LaPierre No the Z values are completely different. Z is a function of the variables X and Y. Unless by correspond to you mean that those X and Y values correspond to the value of Z at the equivalent row and column index.
And I need to interpolate no matter what. I only have a limited dataset of Z values for given X and Y, I do not have more than the given points. I need to find Z for a different subset of x and y values within the set of values I already have.
@Matt J my mistake, I repeated a,b,c
Alessandro Maria Laspina
Alessandro Maria Laspina am 21 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Cris LaPierre
Cris LaPierre am 13 Mär. 2021
Since your data appears to already be gridded, I'd suggest using interp3.
  3 Kommentare
Cris LaPierre
Cris LaPierre am 15 Mär. 2021
I don't see how using interp2 meets all your requirements.
Alessandro Maria Laspina
Alessandro Maria Laspina am 20 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Fit Postprocessing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by