0

I am trying to use 2D polynomial fitting for my table data, my data format is exactly like the link below: http://www.mathworks.de/help/toolbox/curvefit/brx2ldg-1.html#bso46rp-1

I mean I have vector X with length n, Y with length m and m*n Matrix Z, I would like to fit 5 degree 2d polynomial to my data,

I am wondering that is there any syntax in MATLAB for solving this problem? like SFIT command in IDL, http://star.pst.qub.ac.uk/idl/SFIT.html

I have cftool and sftool, but it seems that they don't work for this purpose or most probably I don't know how to employ them.

and I know that there some m.file which people share in MATLAB Mathworks file exchange, please if you know one works properly, suggest me.

I'd appreciate any help and comment.

user1331843
  • 123
  • 1
  • 6
  • 15
  • I am also trying to employ Sfit in IDl, but I couldn't find hot to use it, I'd appreciate any help. – user1331843 Sep 07 '12 at 15:06
  • Maybe the answers here are of additional help http://stackoverflow.com/questions/2963874/fitting-two-dimensional-curves-in-matlab – gevang Sep 11 '12 at 16:09

1 Answers1

2

You can use polyfitn from file exchange and re-format your data in order to have 3 MxN x,y,z vectors.

Example:

Assume you have a table data of the form

N = 100; M = 80;
x = linspace(0, 1, M);
y = linspace(0, 1, N).';
z = bsxfun(@franke, x, y);

Create meshgrid for x, and y instead

N = 100; M = 80;
[x, y] = meshgrid(0:1:N, 0:1:M);
z = bsxfun(@franke, x, y);

(Note that unique(x) and unique(y) will give you the original linspace values of your table rows and columns.)

Use polyfitn to get the 5-degree polynomial coefficients

p = polyfitn([x(:),y(:)], z(:), 5);

You can additionally convert the result into a symbolic form to view the polynomial using the provided polyn2sym(p)

>> pretty(polyn2sym(p))

                      5                           4                             4                         3   2
     90264379051097 X1         2537627280433653 X1  X2       7778045812403061 X1       6982058230382053 X1  X2
- ------------------------- - -------------------------- + ------------------------ - -------------------------- + ...
  2417851639229258349412352   38685626227668133590597632   604462909807314587353088   77371252455336267181195264
gevang
  • 4,994
  • 25
  • 33
  • but I have x, y vectors, and z as a martix – user1331843 Sep 18 '12 at 15:57
  • yes, in my answer I assume you have that (lines 1-4 of code) and suggest how you can get to a convenient all-matrix representation from it (lines 5-7). In essence create a `meshgrid` from your x,y vectors and leave matrix z intact. `polyfitn` then can be applied as shown. – gevang Sep 18 '12 at 19:17
  • I face with this error; which I don't know why, I put my data to my question. could you plz help me? ??? Error using ==> horzcat CAT arguments dimensions are not consistent. – user1331843 Sep 26 '12 at 11:24
  • 1
    the only place you could be getting that error is in `[x(:),y(:)]` inside `polyfitn`. Remember, x, y are originally vectors, which you turn into matrices, by `meshgrid`. Then, irrespective of their original length, doing `[x(:),y(:)]`, i.e. concatenating the vector-turned matrices, will work. – gevang Sep 26 '12 at 16:21