-3

I'm not sure what it means in this context. I tried adding " 'uniformoutput',false " to the end of arrayfun, but then it got upset with the "+" operator saying "Undefined operator '+' for input arguments of type 'cell'." I changed it to ".+" but got a parse error :( What am doing wrong?

Here is an image of the part that is broken and the error. The entire code is below in case someone would like to try running it or copy the broken part.

enter image description here

the whole code:

    function gbp2(zi,zf)

global beam xlist ylist wi qi Ri wf qf Rf Psii Psif x n
beam = struct('E',[],'lambda',[],'w',[],'R',[],'q',[],'a',[]);

E = 1;                              % electric field
lambda = 1064*10^-9;                % wavelength
k = 2*pi/lambda;                    % wave number
wi = 10^-3;                         % initial waist width (minimum spot size)
zr = (pi*wi^2)/lambda;              % Rayleigh range
Ri = zi + zr^2/zi;
qi = 1/(1/Ri-1i*lambda/(pi*wi^2));  % initial complex beam parameter
Psii = atan(real(qi)/imag(qi));     % Gouy phase
mat = [1 zf; 0 1];                  % transformation matrix
A = mat(1,1); B = mat(1,2); C = mat(2,1); D = mat(2,2);
qf = (A*qi + B)/(C*qi + D);
wf = sqrt(-lambda/pi*(1/imag(1/qf)));
Rf = 1/real(1/qf);

u = @(z, coor, mode, w, R, Psi) (2/pi)^(1/4)*sqrt(exp(1i*(2*mode+1)*Psi)/(2^mode*factorial(mode)*w))*...
            hermiteH(mode,sqrt(2)*coor/w)*exp(-coor^2*(1/w^2+1i*k/(2*R))-1i*k*z);

% -------------------- ERROR IN THIS PIECE (below) ----------------------------
xlist = containers.Map('KeyType','double','ValueType','any');
ylist = containers.Map('KeyType','double','ValueType','any');

    function pts(z, w, R, Psi)
        xlist(z) = -2*w:10^-4:2*w;
        ylist(z) = zeros(1,size(xlist(z),2));
        for mode = 0:2:10
            ylist(z) = ylist(z) + arrayfun(@(coor) u(z, coor, mode, w, R, Psi),xlist(z),'uniformoutput',false);
        end
    end

pts(zi,wi,Ri,Psii)
pts(zf,wf,Rf,Psif)
plot(xlist(zi),ylist(zi),xlist(zf),ylist(zf)) end

I tried writing a similar but simpler function and it seems to work just fine:

function test(zi, zf)

u = @(z,coor,mode) z*mode + integral(@(coor)coor,0,1);
xlist = containers.Map('KeyType','double','ValueType','any');
ylist = containers.Map('KeyType','double','ValueType','any');

    function pts(z)
        xlist(z) = -5:5;
        ylist(z) = zeros(1,size(xlist(z),2));
        for mode = 0:2:10
            ylist(z) = ylist(z) + arrayfun(@(coor) u(z,coor,mode),xlist(z));
        end
    end

pts(zi)
pts(zf)
plot(xlist(zi),ylist(zi),xlist(zf),ylist(zf))

end

so I'm not sure what the problem is with my code.

Raksha
  • 1,572
  • 2
  • 28
  • 53
  • 1
    You write, `@(x) u(z, coor, mode, w, R, Psi)`, where is `x` here? Anyway, your question is unclear and I feel too many unnecessary details have been put. It would be good if you can just put the part which is producing the error. Or construct a similar but smaller example which describes the problem. – Autonomous May 23 '15 at 01:23
  • @ParagS.Chandakkar , 'x' gets put in for the 'coor' variable when I call pts(zi,x,n,wi,Ri,Psii) on the very bottom. I put the part which is producing the error in the image. The error appears to be on line 30. I just put the whole code in in case someone would want to run it to see if it works. Sorry it's a bit long. And I did construct an example, but it worked XD ... so it's something specific to the original code, and i don't know what it is... – Raksha May 23 '15 at 01:34
  • pls, google Matlab arrayfun and read again!!! Parag said very clearly: where is `x` in `@(x) u(z, coor, mode, w, R, Psi)`? More detailed: `@(x)` tells Matlab you have a `running parameter named x` in the function `u`. So you have to put `x` as one of the parameter in `u` like: `@(x) u(z, x, mode, w, R, Psi)`. – scmg May 23 '15 at 01:43
  • but pls, do google and try to understand what you are using before asking – scmg May 23 '15 at 01:44
  • @scmg, you're right, sorry, I'm getting all tangled up in all the variables. I changed it to coor as that is indeed what the function depends on, like you said. But it hasn't gotten rid of the error unfortunately. I'm just getting confused by the order of events here I think. I changed the sample function too. Do you have any more tips? – Raksha May 23 '15 at 02:08
  • 1
    you got the same error message? since you have set `uniformoutput` to `false`, i suppose there should be another error. And you should change the run-variable of for-loop (`mode`), since `mode` is also the input argument, and this could make problems – scmg May 23 '15 at 04:24
  • Give us sample inputs so that we can run the function `gbp2(zi,zf)`. Just by looking at the code, its impossible to find the flaw. – Autonomous May 23 '15 at 04:32
  • @ParagS.Chandakkar, i usually test with `zi=0; zf =1`, but they can be any number. – Raksha May 23 '15 at 17:06
  • @scmg, you mean the mode in the `for mode = 0:2:10`? Those are the inputs, aren't they? mode = 0; mode = 2, ..., mode = 10. It's not just an iteration variable, I actually want those numbers to be plugged into the function. If that's what you mean. – Raksha May 23 '15 at 17:08
  • do you know how to write a for-loop? it should be `for i = 0:2:mode` with `mode = 10` as input parameter. – scmg May 23 '15 at 17:17
  • @scmg ... got rid of additional inputs, so there shouldn't be any more confusion – Raksha May 23 '15 at 17:37

1 Answers1

2

The error message give you a big hint where to look:

Undefined operator '+' for input arguments of type 'cell'.

Error in gbp2/pts (line 36)
                   ylist(z) = ylist(z) + arrayfun(@(coor) u(z, coor, mode, w, R,
                   Psi),xlist(z).','uniformoutput',false);

From the documentation for arrayfun and the 'UniformOutput' option:

Requests that the arrayfun function combine the outputs into cell arrays B1,...,Bm. The outputs of function func can be of any size or type.

Indeed, if you check, the output of this line is a cell array:

arrayfun(@(coor) u(z, coor, mode, w, R, Psi),xlist(z),'uniformoutput',false)

You can't sum the values from the cell array directly. Here's one of several ways you can do this:

v = arrayfun(@(coor) u(z, coor, mode, w, R, Psi),xlist(z),'uniformoutput',false);
ylist(z) = ylist(z) + [v{:}];

However, I don't see why you need to used the 'UniformOutput' option or even the slow arrayfun at all. Just vectorize your function with respect to coor:

u = @(z, coor, mode, w, R, Psi)(2/pi)^(1/4)*sqrt(exp(1i*(2*mode+1)*Psi)/(2^mode*factorial(mode)*w))*...
        hermiteH(mode,sqrt(2)*coor/w).*exp(-coor.^2*(1/w^2+1i*k/(2*R))-1i*k*z);

Now

ylist(z) = ylist(z) + u(z, xlist(z), mode, w, R, Psi);


Some additional suggestions: Don't use global variables – they're inefficient and there are almost always better solutions. If pts is meant to be a nested function, you're missing a closing end for the main gbp2 function. It might be a good idea to rename your variable mode so that it doesn't overload the built-in function of the same name. Psif isn't defined. And zeros(1,size(xlist(z),2)) can be written simply as zeros(size(xlist(z))).

Community
  • 1
  • 1
horchler
  • 18,384
  • 4
  • 37
  • 73
  • I'm a little worried about vectorizing coor as far as the hermiteH function. I replaced both `u` and `ylist(z)` as you suggested, but now i'm getting the `Error using * Inner matrix dimensions must agree.` error and errors in both of those lines. I tried replacing `*` inside the `hermiteH` with `.*`, but error was still there, even after I replaced all operators with matrix operators in `u`. – Raksha May 23 '15 at 19:41
  • @Solarmew as i suggested you in last comments, pls do google and try to understand what you are using at first. The error messages told you very clearly where does the problem occurs. This question is a case, where you asked us to become your debugger!!! – scmg May 23 '15 at 19:47
  • @scmg, I understand your confusion. However, I am still trying to understand how all these functions work and I may need some help in addition to googling. Please refrain from commenting here as to save room for useful and helpful comments. I hope you understand. Thank you. – Raksha May 23 '15 at 19:50
  • @horchler , welp ... it worked ... all I did was remove all of global variables ... didn't change `u`, didn't change `ylist`, just deleted that line of globals ... what have we learned? Nothing ... I hate MatLab, it make no friggin sense. But you, you I love :] You're great. Where can I nominate you for mod of the month? :] EDIT: changed `u` and `ylist` as per your suggestions. Still works and seems to run faster. Guess I'll just never use global ever again %\ ... – Raksha May 23 '15 at 20:13
  • 2
    @Solarmew: glad that you got it to work. Another problem with global variables is that they can cause also sorts of unintended side effects if you're not careful. As far as debugging goes, in the future I'd suggest printing out each line and carefully narrowing down the issue, or better, learning how to use the [Matlab debugger](http://www.mathworks.com/help/matlab/debugging-code.html). – horchler May 24 '15 at 00:47