Thread Subject:
Using quadl() in nested functions

Subject: Using quadl() in nested functions

From: Amykelly

Date: 5 Jul, 2012 16:56:08

Message: 1 of 4

I made the following nested functions trying to do integration in both primary and nested functions. Notice the integral from the nested function is in a loop. When I pull all the inputs of primary function "total" into the nested function"hw" and make "hw" an independent function, it runs with no problem. But when I add on an outer integral and impose this nesting structure to make "total', Matlab gives the error message afterwards. Could anybody shed light on what'd go wrong in the function? Many thanks in advance.

function ap=total(d,p,r,nu,al,phi) %all the inputs are scalars, except that phi is a vector.
ap=quadl(@hw,0,1/d)-al;
   function y=hw(x)
    y=0;
      for i=1:(p-1)
      two = @(w)acos(d.*x.*(w+1).^(1/2)).*w.^(r/2-2).*(w+1).^(-r/2);
      wmax=max(0,(cos(phi(i)/2)/(x*d))^2-1);
      inner=quadl(two,0,wmax);
      y=y+inner.*(r-2)./pi-phi(i)./pi.*fcdf(2.*wmax./(r-2),r-2,2);
      end
    y=p.*fcdf(((x.*d).^(-2)-1)./(r-1),r-1,1)-y;
    y=y.*fpdf(r*x^2,nu,r).*2.*r.*x;
    end
end

>> total(1,4,4,60,0.05,[0.5,0.6,0.7])
??? Error using ==> mldivide
Matrix dimensions must agree.

Error in ==> total>hw at 7
 wmax=max(0,(cos(phi(i)/2)/(x*d))^2-1);

Error in ==> quadl at 70
y = feval(f,x,varargin{:}); y = y(:).';

Error in ==> total at 2
ap=quadl(@hw,0,1/d)-al;

Subject: Using quadl() in nested functions

From: Star Strider

Date: 5 Jul, 2012 19:41:08

Message: 2 of 4

"Amykelly " <graduateymz@gmail.com> wrote in message <jt4gv8

-----------------------------------------------

To start, I suggest putting

> ap=quadl(@hw,0,1/d)-al;

after the end of

> function y=hw(x)

and see how it works.

Subject: Using quadl() in nested functions

From: Amykelly

Date: 5 Jul, 2012 21:46:08

Message: 3 of 4

"Star Strider" wrote in message <jt4qkk$bqb$1@newscl01ah.mathworks.com>...
> "Amykelly " <graduateymz@gmail.com> wrote in message <jt4gv8
>
> -----------------------------------------------
>
> To start, I suggest putting
>
> > ap=quadl(@hw,0,1/d)-al;
>
> after the end of
>
> > function y=hw(x)
>
> and see how it works.
Hi,
I got the exactly same error message, by putting
ap=quadl(@hw,0,1/d)-al;
in the end.

Subject: Using quadl() in nested functions

From: Star Strider

Date: 6 Jul, 2012 15:19:17

Message: 4 of 4

"Amykelly " <graduateymz@gmail.com> wrote in message <jt51v0$a3l$1@newscl01ah.mathworks.com>...
> "Star Strider" wrote in message <jt4qkk$bqb$1@newscl01ah.mathworks.com>...
> > "Amykelly " <graduateymz@gmail.com> wrote in message <jt4gv8
> >
> > -----------------------------------------------
> >
> > To start, I suggest putting
> >
> > > ap=quadl(@hw,0,1/d)-al;
> >
> > after the end of
> >
> > > function y=hw(x)
> >
> > and see how it works.
> Hi,
> I got the exactly same error message, by putting
> ap=quadl(@hw,0,1/d)-al;
> in the end.

---------------------------------------------------------------------

It was worth a try, since I'm not sure how MATLAB compiles its nested functions. My hypothesis was that 'ap' needed to have its arguments declared first.

I just now noticed you use 'i' for your loop index. It's a small point, but I suggest you not use 'i' and 'j' for loop indices, since MATLAB also uses them for its imaginary operators. (I use 'k1', 'k2', etc. to avoid this problem.)

Since your code worked before it was part of 'total', something about putting it into a function changed its behavior. (Function variables are local to them and are generally not shared with the outside world.) I suggest you be sure all the parameters are getting passed correctly and that 'wmax' is getting the arguments you believe it's getting. I suspect that 'phi' is being treated in 'wmax' as a vector for some reason — perhaps because of the 'i' subscript — and since 'wmax' is causing the 'mldivide' error, that could be the problem.

I don't know if setting a breakpoint at 'wmax' would let you examine what 'wmax' is calculating in detail [letting you see only 'phi(i)' as opposed to 'phi(:)'], so if that doesn't work, it might be worthwhile to introduce a variable such as:

phidisp = phi(i)

just before 'wmax' (and without the trailing semicolon so it will display) to see if that's the problem.

I don't see anything else that could possibly be wrong with your code if it worked when it wasn't part of 'total'. So I suggest you change you loop indices to something other than 'i' or 'j' for a start, and if that doesn't solve the problem, add 'phidisp' to see if 'total' is treating it there as a vector or a scalar.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us