Reputation: 11
Im new to MATLAB. Want to use integral2 as follows
function num = numer(x)
fun=@(p,w) prod((p+1-p).*(1-w).*exp(w.*x.*x/2))
num= integral2(fun ,0,1,0,1)
end
I get several errors starting with
Error using .*
Matrix dimensions must agree.
Error in numer>@(p,w)prod(p+(1-w).*exp(w.*x.*x/2)) (line 5)
fun=@(p,w) prod(p+(1-w).*exp(w.*x.*x/2))
Can you please tell me what I do wrong. Thanks
Upvotes: 0
Views: 203
Reputation: 18504
From the help for integral2
:
All input functions must accept arrays as input and operate
elementwise. The function Z = FUN(X,Y) must accept arrays X and Y of
the same size and return an array of corresponding values.
When x
was non-scalar, your function fun
did not do this. By wrapping everything in prod
, the function always returned a scalar. Assuming that your prod
is in the right place to begin with and taking advantage of the properties of the exponential, I believe this version will do what you need for vector x
:
x = [0 1];
lx = length(x);
fun = @(p,w)(p+1-p).^lx.*(1-w).^lx.*exp(w).^sum(x.*x/2);
num = integral2(fun,0,1,0,1)
Alternatively, fun = @(p,w)(p+1-p).^lx.*(1-w).^lx.*exp(sum(x.*x/2)).^w;
could be used.
Upvotes: 2