-2

I am trying to store function handles in array by running a loop and then adding them.

for i = 1:n^2
    x1 = x_coord(elements(i,1));
    x2 = x_coord(elements(i,2));
    x3 = x_coord(elements(i,3));
    x4 = x_coord(elements(i,4));
    y1 = y_coord(elements(i,1));
    y2 = y_coord(elements(i,2));
    y3 = y_coord(elements(i,3));
    y4 = y_coord(elements(i,4));

    SF(elements(i,1)) = @(x,y)((x-x3)*(y-y3)/((x1-x3)*(y1-y3)) + SF(elements(i,1)(x,y))
end

Here I am saving function handles in array SF and then adding them inside the loop. But I am getting this error:

Error: ()-indexing must appear last in an index expression.

PLEASE HELP.

jabaldonedo
  • 25,822
  • 8
  • 77
  • 77
Avi Shri
  • 1
  • 1
  • What does `whos elements` return? Usually `A(m,n)(x,y)` is not valid MATLAB syntax. – Oleg Jun 06 '13 at 11:08
  • elements is a matrix which return an integer. What syntax should i use for adding function handles ?? – Avi Shri Jun 06 '13 at 11:16
  • 1
    @AviShri What are you trying to accomplish with this loop anyway? Even if it were correct, what is the point of adding `SF(elements(i, 1)` to another function handle? What sense do you make of it? – Eitan T Jun 06 '13 at 12:07
  • doing multi-scale modelling (Finite Element Analysis) so i need to define Shape functions for each node.I have to use step-up function so that I could define function piecewise for different domains. – Avi Shri Jun 06 '13 at 20:27

1 Answers1

1

When trying something this complicated, break it down and try smaller pieces. For example, when I run this at the command line:

F(1) = @(x,y)(x+y);
F(2) = @(x,y)(x+y+2);

I get the following error message: Nonscalar arrays of function handles are not allowed; use cell arrays instead.

Oops. I guess you need this instead:

F{1} = @(x,y)(x+y);
F{2} = @(x,y)(x+y+2);

And now, sure enough, F{1}(1,2) works as expected.

What's not going to work is the overall logic of the program, as you're trying to recursively define SF(elements(i,1)) in terms of itself, which doesn't make any sense. Try some more smaller pieces, like I show above, to experiment with function handles defined in terms of other function handles. You're likely to find some surprises.

Peter
  • 14,559
  • 35
  • 55
  • its working for simple adding but not on in the loop when i try to define it in terms of itself.I need to add up all the function because i am using step up function to define function different for different region. I just need to initially make all the handles equal to 0. – Avi Shri Jun 06 '13 at 21:00
  • actually its working on command window for simple functions but when i use it in a loop,it does not give you value like calculate S{2}(1,2) gives "[function handler]" or something like "SF{2}(0,0) ans = piecewise([0 <= x and 0 <= y, 0 <= 0])" – Avi Shri Jun 06 '13 at 22:28
  • Now that the syntax error is fixed, you may need to write a new question, or add a bunch of explanation and sample code to this one, that clearly lays out what you're trying to do and what the problem is. – Peter Jun 07 '13 at 14:54