📄 trapint.m
字号:
function y = trapint (a,b,n,f)
%-----------------------------------------------------------------------
% Usage: y = trapint (a,b,n,f);
%
% Description: Use the trapezoid rule to numerically integrate the
% function f(x) over the interval [a,b] using n panels.
%
% Inputs: a = lower limit of integration
% b = upper limit of integration
% n = number of panels (n >= 1)
% f = string containing name of user-supplied function
% to be integrated. The form of f is:
%
% function y = f(x)
%
% When f is called it must return the value f(x).
%
% Outputs: y = estimate of integral
%
% Note: The truncation error is proportional to n^(-2).
%-----------------------------------------------------------------------
% Initialize
n = args (n,1,n,4,'trapint');
h = (b - a)/n;
% Compute integral
y = (feval(f,a) + feval(f,b))/2;
for i = 1 : n-1
y = y + feval(f,a+i*h);
end
y = h*y;
%-----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -