You are on page 1of 75

University of Technical Education HCM City, 2013

Chapter 5 – Numerical Integration

5.1 Matlab function


5.2 Trapzoidal method

5.3 Simpson’s method


y2
5.4 Gauss’s method

5.5 Examples

1 y1
2.5
TÍCH PHAÂN
• 5.1. Duøng haøm thö vieän Matlab: trapz, quad, quad8, dblquad
Syntax

Z = trapz(Y) X, Y: vectô hay ma traän


Z = trapz(X,Y)

Giaù trò chính xaùc tích phaân: I   sin xdx  2
0

>>X = 0:pi/100:pi; >>X = 0:pi/100:pi;


>>Y = sin(x); >>Y = sin(x);
>>Z = trapz(X,Y) >>Z = pi/100*trapz(Y)

Z= Z=
1.9998 1.9998
Syntax
q = quad(fun,a,b)
q = quad(fun,a,b,tol) Ví duï
q = quad(fun,a,b,tol,trace) >> Q = quad('1./(x.^3-2*x-5)',0,2)
Q=
b -0.4605
I   f ( x)dx >>F = inline('1./(x.^3-2*x-5)');
>>Q = quad(F,0,2)
a

Q=
fun: haøm tích phaân
-0.4605
a,b: caän tích phaân Q = quad(‘myfun’,0,2);
Q=
tol: sai soá chaáp nhaän
-0.4605
trace: veát ma traän tích phaân
%myfun.m laø M-file.
---------------------------------------------------
function y = myfun(x)
y = 1./(x.^3-2*x-5);
fun: haøm tích phaân
tol: sai soá chaáp nhaän

Syntax
q = dblquad(fun,xmin,xmax,ymin,ymax) Tính tích phaân keùp
q = dblquad(fun,xmin,xmax,ymin,ymax,tol) xmax y max
I   f ( x, y )dxdy
>>Q = dblquad(inline('y*sin(x)+x*cos(y)'), pi, 2*pi, 0, pi) xmin y min
Q=
-9.8696
>>Q = dblquad(‘integrnd’, pi, 2*pi, 0, pi)
Q=
-9.8696
-------------------------------------------------
integrnd.m (m-file).
function z = integrnd(x, y)
z = y*sin(x)+x*cos(y);
--------------------------------------------------
>>dblquad(inline('sqrt(max(1-(x.^2+y.^2),0))'),-1,1,-1,1)
ans =
2.0944
>>dblquad(inline('sqrt(1-(x.^2+y.^2)).*(x.^2+y.^2<=1)'),-1,1,-1,1)
ans =
2.0944
Coâng sinh ra do löïc ñaåy cuûa piston p(x)  2 - 3x
1/2
 xcos(2x)  xe 0.5 x

 2 - 3x 
2.5
treân ñoä dôøi ab : A  1/2
 xcos(2x)  xe 0.5 x dx
1
y2

1 y1
2.5

Thoâng löôïng doøng chaûy trong moät oáng coù tieát dieän thay ñoåi nhö hình veõ.
Bieát vaän toác taïi moät ñieåm baát kì trong oáng coù phöông trình V=3xy.

Ñöôøng sinh Y1=1-(X/2)2 ; Y2=1+(X/2)2 ; X[1,2.5]


Theå tích baùn caàu [Stewart 937/12]
• 5.2. Luaät hình thang (Trapzoidal Rule)
xi
hi f(x)

xi 1
f ( x)dx 
2
( f ( xi 1 )  f ( xi ))
Eh
y

h  f ( x0 )  2 f ( x1 )  2 f ( x 2 )  .....
I trap     E
2   2 f ( x n 1 )  f ( x n ) 

ba
h , x i  a  i * h, x 0  a , x n  b x
N
x0 = a x1 x2 …. Xn-1 xn=b
  f 0  2 f 1  2 f 2  .....  2 f n 1  f n   E
h
I trap
2
1 (b  a) 3 N
xi 1  xi 1
E
12 N 3
i 1
f '' ( xi ), xi 
2
Ví duï 2
2 2
  x 2 
Tính tích phân: S f ( x)dx    1     dx
 2 
0 0  
Matlab program

clear all
clc
N=16; Keát quûa
a=0;
b=2; N h Sh Eh
h=(b-a)/N; 2 1. 12.7627 -1.0341
S=0; 4 0.5 11.9895 -0.2609
for i=0:N 8 0.25 11.7940 -0.0654
x=a+i*h; 16 0.125 11.7449 -0.0163
if i==0 | i==N 32 0.0625 11.7326 -0.0040
64 0.03125 11.7296 -0.0010
c=1;
else
c=2;
end
S=S+c*pi*(1+(x/2).^2).^2;
end
S=h*S/2
• 5.3. Luaät Simpson 1/3 (Simpson Rule)
b
h h N 1 N 2

S   f ( x )dx   f (a)  4 f ( x )  f (b)  E S simp   f (a )  4 f (a  ih)  2  f (a  ih)  f (b)  E
a
3 3 i 1 i 2 
ba ab h  f ( x0 )  4 f ( x1 )  2 f ( x 2 )  4 f ( x3 )  
x 0  a , x 2  b, h  , x    E
S simp 
3  .....  4 f ( x n 1 )  f ( x n )
2 2
b 
S   f ( x)dx 
h
 f 0  4 f1  f 2   E N h 5 '''' N
xi 1  xi 1
a
3 E f , f ''''
  f '''' ( xi ) / N , xi 
2 90 i 1 2

N h Sh Eh
2 1. 11.7809 -0.0523
4 0.5 11.7318 -0.0032
8 0.25 11.7288 -0.0002
16 0.125 11.7286 -0.0000
32 0.0625 11.7286 -0.0000
64 0.03125 11.7286 -0.0000
clear all Matlab program
clc
N=16;
a=0;
b=2; Keát quûa
h=(b-a)/N;
Giaù trò tích phaân
S=0;
1.325
for i=0:N
x=a+i*h; 1.32
if i==0 | i==N
1.315
c=1;
1.31
elseif i==fix(i/2)*2+1 Luaät Simpson
c=4; 1.305

else Chính xaùc


1.3
c=2;
end 1.295
S=S+c*pi*(1+(x/2).^2).^2; Luaät hình thang
1.29
end 0 10 20 30 40 50
S=h*S/3 Soá phaân ñoaïn
3. Tích phaân Gauss (Gauss quadrature):

1
I   f ( x) dx  w1 f ( x1 )  w2 f ( x 2 )    wn f ( x n )
1

Ví duï 1
I   (0.2  25 x  200 x 2  675 x 3  900 x 4  400 x 5 ) dx
1

Matlab program
f1=w1*gauss1(x1);
Tính vôùi 4 ñieåm Gauss: f2=w2*gauss1(x2);
clear all f3=w3*gauss1(x3);
clc f4=w4*gauss1(x4);
format long I=f1+f2+f3+f4
x1=-0.861136;
%------------------------------------------------------------
x2=-0.339981;
--------
x3=0.339981;
function ff=gauss1(x)
x4=0.861136;
ff=400*x^5-900*x^4+675*x^3-
% ------troïng soá-------
200*x^2+25*x+0.2;
w1=0.347855;
keát quaû:
w2=0.652145;
I=-4.929329328775451e+002
w3=0.652145;
w4=0.347855;
Forced Vibrations

The driving force can be periodic


(repeating), like in (a) and (b)

non-periodic © can be created by


forming infinite series of harmonic
functions (”fourier analysis”).
Types of Harmonic Forcing
Rotor Excitation
External Forcing

Base Excitation
syms t n T
>> y=t*sin(2*pi*n*t/T);
>> Z=int(y,0,T)

>> pretty(Z)

2
T (sin(2 PI n) - 2 PI n cos(2 PI n))
-------------------------------------
2 2
4 PI n
Tổng quan phép biến đổi Laplace thuận và ngược

- Giải phương trình vi phân và hệ phương trình vi phân


- Biến đổi không gian trạng thái (t) không gian tần số (s)
- Xác định hàm truyền trong điều khiển tự động
- Qui luật chuyển động hệ cơ học có điều khiển

Cho hàm f(t) tìm biến đổi Laplace thuận (laplace)


Ví dụ 1 Tìm biến đổi Laplace hàm

Ví dụ 2 Tìm biến đổi Laplace hàm

Matlab
clear all
syms t T; syms s positive
f=sin(3*t);
I=int(f*exp(-s*t),t); 3
v=subs(I,t,T)-subs(I,t,0); ------
F=limit(v,T,inf); pretty(F)
2
%----------kiểm tra bằng hàm Matlab-----------------
syms s unreal s +9
F=laplace(f); pretty(F)
Cho hàm f(t) tìm biến đổi Laplace ngược (ilaplace)

 
F ( s )  L[ f (t ) ] f (t )e  st dt  f (t )  L1 [ F ( s)]   F ( s)e st ds
0 0

Ví dụ 3 Tìm biến đổi Laplace ngược hàm


syms a b s
q=(4*s+15)/(2*s^2+3*s);
F=a/s+b/(2*s+3);
pf=cpf(q,F); 5 - 3 exp(- 3/2 t)
f=ilaplace(pf);pretty(f)
%----------------------
f=ilaplace(q); pretty(f)
Ví dụ 4 Dùng biến đổi Laplace thuận và ngược giải phương trình vi phân
cấp 2 sau:

y ''  4 y  e 2t cos t y (0)  1, y ' (0)  0

Matlab

clear all
syms s t ys
y=sym('y(t)');
di=diff(y,t,2)+4*y-exp(-2*t)*cos(t);
Lp=laplace(di);
Lp=chiclet(Lp);pretty(Lp);
pretty(Lp)
eq=subs(Lp,{'y(0)','Dy(0)'},{1,0});
pretty(eq)
ys=solve(eq,ys); pretty(ys)
y=ilaplace(ys); pretty(y)
Sủ dụng hàm [r,p,k]=residue(b,a)

Phân rã hàm F(s)

MATLAB

Kết quả
Kết quả phân rã

[b,a]=residue(r,p,k) Chuyển ngược


Ví dụ: Giải phương trình vi phân

y  4 y  e 2t cos t  0
 y (0)  1; y (0)  0
Matlab code
clear all
clc
t=0:pi/100:2*pi;
y=(7/65)*exp(-2*t).*cos(t)-(4/65)*exp(-2*t).*sin(t)+(58/65)*cos(2*t)+(9/65)*sin(2*t);
plot(t,y,'-b','Linewidth',2)
xlabel('thoi gian t(s)')
ylabel('dap ung dau ra y(t)')
title('dap ung theo thoi gian')
grid on

dap ung theo thoi gian


1

0.8

0.6

0.4
dap ung dau ra y(t)

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
thoi gian t(s)
% Establish a mesh on the domain:

T=RectangleMeshTopLeftD1(16);

% Assemble the stiffness matrix: 15

K=Stiffness1(T,k); 10

% Create the boundary data: 5

0
1
1
0.8
0.5 0.6
0.4
0.2
0 0
g=getDirichletData(T,u);
h=getNeumannData1(T,ux,uy,k);
% Assemble the load vector:
F=Load1(T,f,k,g,h);
% Solve the finite element equations:
U=K\F;
% Display the computed solution:
figure(1)
clf
ShowPWLinFcn1(T,U,g)
% Compute the error (in the energy norm) in the computed
% solution. Also compute the energy norm of the solution
% and display the relative error:
err=EnergyNormErr1(T,k,ux,uy,U,g);
nrm=EnergyNorm1(T,k,ux,uy);
disp(['Relative energy norm error in computed solution: ',num2str(err/nrm)])
Function T=RectangleMeshTopLeftD1(nx,ny,lx,ly)
if nargin==3
ly=lx;
elseif nargin<3
lx=1;
ly=1;
end
if nargin<2
ny=nx;
end
T.Degree=1;
% Compute the number of nodes and allocate space for Nodes.
% Also define NodePtrs:
Nv=(nx+1)*(ny+1);
T.Nodes=zeros(Nv,2);
T.NodePtrs=zeros(Nv,1);
% Compute the number of free nodes and define T.FNodePtrs:
Nf=Nv-(nx+1)-ny;
T.FNodePtrs=zeros(Nf,1);
for j=1:ny
T.FNodePtrs((j-1)*nx+1:j*nx)=((j-1)*(nx+1)+2:j*(nx+1))';
end
% Compute the number of constrained nodes and define CNodePtrs:
Nc=nx+1+ny;
T.CNodePtrs=[(1:nx+1:ny*(nx+1)+1-(nx+1))';(ny*(nx+1)+1:Nv)'];
T.NodePtrs(T.FNodePtrs)=(1:Nf)';
T.NodePtrs(T.CNodePtrs)=-(1:Nc)';
% Compute the number of triangles and allocate space for Elements:
Nt=2*nx*ny;
T.Elements=zeros(Nt,3);
% Compute the number of edges and allocate space for Edges, EdgeEls,
% and EdgeCFlags:
Ne=nx+ny*(3*nx+1);
T.Edges=zeros(Ne,2);
T.EdgeEls=zeros(Ne,2);
T.EdgeCFlags=zeros(Ne,1);
% Compute the number of boundary edges and define FBndyEdges:
Nb=nx+ny;
T.FBndyEdges=zeros(Nb,1);
for i=1:nx
T.FBndyEdges(i)=i;
end
for j=1:ny
T.FBndyEdges(nx+j)=nx+(j-1)*(3*nx+1)+2*nx+1;
end
% Loop over the rows and columns of the mesh, defining the nodes.
%
% k is the number of the node.
k=0;
dx=lx/nx;
dy=ly/ny;
% Loop over the rows of the grid
for j=0:ny
y=j*dy;
% Loop over the columns of the grid
for i=0:nx
x=i*dx;
k=k+1;
% Insert the coordinates of the node
T.Nodes(k,:)=[x,y];
end
end
% Define the bottom row of edges:
for i=1:nx
T.Edges(i,:)=[i,i+1];
T.EdgeEls(i,:)=[2*i,-i];
end
% Loop over the rows and columns of the mesh, defining the edges and
% triangular elements.
%
% l is the number of the edge
% k is the number of the element
k=-1;
l=nx;
% Loop over the rows of the grid
for j=1:ny
% Define the left-hand edge on this row:
l=l+1;
T.Edges(l,:)=[(j-1)*(nx+1)+1,j*(nx+1)+1];
T.EdgeEls(l,:)=[2*nx*(j-1)+1,0];
% Loop over the columns of the grid
for i=1:nx
% k is the number of the "upper left" triangle
% k+1 is the number of the "lower right" triangle
k=k+2;
T.Elements(k,:)=[-l,l+1,-(l+2*(nx+1)-i)];
T.Elements(k+1,:)=[l-nx-i+1,l+2,-(l+1)];
T.Edges(l+1,:)=[(j-1)*(nx+1)+i,j*(nx+1)+i+1];
T.EdgeEls(l+1,:)=[k,k+1];
T.Edges(l+2,:)=[(j-1)*(nx+1)+i+1,j*(nx+1)+i+1];
if i<nx
T.EdgeEls(l+2,:)=[k+1,k+2];
else
T.EdgeEls(l+2,:)=[k+1,-(nx+j)];
end
l=l+2;
end
% Define the edges on the top of this row:
for i=1:nx
l=l+1;
T.Edges(l,:)=[j*(nx+1)+i,j*(nx+1)+i+1];
if j<ny
T.EdgeEls(l,:)=[(j-1)*2*nx+2*i-1,j*2*nx+2*i];
else
T.EdgeEls(l,:)=[(j-1)*2*nx+2*i-1,0];
end
end
end
“ Mỗi chúng ta phải nổ lực hết mình, biết chia sẽ và hợp tác để tìm
ra những mảnh ghép hợp lý bắt cầu cho sự thành công”
N.H.SON

You might also like