xintegration.m

来自「黄华江所著《实用化工计算机模拟—MATLAB在化学工程中的应用》的所有例子源程序」· M 代码 · 共 40 行

M
40
字号
function xIntegration
% 积分例子
% An Example of Integration of a function f(x) from a to b
% by using TRAPZ (trapezoidal numerical integration),
% QUAD (adaptive Simpson quadrature), and  QUADL (adaptive Lobatto quadrature)
%
%   Author: HUANG Huajiang
%   Copyright 2002 UNILAB Research Center, 
%   East China University of Science and Technology, Shanghai, PRC
%   $Revision: 1.0 $  $Date: 2002/05/9 $

clear all
clc

% the interval: [a, b], the step: d
a = 0;          % 积分下限
b = 3*pi;       % 积分上限
d = pi/1000;    % 积分步长 
t = a:d:b;
y = func(t);

format long 

% 梯形数值积分(Trapezoidal numerical integration)
y_trapz = trapz(y) * d  

% 自适应Simpson法(Adaptive Simpson quadrature, low order) 
y_quad = quad(@func,a,b)

% 自适应Lobatto求积法(Adaptive Lobatto quadrature, high order)
y_quadl = quadl(@func,a,b)

disp('Results:')
disp('   Trapz              Qquad              Quadl')
disp([y_trapz,y_quad,y_quadl])

% ------------------------------------------------------------------
function y = func(t)  
y = exp(-0.5*t) .* sin(t+pi/6); 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?