23 views (last 30 days)

I have a Matrix. The colums represent the X values and the rows represent the Y values and the matrix element Z represents the value at (X,Y). So, in my case, X runs from 1 to 250 and Y runs from 1 to 3500. I would like to fit an equation (I suspect that the data is going to be of high order *say 50) so maybe spline?) such that Z=f(X,Y) where Z is the matrix element

Example :

Say I choose X=10 and Y=2300 in the matrix. The matrix will have a particular value Z (say 30000) at (10,2300). Now I want to fit a equation for Z as a function of X and Y. What do I do?

Bjorn Gustavsson
on 25 Nov 2019

What you should do depends on your objective. If you very explicitly want a function you could use spap2 (and perhaps some of its siblings). It is a least-square-fitting spline function with 2-D capabilities.

If you can do all of the function evaluations at once you might be better off with interp2 (your data seems to be on a plaid grid), or griddata, scatteredInterp, triscatteredinterp functions (the latter might give you approximately your required function output), Otherwise there is always the gridfit function on the file exchange.

HTH

Bjorn Gustavsson
on 3 Dec 2019

It was a "reasonably long time ago" I used fitting splines, but I remember that you could generate a function-call for evaluation of the spline at arbitrary points. After some snoping aroung I managed to find: fnval for you. If you check the help for that function you should be able to get to a function-call. Perhaps something like this:

knotsx = augknt(linspace(x_r(1), x_r(end), 2000), 4);

knotsy = augknt(linspace(y_r(1), y_r(end), 200), 4);

bsp2 = spap2({knotsx,knotsy},[4 4], {x_r,y_r},f);

[X,Y] = meshgrid(x_roi,y_roi); % Yeah, yeah...

your_fun = @(x,y) fnval(bsp2,{x(:),y(:)});

Z = Y;

Z(:) = your_fun(X,Y);

Completely untested (TM)

HTH

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.