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:
problem with integral2, function not vectorized properly

Subject: problem with integral2, function not vectorized properly

From: hanciong

Date: 3 May, 2013 17:52:14

Message: 1 of 7

Hello all, I have a problem. Try to run the following code, and you will get a warning. For those of you who don't have integral2, replace all integral2 with quad2d. It will still produce the same warning.

Basically I want to integrate a function defined from two dimensional interpolation with interp2. But somehow, it produces a warning "function is not vectorized properly" that I don't know how to handle.

This is the code:

%%%%%%% BEGINNING OF CODE %%%%%%%%%%%

% Making vector interpolation of x and y
xcb = linspace(0,5,50);
ycb = linspace(0,5,40);

% Making grid of x and y
[xgr,ygr] = ndgrid(xcb,ycb);

% Define a function
fu = @(x,y) exp(-x./2-y./2);

% Making grid of z
zgr = fu(xgr,ygr);

% If you want to plot z(x,y), uncomment this line
surf(xgr,ygr,zgr)

% Making interpolation function of fu(x,y)
fuinttemp = @(x,y) interp2(xgr',ygr',zgr',x,y,'cubic',NaN);

% I reshape the result such that the output of fuint(x,y) always has the
% same size as y. This is because in the end, I will integrate fuint(x,y)
% with respect to y, while x is held constant. So the size of y varies, but
% x is constant (a number/scalar).
fuint = @(x,y) reshape(fuinttemp(x.*ones(size(y)),y),size(y));

% Define another function
fu2 = @(w,y) log(y.*w) + log(y+w);

% If you want, try to replace fuint here with fuinttemp (the
% one before reshaped), it will produce error when you want to evaluate fu3(x)
fu3 = @(x) integral2 ( @(y,w) fu2(w,y).*fuint(x,y), 1,4,1,4);

% Some examples:
disp(fu3(2))
disp(fu3(1))

% Why the error message???
% If I read the manual, you can produce the same error by doing this:
integral2(@(x,y) x+y(1), 0,1,0,1)

%%%%%%% END OF CODE %%%%%%%%%%

Thank you all

Subject: problem with integral2, function not vectorized properly

From: Steven_Lord

Date: 3 May, 2013 18:08:55

Message: 2 of 7



"hanciong " <hanciong@gmail.com> wrote in message
news:km0tge$2u$1@newscl01ah.mathworks.com...
> Hello all, I have a problem. Try to run the following code, and you will
> get a warning. For those of you who don't have integral2, replace all
> integral2 with quad2d. It will still produce the same warning.
>
> Basically I want to integrate a function defined from two dimensional
> interpolation with interp2. But somehow, it produces a warning "function
> is not vectorized properly" that I don't know how to handle.
> This is the code:

*snip*

> % If you want, try to replace fuint here with fuinttemp (the
> % one before reshaped), it will produce error when you want to evaluate
> fu3(x)
> fu3 = @(x) integral2 ( @(y,w) fu2(w,y).*fuint(x,y), 1,4,1,4);
>
> % Some examples:
> disp(fu3(2))
> disp(fu3(1))
>
> % Why the error message???
> % If I read the manual, you can produce the same error by doing this:
> integral2(@(x,y) x+y(1), 0,1,0,1)

The result of:

f = @(x, y) x+y(1)
M1 = magic(5);
M2 = 26-M1;
result1 = f(M1, M2)

does not match the results of:

result2 = zeros(size(M1));
for k = 1:numel(M1)
    result2(k) = f(M1(k), M2(k));
end
result2

The description of the INTEGRAL2 function's first input, the function to be
integrated, states:

http://www.mathworks.com/help/matlab/ref/integral2.html

"The function fun must accept two arrays of the same size and return an
array of corresponding values. It must perform element-wise operations."

The nth element of the output of your function should depend only on the nth
element of the first input and the nth element of the second. The function
"@(x, y) x+y(1)" does not satisfy this requirement when INTEGRAL2 checks it,
and so INTEGRAL2 warns. I strongly suspect that the function "@(y,w)
fu2(w,y).*fuint(x,y)" does not satisfy this requirement either.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: problem with integral2, function not vectorized properly

From: hanciong

Date: 4 May, 2013 10:46:09

Message: 3 of 7

Hi Steven,

Thank you for your help. So how do you suggest me to modify the function, so that I can integrate it? Thanx

"Steven_Lord" <slord@mathworks.com> wrote in message <km0ufm$382$1@newscl01ah.mathworks.com>...

> The description of the INTEGRAL2 function's first input, the function to be
> integrated, states:
>
> http://www.mathworks.com/help/matlab/ref/integral2.html
>
> "The function fun must accept two arrays of the same size and return an
> array of corresponding values. It must perform element-wise operations."
>
> The nth element of the output of your function should depend only on the nth
> element of the first input and the nth element of the second. The function
> "@(x, y) x+y(1)" does not satisfy this requirement when INTEGRAL2 checks it,
> and so INTEGRAL2 warns. I strongly suspect that the function "@(y,w)
> fu2(w,y).*fuint(x,y)" does not satisfy this requirement either.
>
 

Subject: problem with integral2, function not vectorized properly

From: hanciong

Date: 4 May, 2013 11:11:12

Message: 4 of 7

Hi Steven,

Thank you for pointing me in the right direction. I try this:

result1 = fuint(1,ytry); %fuint is the function from interp2 which is reshaped
disp(result1)

result2 = zeros(size(ytry));
for kr = 1:numel(result2)
    result2(kr) = fuint(1,ytry(kr));
end
disp(result2)

result1 and result2 are different! Thanx for pointing this to me. Now, I wonder how to correct this problem ..........

hanciong


"Steven_Lord" <slord@mathworks.com> wrote in message <km0ufm$382$1@newscl01ah.mathworks.com>...
>
>
> The description of the INTEGRAL2 function's first input, the function to be
> integrated, states:
>
> http://www.mathworks.com/help/matlab/ref/integral2.html
>
> "The function fun must accept two arrays of the same size and return an
> array of corresponding values. It must perform element-wise operations."
>
> The nth element of the output of your function should depend only on the nth
> element of the first input and the nth element of the second. The function
> "@(x, y) x+y(1)" does not satisfy this requirement when INTEGRAL2 checks it,
> and so INTEGRAL2 warns. I strongly suspect that the function "@(y,w)
> fu2(w,y).*fuint(x,y)" does not satisfy this requirement either.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: problem with integral2, function not vectorized properly

From: Bruno Luong

Date: 4 May, 2013 11:51:09

Message: 5 of 7

"hanciong" wrote in message <km0tge$2u$1@newscl01ah.mathworks.com>...
> Hello all, I have a problem. Try to run the following code, and you will get a warning. For those of you who don't have integral2, replace all integral2 with quad2d. It will still produce the same warning.
>
> Basically I want to integrate a function defined from two dimensional interpolation with interp2. But somehow, it produces a warning "function is not vectorized properly" that I don't know how to handle.
>
> This is the code:
>
> %%%%%%% BEGINNING OF CODE %%%%%%%%%%%
>
> % Making vector interpolation of x and y
> xcb = linspace(0,5,50);
> ycb = linspace(0,5,40);
>
> % Making grid of x and y
> [xgr,ygr] = ndgrid(xcb,ycb);
>
> % Define a function
> fu = @(x,y) exp(-x./2-y./2);
>
> % Making grid of z
> zgr = fu(xgr,ygr);
>
> % If you want to plot z(x,y), uncomment this line
> surf(xgr,ygr,zgr)
>
> % Making interpolation function of fu(x,y)
> fuinttemp = @(x,y) interp2(xgr',ygr',zgr',x,y,'cubic',NaN);
>
> % I reshape the result such that the output of fuint(x,y) always has the
> % same size as y. This is because in the end, I will integrate fuint(x,y)
> % with respect to y, while x is held constant. So the size of y varies, but
> % x is constant (a number/scalar).
> fuint = @(x,y) reshape(fuinttemp(x.*ones(size(y)),y),size(y));
>
> % Define another function
> fu2 = @(w,y) log(y.*w) + log(y+w);
>
> % If you want, try to replace fuint here with fuinttemp (the
> % one before reshaped), it will produce error when you want to evaluate fu3(x)
> fu3 = @(x) integral2 ( @(y,w) fu2(w,y).*fuint(x,y), 1,4,1,4);
>
> % Some examples:
> disp(fu3(2))
> disp(fu3(1))
>
> % Why the error message???

I don't get any error message, it displays

    3.1554

    5.2023

Now the other example integral2(@(x,y) x+y(1), 0,1,0,1) does not meet integral2 requirement for input function. So it is normal it throw out the error. Steve has already pointed out the error, and it seems pretty clear to me.

Bruno

Subject: problem with integral2, function not vectorized properly

From: hanciong

Date: 4 May, 2013 14:04:09

Message: 6 of 7

Hi Bruno,

what version of matlab are you using? in matlab 2012b, there is error. Anyway, I have found the problem. Could you take a look here:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/328769?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+mathworks%2FzAHF+%28MATLAB+Central+Newsreader+Recent+Posts%29

Apparently interp2 can accept matrices as inputs. I mean, the results of matrices as inputs and element by element calculation are different. Do you experience it too?

hanciong

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message >
> I don't get any error message, it displays
>
> 3.1554
>
> 5.2023
>
> Now the other example integral2(@(x,y) x+y(1), 0,1,0,1) does not meet integral2 requirement for input function. So it is normal it throw out the error. Steve has already pointed out the error, and it seems pretty clear to me.
>
> Bruno

Subject: problem with integral2, function not vectorized properly

From: hanciong

Date: 4 May, 2013 14:07:09

Message: 7 of 7

Hi Steven,

I get the problem. Apparently if I use interp2 with matrices as input, and with element by element calculation, the results are different. Could you take a look here:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/328769?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+mathworks%2FzAHF+%28MATLAB+Central+Newsreader+Recent+Posts%29

Btw what matlab version are you using? Because in some other version of matlab, there is no problem

Thanx

"Steven_Lord" <slord@mathworks.com> wrote in message
>
> The result of:
>
> f = @(x, y) x+y(1)
> M1 = magic(5);
> M2 = 26-M1;
> result1 = f(M1, M2)
>
> does not match the results of:
>
> result2 = zeros(size(M1));
> for k = 1:numel(M1)
> result2(k) = f(M1(k), M2(k));
> end
> result2
>
> The description of the INTEGRAL2 function's first input, the function to be
> integrated, states:
>
> http://www.mathworks.com/help/matlab/ref/integral2.html
>
> "The function fun must accept two arrays of the same size and return an
> array of corresponding values. It must perform element-wise operations."
>
> The nth element of the output of your function should depend only on the nth
> element of the first input and the nth element of the second. The function
> "@(x, y) x+y(1)" does not satisfy this requirement when INTEGRAL2 checks it,
> and so INTEGRAL2 warns. I strongly suspect that the function "@(y,w)
> fu2(w,y).*fuint(x,y)" does not satisfy this requirement either.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

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