Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to make input size the same as output in interp2

Subject: how to make input size the same as output in interp2

From: hanciong

Date: 25 Jan, 2013 17:34:08

Message: 1 of 5

Hello all, I have a problem. I have the following scipt to make 2D interpolation of some function fu(x,y) = x^2y-y^2x. There is no problem if both x and y are scalars. However, if I call fuint with x scalar and y row vector for example, the output is column vector. also, if I call fuint with x column vector and y scalar, the output is row vector. see the two lines in which I write comment %-->problem here. The problem is that, quadgk can't integrate @(y) fuint(3,y), because input size is not the same as output. how should I modify this such that the size of the output is the same as input, if either x or y is scalar? Thanx ==========================================

clear all

xcoba = linspace(-5,5,50); ycoba = linspace(-5,5,50);

fu = @(x,y) x.^2.*y - y.^2.*x;

[xgrid,ygrid] = ndgrid(xcoba,ycoba);

fugrid = fu(xgrid,ygrid);

fuint = @(x,y) interp2(xgrid',ygrid',fugrid',x,y,'cubic',NaN);

fuint(1,[2,3,4]) %--> problem here

fuint([2;3;4],1) %--> problem here

quadgk(@(y) fuint(3,y), -3,3)

=======================================

Subject: how to make input size the same as output in interp2

From: Bruno Luong

Date: 25 Jan, 2013 21:08:08

Message: 2 of 5

"hanciong" wrote in message <kdufmg$ha6$1@newscl01ah.mathworks.com>...
>how should I modify this such that the size of the output is the same as input, if either x or y is scalar? Thanx

You can't. When calling interp2(..., x, y); with the interpolation points x and y are scalar/vectors with different sizes, the inputs x and y are first expanded on grid as following:

[X Y] = meshgrid(x(:),y(:)) % x varying 2nd dimension and x first
z = interp2(..., X, Y);

Therefore
1) if x is scalar, the output is vertical regardless y;
2) if y is scalar, the output is horizontal regardless x.

This behavior is documented.

If you want to force the result to be horizontal or vertical vectors, you must expand x, y in the same size:

fuint([1 1 1],[2,3,4])
fuint([2;3;4],[1;1;1])

% Bruno

Subject: how to make input size the same as output in interp2

From: hanciong

Date: 7 Mar, 2013 20:37:08

Message: 3 of 5

But then what should I do if I want to integrate this interpolation function? The quad2d or integral2 or whatever make input of x and y arbitrarily; I can't force them to expand x input or y input to have same size.

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdus7o$9he$1@newscl01ah.mathworks.com>...
> "hanciong" wrote in message <kdufmg$ha6$1@newscl01ah.mathworks.com>...
> >how should I modify this such that the size of the output is the same as input, if either x or y is scalar? Thanx
>
> You can't. When calling interp2(..., x, y); with the interpolation points x and y are scalar/vectors with different sizes, the inputs x and y are first expanded on grid as following:
>
> [X Y] = meshgrid(x(:),y(:)) % x varying 2nd dimension and x first
> z = interp2(..., X, Y);
>
> Therefore
> 1) if x is scalar, the output is vertical regardless y;
> 2) if y is scalar, the output is horizontal regardless x.
>
> This behavior is documented.
>
> If you want to force the result to be horizontal or vertical vectors, you must expand x, y in the same size:
>
> fuint([1 1 1],[2,3,4])
> fuint([2;3;4],[1;1;1])
>
> % Bruno

Subject: how to make input size the same as output in interp2

From: Bruno Luong

Date: 7 Mar, 2013 22:14:08

Message: 4 of 5

"hanciong" wrote in message <khatpk$bpv$1@newscl01ah.mathworks.com>...
> But then what should I do if I want to integrate this interpolation function? The quad2d or integral2 or whatever make input of x and y arbitrarily; I can't force them to expand x input or y input to have same size.

But who force you to use anonymous function? Use in regular function, put interp2 followed by an appropriate reshape.

It is also not so smart to integrate an interpolation on grid by brute-force numerically integration. One can obtain the result directly from the grid values, if you are willing to make a little work beforehand. Or or lazier way of using summing Gauss point would be more economic.

Bruno

Subject: how to make input size the same as output in interp2

From: hanciong

Date: 4 May, 2013 10:48:09

Message: 5 of 5

Hello,

Could you check this: http://www.mathworks.com/matlabcentral/newsreader/view_thread/328759

This is the actual problem. Thank you for your time.

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <khb3fg$qi$1@newscl01ah.mathworks.com>...
> "hanciong" wrote in message <khatpk$bpv$1@newscl01ah.mathworks.com>...
> > But then what should I do if I want to integrate this interpolation function? The quad2d or integral2 or whatever make input of x and y arbitrarily; I can't force them to expand x input or y input to have same size.
>
> But who force you to use anonymous function? Use in regular function, put interp2 followed by an appropriate reshape.
>
> It is also not so smart to integrate an interpolation on grid by brute-force numerically integration. One can obtain the result directly from the grid values, if you are willing to make a little work beforehand. Or or lazier way of using summing Gauss point would be more economic.
>
> Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us