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:
function handle, cellfun, prod, short simple question

Subject: function handle, cellfun, prod, short simple question

From: leo nidas

Date: 10 May, 2012 00:19:14

Message: 1 of 7


Hi there,


I am working with a complicated function using cells and then convert into a function handle. Here I provide an arbitrary small example which will hopefully help me to continue if I undestand it.

Below I provide the same function (cc) in three different ways. At least I think so (and want so).

The different syntax refer to the prod thing. Please comment out 2 out of the three cc functions and run the program. My question is why the first way yields different result from the third? And why the second won't work? Have I done something terribly wrong?

     cc={@(g) dblquad(@(b0,b1) (g(1).*b1+g(2).^(b0)).*(prod( [b0+2, b0+3] )) ,0,1,0,1) } %this is too slow
    % cc={@(g) dblquad(@(b0,b1) (g(1).*b1+g(2).^(b0)).*(prod( b0+[2 3] )) ,0,1,0,1) } %this does not work
     cc={@(g) dblquad(@(b0,b1) (g(1).*b1+g(2).^(b0)).*((b0+2).*(b0+3)) ,0,1,0,1) } %this is ok in terms of speed but disagrees with the first one
     
     L=@(g) -sum(log(cellfun(@(f) f(g), cc )))
     L([1 1 ])

    

Why is that? What am I doing wrong? I would expect that all three would work and yield the same result. In any case I am interested in using something like the second way because instead of the vector [2 3] I may have a random vector with random number of elements let a, so I would simply plug in prod( b0+[a] ).

Any thoughts?

Thanx in advance for any answers!

Subject: function handle, cellfun, prod, short simple question

From: Roger Stafford

Date: 10 May, 2012 01:51:19

Message: 2 of 7

"leo nidas" <bleonidas25@yahoo.gr> wrote in message <jof1i2$cfh$1@newscl01ah.mathworks.com>...
> .......
> cc={@(g) dblquad(@(b0,b1) (g(1).*b1+g(2).^(b0)).*(prod( [b0+2, b0+3] )) ,0,1,0,1) } %this is too slow
> % cc={@(g) dblquad(@(b0,b1) (g(1).*b1+g(2).^(b0)).*(prod( b0+[2 3] )) ,0,1,0,1) } %this does not work
> cc={@(g) dblquad(@(b0,b1) (g(1).*b1+g(2).^(b0)).*((b0+2).*(b0+3)) ,0,1,0,1) } %this is ok in terms of speed but disagrees with the first one
> ........
- - - - - - - - -
  Leo, you should be aware that 'dblquad' expects your integrand function to be able to accept a vector for the b0 variable. In your first version you will get a wrong answer if that vector is a column vector since the product will by default be taken column-wise. You should have

 "prod([b0+2,b0+3],2)"

instead. In case b0 is a row vector it will be wrong in any case. In the second version the expression b0+[2 3] is simply invalid syntax since [2 3] cannot be added to b0 unless b0 is a scalar or is 1 by 2 in size. Version 3 looks right.

Roger Stafford

Subject: function handle, cellfun, prod, short simple question

From: Roger Stafford

Date: 10 May, 2012 03:45:24

Message: 3 of 7

"leo nidas" <bleonidas25@yahoo.gr> wrote in message <jof1i2$cfh$1@newscl01ah.mathworks.com>...
> cc={@(g) dblquad(@(b0,b1) (g(1).*b1+g(2).^(b0)).*((b0+2).*(b0+3)) ,0,1,0,1) } %this is ok in terms of speed but disagrees with the first one
- - - - - - - - - - -
  By the way, there is no need to do double integration on the integrand you defined. In fact there is no need even to solve it numerically since there is an easy, direct solution using elementary calculus. The answer I get is:

 53/12*g(1) + (g(2)*(12*k^2-7*k+1)-(6*k^2-5*k+1))/k^3

where k = log(g(2)) (if my pen and paper scratchings are correct.)

  You should be able to use the symbolic toolbox with 'int' to get the solution in your contemplated more general product case.

Roger Stafford

Subject: function handle, cellfun, prod, short simple question

From: Roger Stafford

Date: 10 May, 2012 05:59:08

Message: 4 of 7

"Roger Stafford" wrote in message <jofdkk$e$1@newscl01ah.mathworks.com>...
> 53/12*g(1) + (g(2)*(12*k^2-7*k+1)-(6*k^2-5*k+1))/k^3
>
> where k = log(g(2)) (if my pen and paper scratchings are correct.)
- - - - - - - -
  I am sorry to report that my "scratchings" were not quite accurate. Using 'int' gives the correct integral:

 53/12*g(1) + (g(2)*(12*k^2-7*k+2)-(6*k^2-5*k+2))/k^3

Roger Stafford

Subject: function handle, cellfun, prod, short simple question

From: leo nidas

Date: 10 May, 2012 07:04:13

Message: 5 of 7

"Roger Stafford" wrote in message <joflfc$su0$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <jofdkk$e$1@newscl01ah.mathworks.com>...
> > 53/12*g(1) + (g(2)*(12*k^2-7*k+1)-(6*k^2-5*k+1))/k^3
> >
> > where k = log(g(2)) (if my pen and paper scratchings are correct.)
> - - - - - - - -
> I am sorry to report that my "scratchings" were not quite accurate. Using 'int' gives the correct integral:
>
> 53/12*g(1) + (g(2)*(12*k^2-7*k+2)-(6*k^2-5*k+2))/k^3
>
> Roger Stafford


"Roger Stafford" wrote in message <joflfc$su0$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <jofdkk$e$1@newscl01ah.mathworks.com>...
> > 53/12*g(1) + (g(2)*(12*k^2-7*k+1)-(6*k^2-5*k+1))/k^3
> >
> > where k = log(g(2)) (if my pen and paper scratchings are correct.)
> - - - - - - - -
> I am sorry to report that my "scratchings" were not quite accurate. Using 'int' gives the correct integral:
>
> 53/12*g(1) + (g(2)*(12*k^2-7*k+2)-(6*k^2-5*k+2))/k^3
>
> Roger Stafford


Thanx for your answers,

I know that this can be solved analytically, I randomly typed an expression there. This is just an example of a greater problem I have which cannot be solved analytically and want to do something like above.

So, I think it is not clear to me how should I form the cc so that is does the third calculation given a row vector a?? I mean that I cannot simply type in

(b0+a(1))*(b0+a(2))*(b0+a(3))... and so on.

That is why I tried using prod. (For this specific example a=[2 3]). I hope I am clear in what I want to calculate.

Subject: function handle, cellfun, prod, short simple question

From: Steven_Lord

Date: 10 May, 2012 13:40:30

Message: 6 of 7



"leo nidas" <bleonidas25@yahoo.gr> wrote in message
news:jofp9d$e1q$1@newscl01ah.mathworks.com...
> "Roger Stafford" wrote in message
> <joflfc$su0$1@newscl01ah.mathworks.com>...
>> "Roger Stafford" wrote in message
>> <jofdkk$e$1@newscl01ah.mathworks.com>...
>> > 53/12*g(1) + (g(2)*(12*k^2-7*k+1)-(6*k^2-5*k+1))/k^3
>> >
>> > where k = log(g(2)) (if my pen and paper scratchings are correct.)
>> - - - - - - - -
>> I am sorry to report that my "scratchings" were not quite accurate.
>> Using 'int' gives the correct integral:
>>
>> 53/12*g(1) + (g(2)*(12*k^2-7*k+2)-(6*k^2-5*k+2))/k^3
>>
>> Roger Stafford
>
>
> "Roger Stafford" wrote in message
> <joflfc$su0$1@newscl01ah.mathworks.com>...
>> "Roger Stafford" wrote in message
>> <jofdkk$e$1@newscl01ah.mathworks.com>...
>> > 53/12*g(1) + (g(2)*(12*k^2-7*k+1)-(6*k^2-5*k+1))/k^3
>> >
>> > where k = log(g(2)) (if my pen and paper scratchings are correct.)
>> - - - - - - - -
>> I am sorry to report that my "scratchings" were not quite accurate.
>> Using 'int' gives the correct integral:
>>
>> 53/12*g(1) + (g(2)*(12*k^2-7*k+2)-(6*k^2-5*k+2))/k^3
>>
>> Roger Stafford
>
>
> Thanx for your answers,
>
> I know that this can be solved analytically, I randomly typed an
> expression there. This is just an example of a greater problem I have
> which cannot be solved analytically and want to do something like above.
>
> So, I think it is not clear to me how should I form the cc so that is does
> the third calculation given a row vector a?? I mean that I cannot simply
> type in
>
> (b0+a(1))*(b0+a(2))*(b0+a(3))... and so on.
>
> That is why I tried using prod. (For this specific example a=[2 3]). I
> hope I am clear in what I want to calculate.

At some point, the calculations become complicated enough that while you
_could_ write them as an anonymous function, it would be clearer IMO to
write them as a regular function.


% begin leotest.m
function cc = leotest
%
% Call this as: cc = leotest;
cc = @(g) dblquad(@(b0,b1) myfun(g, b0, b1) ,0,1,0,1);

function y = myfun(g, b0, b1)
y = (g(1).*b1+g(2).^(b0));
for k = [2 3]
    y = y.*(b0+k);
end
% end leotest.m


This looks to be in agreement with Roger's answer, and should be easier to
debug if necessary (since you can set a breakpoint on a particular line of
myfun to ensure that it's evaluating your integrand correctly.)

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

Subject: function handle, cellfun, prod, short simple question

From: Roger Stafford

Date: 10 May, 2012 13:56:07

Message: 7 of 7

"leo nidas" <bleonidas25@yahoo.gr> wrote in message <jofp9d$e1q$1@newscl01ah.mathworks.com>...
> So, I think it is not clear to me how should I form the cc so that is does the third calculation given a row vector a?? I mean that I cannot simply type in
>
> (b0+a(1))*(b0+a(2))*(b0+a(3))... and so on.
>
> That is why I tried using prod. (For this specific example a=[2 3]). I hope I am clear in what I want to calculate.
- - - - - - - - - -
  In their documentation Mathworks states only that in 'dblquad', "fun(x,y) must accept a vector x" without committing themselves as to which kind of vector, column or row, would be sent. For that reason it would probably be prudent to use this expression:

 prod(bsxfun(@plus,b0(:),a),2)

for your product where 'a' is given as a row vector. On the other hand if you were sure it was always a column vector, the "(:)" after 'b0' would be unnecessary.

  You should bear in mind that if your integrand is separable as was the case in your example, it would be unnecessary to use 'dblquad' since the double integral could then be expressed in terms of separate single integrals.

Roger Stafford

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