How to make input size the same as output in interp2

1 回表示 (過去 30 日間)
hanciong
hanciong 2013 年 1 月 25 日
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)
=======================================

回答 (2 件)

John Petersen
John Petersen 2013 年 1 月 25 日
you can make the scalar into an array with
X = x*ones(1,length(y));

Jan
Jan 2013 年 1 月 25 日
編集済み: Jan 2013 年 1 月 28 日
Alternatively:
x = repmat(x, 1, length(y));
or
x = x(ones(1, length(y));

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by