gquad1.m

来自「小的源码Matlab编程」· M 代码 · 共 33 行

M
33
字号

function [s, w, x] = Gquad1(fun, a, b, n, type, varargin)

% Numerical integration using either the Gauss-Legendre (type = 'L')
% or the Gauss-Chebyshev (type = 'C') quadrature with n (n > 0) nodes. 
% fun is a string representing the name of the function that is 
% integrated from a to b. For the Gauss - Chebyshev quadrature
% it is assumed that a = -1 and b = 1.
% The output parameters s, w, and x hold the computed approximation
% of the integral, list of weights, and the list of nodes, 
% respectively.

d = zeros(1,n-1);
if type == 'L'
   k = 1:n-1;
   d = k./(2*k - 1).*sqrt((2*k - 1)./(2*k + 1));
   fc = 2;
   J = diag(d,-1) + diag(d,1); 
   [u,v] = eig(J);
   [x,j] = sort(diag(v));
   w = (fc*u(1,:).^2)';
   w = w(j)';
   w = 0.5*(b - a)*w;
   x = 0.5*((b - a)*x + a + b);
else
   x = cos((2*(1:n) - (2*n + 1))*pi/(2*n))';
   w(1:n) = pi/n;
end
f = feval(fun,x,varargin{:});
s = w*f(:);
w = w';

⌨️ 快捷键说明

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