how to calculated integral of multiplying two function handle that changed by loop

16 views (last 30 days)
hi guys
i have two function like these:
f(x,y)=(i+1)x+(j-1)y
g(x,y)=(i-1)x+(j-1)y
(real functions are more complex that here)
now i want to calculate integral of h(x,y)=f*g when f & g change by loops depend on i & j.
i want to write these function in a new mfile & use of them in my code. but i don't know hoe i can do this.
actually i want do like bellow.
for i=1:n
for j=1:m
h=@(x,y)(f*g)
l=integral2(h,-1,1,-1,1)
end
end
that f & g are functions like this.
function d=f(i,j)
f(x,y)=(i+1)*x+(j-1)*y
end
i know cods are incorrect but wrote these to saying what i want to do.
function are complex & i should use them several times in my code so i cant write them in it.
thank u for answering :)
  1 Comment
Walter Roberson
Walter Roberson on 14 Dec 2016
for i=1:n
for j=1:m
h =@(x,y) f(x,y,i,j) * g(x,y,i,j);
l = integral2(h,-1,1,-1,1)
end
end
function d = f(x, y, i, j)
d = (i+1).*x + (j-1).*y;
end

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 12 Dec 2016
h = @(x, y) f(x, y).*g(x,y);
  1 Comment
mohammad m
mohammad m on 14 Dec 2016
Edited: mohammad m on 14 Dec 2016
hi thank you for answering. when i use this code an error like bellow occurred when i edit my code like you.
Undefined function or variable 'x'.
then for calculate integral of (f*f), i change my code like below :
for i=1:2
for j=1:2
h=@(x,y)f(i,j).*f(i,j);
l=integral2(h,-1,1,-1,1)
end
end
and function f in another mfile like below
function d=f(i,j)
d=@(x,y)(i+1)*x+(j-1)*y
end
now i get this error:
Undefined function 'times' for input arguments
of type 'function_handle'.
Error in @(x,y)f(i,j).*f(i,j)
Error in integral2Calc>integral2t/tensor (line
229)
Z = FUN(X,Y); NFE = NFE + 1;
Error in integral2Calc>integral2t (line 56)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 10)
[q,errbnd] =
integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q =
integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in Untitled4 (line 6)
l=integral2(h,-1,1,-1,1)
i don't know how to make new functions by loop & how to multiply them & how to integrate new function. i'm using matlab 2013

Sign in to comment.


Steven Lord
Steven Lord on 14 Dec 2016
A 1-dimensional example:
fun1 = @(x) sin(x);
fun2 = @(y) cos(y);
fun3 = @(z) fun1(z).*fun2(z);
integral(fun3, 0, 1)
To check, explicitly specify the function in the integral call:
integral(@(x) sin(x).*cos(x), 0, 1)
Another check is to perform the integration symbolically with the int function from Symbolic Math Toolbox. The vpa call just displays the answer in a format that's easier to compare with the numeric answers.
syms q
vpa(int(sin(q)*cos(q), q, 0, 1))
  1 Comment
mohammad m
mohammad m on 14 Dec 2016
thank you for answering. my function change by loop & are complex, i'm trying to don't use of syms, because syms reduce speed, so i'm trying to find a way by function handle or any other way that can calculate multiplying & integration of parametric functions that change by loop.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!