【源码】计算求解定积分的Legendre-Gauss权和节点:lgwt函数

这是一个生成Legendre-Gauss权和节点的简单函数,用于计算某个区间[a,b]上连续函数的定积分。

This is a simple script which produces the Legendre-Gauss weights and nodes for computing the definite integral of a continuous function on some interval [a,b].

在区间[a,b]上以截断阶数 N 计算Legendre-Gauss节点和权值。

Computes the Legendre-Gauss nodes and weights on an interval [a,b] with truncation order N.

function [x,w]=lgwt(N,a,b)

% lgwt.m

%

% This script is for computing definite integrals using Legendre-Gauss

% Quadrature. Computes the Legendre-Gauss nodes and weights on an interval

% [a,b] with truncation order N

%

% Suppose you have a continuous function f(x) which is defined on [a,b]

% which you can evaluate at any x in [a,b]. Simply evaluate it at all of

% the values contained in the x vector to obtain a vector f. Then compute

% the definite integral using sum(f.*w);

%

% Written by Greg von Winckel - 02/25/2004

N=N-1;

N1=N+1; N2=N+2;

xu=linspace(-1,1,N1)’;

% Initial guess

y=cos((2*(0:N)’+1)pi/(2N+2))+(0.27/N1)sin(pixu*N/N2);

% Legendre-Gauss Vandermonde Matrix

L=zeros(N1,N2);

% Derivative of LGVM

Lp=zeros(N1,N2);

% Compute the zeros of the N+1 Legendre Polynomial

% using the recursion relation and the Newton-Raphson method

y0=2;

% Iterate until new points are uniformly within epsilon of old points

while max(abs(y-y0))>eps

L(:,1)=1;

Lp(:,1)=0;



L(:,2)=y;

Lp(:,2)=1;



for k=2:N1

    L(:,k+1)=( (2*k-1)*y.*L(:,k)-(k-1)*L(:,k-1) )/k;

end



Lp=(N2)*( L(:,N1)-y.*L(:,N2) )./(1-y.^2);   



y0=y;

y=y0-L(:,N2)./Lp;

end

% Linear map from[-1,1] to [a,b]

x=(a*(1-y)+b*(1+y))/2;

% Compute the weights

w=(b-a)./((1-y.2).*Lp.2)*(N2/N1)^2;

源码下载地址:

http://page5.dfpan.com/fs/flec4jc252419279169/

更多精彩文章请关注微信号:在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值