资源简介
高斯-勒让德积分(Gauss-legendre积分)matlab程序
可直接在函数名前设置输出为 [x,w]= 其中x为积分点,w为权重
代码片段和文件信息
function legendre_rule ( order a b filename )
%*****************************************************************************80
% 可直接在函数名前设置输出为 [xw]= 其中x为积分点,w为权重,此处为求积分,因此w之和可能不为1。
%% LEGENDRE_RULE generates a Gauss-Legendre rule.
%
% Discussion:
%
% This program computes a standard Gauss-Legendre quadrature rule
% and writes it to a file.
%
% The user specifies:
% * the ORDER (number of points) in the rule;
% * A the left endpoint;
% * B the right endpoint;
% * FILENAME the root name of the output files.
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
% Modified:
%
% 08 February 2014
%
% Author:
%
% John Burkardt
%
timestamp ( );
fprintf ( 1 ‘\n‘ );
fprintf ( 1 ‘LEGENDRE_RULE\n‘ );
fprintf ( 1 ‘ MATLAB version\n‘ );
fprintf ( 1 ‘\n‘ );
fprintf ( 1 ‘ Compute a Gauss-Legendre rule for approximating\n‘ );
fprintf ( 1 ‘ Integral ( A <= x <= B ) f(x) dx\n‘ );
fprintf ( 1 ‘ of order ORDER.\n‘ );
fprintf ( 1 ‘\n‘ );
fprintf ( 1 ‘ The user specifies ORDER A B and FILENAME.\n‘ );
fprintf ( 1 ‘\n‘ );
fprintf ( 1 ‘ ORDER is the number of points;\n‘ );
fprintf ( 1 ‘ A is the left endpoint;\n‘ );
fprintf ( 1 ‘ B is the right endpoint;\n‘ );
fprintf ( 1 ‘ FILENAME is used to generate 3 files:\n‘ );
fprintf ( 1 ‘ filename_w.txt - the weight file\n‘ );
fprintf ( 1 ‘ filename_x.txt - the abscissa file.\n‘ );
fprintf ( 1 ‘ filename_r.txt - the region file.\n‘ );
%
% Initialize the parameters.
%
alpha = 0.0;
beta = 0.0;
%
% Get ORDER.
%
if ( nargin < 1 )
order = input ( ‘ Enter the rule order ORDER.‘ );
elseif ( ischar ( order ) )
order = str2num ( order );
end
%
% Get A.
%
if ( nargin < 2 )
a = input ( ‘ Enter A.‘ );
elseif ( ischar ( a ) )
a = str2num ( a );
end
%
% Get B.
%
if ( nargin < 3 )
b = input ( ‘ Enter B.‘ );
elseif ( ischar ( b ) )
b = str2num ( b );
end
%
% Get FILENAME.
%
if ( nargin < 4 )
fprintf ( 1 ‘\n‘ );
fprintf ( 1 ‘ FILENAME specifies the ‘‘root name‘‘ of the quadrature files).\n‘ );
filename = input ( ‘ Enter the value of FILENAME as a quoted string:‘ );
end
%
% Input summary.
%
fprintf ( 1 ‘\n‘ );
fprintf ( 1 ‘ ORDER = %d\n‘ order );
fprintf ( 1 ‘ A = %f\n‘ a );
fprintf ( 1 ‘ B = %f\n‘ b );
fprintf ( 1 ‘ FILENAME = “%s“.\n‘ filename );
%
% Construct the rule.
%
kind = 1;
[ x w ] = cgqf ( order kind alpha beta a b );
%
% Write the rule.
%
r = [ a b ]‘;
rule_write ( order filename x w r );
%
% Terminate.
%
fprintf ( 1 ‘\n‘ );
fprintf ( 1 ‘LEGENDRE_RULE:\n‘ );
fprintf ( 1 ‘ Normal end of execution.\n‘ );
fprintf ( 1 ‘\n‘ );
timestamp ( );
return
end
function [ t wts ] = cdgqf ( nt kind alpha beta )
%******************************************
- 上一篇:一种基于MATLAB的机械臂工作空间简单算法
- 下一篇:最小二乘法定位解算
评论
共有 条评论