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

조회 수: 18 (최근 30일)
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
Walter Roberson
Walter Roberson 2016년 12월 14일
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

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2016년 12월 12일
h = @(x, y) f(x, y).*g(x,y);
  댓글 수: 1
mohammad m
mohammad m 2016년 12월 14일
편집: mohammad m 2016년 12월 14일
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

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2016년 12월 14일
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
mohammad m
mohammad m 2016년 12월 14일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by