1

I have written a function feval that takes two arguments and spits out a number.

Now I wanted to use the command integral2 in order to integrate over my function feval(x,y).

The problem seems to be that integral2 thinks that I have a function that can take two arrays as arguments and apply pairwise operations on them. Unfortunately, this is not the case. My function can only works with 2 numbers and not with full arrays. Is there any standard method to make this work?

Actually, this is my code now and MATLAB claims that q = integral2( @(x,y) arrayfun(func_cross_scat,x,y),0,2*pi,0,pi); my function(feval, that i renamed func_cross_scat does not get enough input arguments)

2
  • 2
    (1) you can create a wrapper function that will call your function for all values in a larger array (2) you can use the . command where possible (vectorize) Commented Aug 15, 2013 at 16:21
  • @TryHard would you mind taking a lot at this error? Commented Aug 15, 2013 at 17:32

1 Answer 1

1

Feed integral2 not with feval, but with feval_wrapper defined as

feval_wrapper = @(x,y) arrayfun(feval, x, y)

x and y can now be arrays (of the same size). This works because arrayfun calls feval for each pair of elements of the input arrays x, y and gives an array as the result.

As a side comment, "feval" is probably not a good name for your function, because Matlab has a built-in feval.

Sign up to request clarification or add additional context in comments.

9 Comments

sorry a problem occured
yes, as I included in my initial question. matlab says now, that the function does not get any arguments at all. are you sure that you do not need to write arguments there? (you have just written arrayfun(feval,x,y), perhaps one has to write something that suggests that feval has arguments (x,y) itself. but i am not sure about this. therefore i just wanted to ask you whether you are completely sure about your answer?
@Lipschitz if you read Luis Mendos answer he explains that you should NOT use the name "feval" as this will lead to confusion. Try instead feval_wrapper = @(x,y) arrayfun(myfunction, x, y)...
yes, i know and i just used this name here, so that all knew about which function i was talking. so nevertheless, it does not work right.
@Lipschitz It would be easier to discuss on real code. Could you post what you are doing?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.