Integrate a function over a triangle area
이전 댓글 표시
Hi, I'm trying to integrate a function over the area of a triangle, that is given by a set of 3 arbitrary points. My problem is that because the points are arbitrary, my program doesn't take into account that dA doesn't really equal dy.dx. Now, is there a predefined function that can do the integration over the area given by 3 points?
Here's my current code:
function [Ke]=Ke(F,ex,ey)
%F=@(x,y)(x/2 - y/2 + 1/2)^2 + (x/2 + y/2 - 1/2)^2 + y^2
%ex=[0 -1 1]
%ey=[1 0 0]
syms x y
x1= ex(1);
x2= ex(2);
x3= ex(3);
y1= ey(1);
y2= ey(2);
y3= ey(3);
Ke=zeros(3,3)
for i=1:3;
for j=1:3;
Ke(i,j)=integral2(F,x,ex(i),ex(j),ey(i),ey(j))
K=K+Ke(i,j)
end
end
댓글 수: 3
Bruno Luong
2018년 11월 17일
편집: Bruno Luong
2018년 11월 17일
IMO you have a much bigger problem than not knowing dA.
Your code adds the integrals of function 9 rectanges (3 x 3 pairs of x and y intervals), which won't give the integral on triangle at all.
This code is emply incorrect to start with.
John D'Errico
2018년 11월 17일
Is your function truly known, and as simple as the one you show? If so, you can write an analytical integral, there is no need for a call to integral2.
Jafar Alrashdan
2018년 11월 17일
편집: Jafar Alrashdan
2018년 11월 17일
채택된 답변
추가 답변 (2개)
Bruno Luong
2018년 11월 17일
편집: Bruno Luong
2018년 11월 17일
Here is a method.
Assuming you triangle T has 3 vertexes P1, P2, P3, each vertex Pi has corrdinates (xi,yi).
Any point P inside a triangle can be written as barycentric coordinates (w1,w2,w3)
P = w1*P1 + w2*P2 + w3*P3
with wi such that
0 <= wi <=0
and
w1+w2+w3 = 1.
Integral of f on T written in barycentric cooordinates becomes
|T|/2 Integral f(w1*P1+w2*P2+(1-w1-w2)*P3) (dw1*dw2)
the integral is carried out on the "right" triangle domain T12 (where |T| is the area of the original triangle T given later):
T12: = { (w1,w2): 0<=w1<=1; 0<=w2<=1-w1 }.
So what you should do is compute this double-cascaded integrals:
|T|/2 * integral_(w1 in (0,1)) integral (w2 in (0,1-w1)) f(w1*P1+w2*P2+(1-w1-w2)*P3) dw2 dw1.
The area |T| (dA) can be computed
|T| = 0.5* abs(det (P2-P1,P3-P1))
MATLAB code
function I = TriIntegral(f, Tx, Ty)
% I = TriIntegral(f, Tx, Ty)
% 2D integration of f on a triangle
% INPUTS:
% - f is the vectorized function handle that when calling f(x,y) returns
% function value at (x,y), x and y are column vectors
% - Tx,Ty are is two vectors of length 3, coordinates of the triangle
% OUTPUT
% I: integral of f in T
T = [Tx(:), Ty(:)];
I = integral2(@(s,t) fw(f,s,t,T),0,1,0,1);
A = det(T(2:3,:)-T(1,:));
I = I*abs(A);
end % TriIntegral
%%
function y = fw(f, s, t, T)
sz = size(s);
w1 = (1-s); % Bug fix
w2 = s.*t;
w3 = 1-w1-w2;
P = [w1(:),w2(:),w3(:)] * T;
y = feval(f,P(:,1),P(:,2));
y = s(:).*y(:);
y = reshape(y,sz);
end
Apply to your example:
F=@(x,y)(x/2 - y/2 + 1/2).^2 + (x/2 + y/2 - 1/2).^2 + y.^2;
ex=[0 -1 1;
ey=[1 0 0];
TriIntegral(F,ex,ey)
>> ans =
0.5000
댓글 수: 8
Bruno Luong
2018년 11월 17일
Note:
I = integral_(w1 in (0,1)) integral (w2 in (0,w1)) g(w1,w2) dw1 dw2
Can be transformed to an integral on square, thus using integral_2
I = integral_(s in (0,1)) integral (t in (0,1)) g(s,s*t)*s ds dt
Bruno Luong
2018년 11월 17일
Note 2:
If you have f polynomial function; the integration can be computed by using Gauss and weight points. Checkout https://en.wikipedia.org/wiki/Gaussian_quadrature
Jafar Alrashdan
2018년 11월 17일
Bruno Luong
2018년 11월 17일
Not complicated at all if you don't want to reinvent the wheel.
You have many File Exchange available that compute the Points and Weights with respect to the Barycentric coordinates (again), and apply it is simply a matter of summing up the function.
Jafar Alrashdan
2018년 11월 17일
John D'Errico
2018년 11월 17일
As I showed in my response, a Gaussian integration is simple, if you use Greg's code to give a Gaussian integration over a simplex.
Bruno Luong
2018년 11월 17일
wops, I made a mistake, w2 should be in (0,1-w1). Code and text corrected accordingly
Bruno Luong
2018년 11월 18일
Try the same test than John's, it differs by 2 last digits.
The advantage of using integral2 over Gaussian quadrature is that it will automatically adapt to the function smoothness and you don't have to ask whereas it converges or not.
F = @(x,y) sin(x+y).*cos(x-2*y)
ex=[0 -1 1];
ey=[1 0 0];
TriIntegral(F,ex,ey)
ans =
0.188712575302678
madhan ravi
2018년 11월 17일
편집: madhan ravi
2018년 11월 17일
0 개 추천
1) Find the equation of the 3 lines connecting the three points.
2)Split the domain according to upper limit and lower limit by substituting the points in the domain you can determine it.
3) Use double integration in each domain and sum up all the integrals to attain the final triangulare area.
댓글 수: 5
Jafar Alrashdan
2018년 11월 17일
madhan ravi
2018년 11월 17일
I know what you mean but the domain can only be defined by the user becuase matlab won't analayse the upper and lower limits by itself.
Jafar Alrashdan
2018년 11월 17일
madhan ravi
2018년 11월 17일
Your welcome , if you do it publish it as file exchange
Jafar Alrashdan
2018년 11월 17일
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
