├── LGL1 ├── main.m ├── main2.m ├── mycost.m ├── mydae.m ├── LGL_cons.m ├── LGL_obj.m ├── myevent.m ├── LagrangeInterpolation.m ├── LGL_weights.m ├── lepoly.m ├── LGL_Dmatrix.m └── LGL_nodes.m ├── LGL2 ├── main.m ├── main2.m ├── mycost.m ├── mydae.m ├── LGL_cons.m ├── LGL_obj.m ├── myevent.m ├── LagrangeInterpolation.m ├── LGL_weights.m ├── lepoly.m ├── LGL_Dmatrix.m └── LGL_nodes.m ├── README.md └── LICENSE /LGL1/main.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL1/main.m -------------------------------------------------------------------------------- /LGL1/main2.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL1/main2.m -------------------------------------------------------------------------------- /LGL1/mycost.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL1/mycost.m -------------------------------------------------------------------------------- /LGL1/mydae.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL1/mydae.m -------------------------------------------------------------------------------- /LGL2/main.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL2/main.m -------------------------------------------------------------------------------- /LGL2/main2.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL2/main2.m -------------------------------------------------------------------------------- /LGL2/mycost.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL2/mycost.m -------------------------------------------------------------------------------- /LGL2/mydae.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL2/mydae.m -------------------------------------------------------------------------------- /LGL1/LGL_cons.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL1/LGL_cons.m -------------------------------------------------------------------------------- /LGL1/LGL_obj.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL1/LGL_obj.m -------------------------------------------------------------------------------- /LGL1/myevent.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL1/myevent.m -------------------------------------------------------------------------------- /LGL2/LGL_cons.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL2/LGL_cons.m -------------------------------------------------------------------------------- /LGL2/LGL_obj.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL2/LGL_obj.m -------------------------------------------------------------------------------- /LGL2/myevent.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cporoske/LGLDemo/HEAD/LGL2/myevent.m -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LGLDemo 2 | For Matlab Legendre Pseudospectral Method Implementation Demostration 3 | -------------------------------------------------------------------------------- /LGL1/LagrangeInterpolation.m: -------------------------------------------------------------------------------- 1 | function y=gpopsLagrangeInterpolation(x,pointx,pointy) 2 | % 3 | %LAGRANGE approx a point-defined function using the Lagrange polynomial interpolation 4 | % 5 | % LAGRANGE(X,POINTX,POINTY) approx the function definited by the points: 6 | % P1=(POINTX(1),POINTY(1)), P2=(POINTX(2),POINTY(2)), ..., PN(POINTX(N),POINTY(N)) 7 | % and calculate it in each elements of X 8 | % 9 | % If POINTX and POINTY have different number of elements the function will return the NaN value 10 | % 11 | % function wrote by: Calzino 12 | % 7-oct-2001 13 | % 14 | x = x'; 15 | pointx = pointx'; 16 | pointy = pointy'; 17 | n=size(pointx,2); 18 | L=ones(n,size(x,2)); 19 | if (size(pointx,2)~=size(pointy,2)) 20 | fprintf(1,'\nERROR!\nPOINTX and POINTY must have the same number of elements\n'); 21 | y=NaN; 22 | else 23 | for i=1:n 24 | for j=1:n 25 | if (i~=j) 26 | L(i,:)=L(i,:).*(x-pointx(j))/(pointx(i)-pointx(j)); 27 | end 28 | end 29 | end 30 | y=0; 31 | for i=1:n 32 | y=y+pointy(i)*L(i,:); 33 | end 34 | end 35 | 36 | y = y'; 37 | -------------------------------------------------------------------------------- /LGL2/LagrangeInterpolation.m: -------------------------------------------------------------------------------- 1 | function y=gpopsLagrangeInterpolation(x,pointx,pointy) 2 | % 3 | %LAGRANGE approx a point-defined function using the Lagrange polynomial interpolation 4 | % 5 | % LAGRANGE(X,POINTX,POINTY) approx the function definited by the points: 6 | % P1=(POINTX(1),POINTY(1)), P2=(POINTX(2),POINTY(2)), ..., PN(POINTX(N),POINTY(N)) 7 | % and calculate it in each elements of X 8 | % 9 | % If POINTX and POINTY have different number of elements the function will return the NaN value 10 | % 11 | % function wrote by: Calzino 12 | % 7-oct-2001 13 | % 14 | x = x'; 15 | pointx = pointx'; 16 | pointy = pointy'; 17 | n=size(pointx,2); 18 | L=ones(n,size(x,2)); 19 | if (size(pointx,2)~=size(pointy,2)) 20 | fprintf(1,'\nERROR!\nPOINTX and POINTY must have the same number of elements\n'); 21 | y=NaN; 22 | else 23 | for i=1:n 24 | for j=1:n 25 | if (i~=j) 26 | L(i,:)=L(i,:).*(x-pointx(j))/(pointx(i)-pointx(j)); 27 | end 28 | end 29 | end 30 | y=0; 31 | for i=1:n 32 | y=y+pointy(i)*L(i,:); 33 | end 34 | end 35 | 36 | y = y'; 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 张柯柯 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LGL1/LGL_weights.m: -------------------------------------------------------------------------------- 1 | %-------------------------------------------------------------------------- 2 | % LGL_weights.m 3 | % determines Gaussian quadrature weights using Lagrange-Gauss-Lobatto (LGL) 4 | % nodes 5 | %-------------------------------------------------------------------------- 6 | % w = LGL_weights(tau) 7 | % tau: LGL nodes 8 | % w: Gaussian quadrature weights 9 | %-------------------------------------------------------------------------- 10 | % Primary Contributor: Daniel R. Herber, Graduate Student, University of 11 | % Illinois at Urbana-Champaign 12 | % Link: https://github.com/danielrherber/basic-multiple-interval-pseudospectral 13 | %-------------------------------------------------------------------------- 14 | function w = LGL_weights(tau) 15 | % number of nodes 16 | N = length(tau)-1; 17 | 18 | % See Page 99 of the book: J. Shen, T. Tang and L. Wang, Spectral Methods: 19 | % Algorithms, Analysis and Applications, Springer Series in Computational 20 | % Mathematics, 41, Springer, 2011. 21 | % Uses the function: lepoly() 22 | % Original function: [varargout] = legslb(n) located at 23 | % http://www1.spms.ntu.edu.sg/~lilian/bookcodes/legen/legslb.m 24 | [~,y] = lepoly(N,tau(2:end-1)); 25 | % Use the weight expression (3.188) to compute the weights 26 | w = [2/(N*(N+1));2./(N*(N+1)*y.^2);2/(N*(N+1))]; 27 | 28 | end -------------------------------------------------------------------------------- /LGL2/LGL_weights.m: -------------------------------------------------------------------------------- 1 | %-------------------------------------------------------------------------- 2 | % LGL_weights.m 3 | % determines Gaussian quadrature weights using Lagrange-Gauss-Lobatto (LGL) 4 | % nodes 5 | %-------------------------------------------------------------------------- 6 | % w = LGL_weights(tau) 7 | % tau: LGL nodes 8 | % w: Gaussian quadrature weights 9 | %-------------------------------------------------------------------------- 10 | % Primary Contributor: Daniel R. Herber, Graduate Student, University of 11 | % Illinois at Urbana-Champaign 12 | % Link: https://github.com/danielrherber/basic-multiple-interval-pseudospectral 13 | %-------------------------------------------------------------------------- 14 | function w = LGL_weights(tau) 15 | % number of nodes 16 | N = length(tau)-1; 17 | 18 | % See Page 99 of the book: J. Shen, T. Tang and L. Wang, Spectral Methods: 19 | % Algorithms, Analysis and Applications, Springer Series in Computational 20 | % Mathematics, 41, Springer, 2011. 21 | % Uses the function: lepoly() 22 | % Original function: [varargout] = legslb(n) located at 23 | % http://www1.spms.ntu.edu.sg/~lilian/bookcodes/legen/legslb.m 24 | [~,y] = lepoly(N,tau(2:end-1)); 25 | % Use the weight expression (3.188) to compute the weights 26 | w = [2/(N*(N+1));2./(N*(N+1)*y.^2);2/(N*(N+1))]; 27 | 28 | end -------------------------------------------------------------------------------- /LGL1/lepoly.m: -------------------------------------------------------------------------------- 1 | function [varargout]=lepoly(n,x) 2 | 3 | % lepoly Legendre polynomial of degree n 4 | % y=lepoly(n,x) is the Legendre polynomial 5 | % The degree should be a nonnegative integer 6 | % The argument x should be on the closed interval [-1,1]; 7 | % [dy,y]=lepoly(n,x) also returns the values of 1st-order 8 | % derivative of the Legendre polynomial stored in dy 9 | % Last modified on August 30, 2011 10 | % Verified with the chart in http://keisan.casio.com/has10/SpecExec.cgi 11 | 12 | if nargout==1, 13 | if n==0, varargout{1}=ones(size(x)); return; end; 14 | if n==1, varargout{1}=x; return; end; 15 | polylst=ones(size(x)); poly=x; % L_0(x)=1, L_1(x)=x 16 | for k=2:n, % Three-term recurrence relation: 17 | polyn=((2*k-1)*x.*poly-(k-1)*polylst)/k; % kL_k(x)=(2k-1)xL_{k-1}(x)-(k-1)L_{k-2}(x) 18 | polylst=poly; poly=polyn; 19 | end; 20 | varargout{1}=polyn; 21 | end; 22 | 23 | if nargout==2, 24 | if n==0, varargout{2}=ones(size(x)); varargout{1}=zeros(size(x)); return;end; 25 | if n==1, varargout{2}=x; varargout{1}=ones(size(x)); return; end; 26 | 27 | polylst=ones(size(x)); pderlst=zeros(size(x));poly=x; pder=ones(size(x)); 28 | % L_0=1, L_0'=0, L_1=x, L_1'=1 29 | for k=2:n, % Three-term recurrence relation: 30 | polyn=((2*k-1)*x.*poly-(k-1)*polylst)/k; % kL_k(x)=(2k-1)xL_{k-1}(x)-(k-1)L_{k-2}(x) 31 | pdern=pderlst+(2*k-1)*poly; % L_k'(x)=L_{k-2}'(x)+(2k-1)L_{k-1}(x) 32 | polylst=poly; poly=polyn; 33 | pderlst=pder; pder=pdern; 34 | end; 35 | varargout{2}=polyn; varargout{1}=pdern; 36 | end; 37 | 38 | return 39 | 40 | -------------------------------------------------------------------------------- /LGL2/lepoly.m: -------------------------------------------------------------------------------- 1 | function [varargout]=lepoly(n,x) 2 | 3 | % lepoly Legendre polynomial of degree n 4 | % y=lepoly(n,x) is the Legendre polynomial 5 | % The degree should be a nonnegative integer 6 | % The argument x should be on the closed interval [-1,1]; 7 | % [dy,y]=lepoly(n,x) also returns the values of 1st-order 8 | % derivative of the Legendre polynomial stored in dy 9 | % Last modified on August 30, 2011 10 | % Verified with the chart in http://keisan.casio.com/has10/SpecExec.cgi 11 | 12 | if nargout==1, 13 | if n==0, varargout{1}=ones(size(x)); return; end; 14 | if n==1, varargout{1}=x; return; end; 15 | polylst=ones(size(x)); poly=x; % L_0(x)=1, L_1(x)=x 16 | for k=2:n, % Three-term recurrence relation: 17 | polyn=((2*k-1)*x.*poly-(k-1)*polylst)/k; % kL_k(x)=(2k-1)xL_{k-1}(x)-(k-1)L_{k-2}(x) 18 | polylst=poly; poly=polyn; 19 | end; 20 | varargout{1}=polyn; 21 | end; 22 | 23 | if nargout==2, 24 | if n==0, varargout{2}=ones(size(x)); varargout{1}=zeros(size(x)); return;end; 25 | if n==1, varargout{2}=x; varargout{1}=ones(size(x)); return; end; 26 | 27 | polylst=ones(size(x)); pderlst=zeros(size(x));poly=x; pder=ones(size(x)); 28 | % L_0=1, L_0'=0, L_1=x, L_1'=1 29 | for k=2:n, % Three-term recurrence relation: 30 | polyn=((2*k-1)*x.*poly-(k-1)*polylst)/k; % kL_k(x)=(2k-1)xL_{k-1}(x)-(k-1)L_{k-2}(x) 31 | pdern=pderlst+(2*k-1)*poly; % L_k'(x)=L_{k-2}'(x)+(2k-1)L_{k-1}(x) 32 | polylst=poly; poly=polyn; 33 | pderlst=pder; pder=pdern; 34 | end; 35 | varargout{2}=polyn; varargout{1}=pdern; 36 | end; 37 | 38 | return 39 | 40 | -------------------------------------------------------------------------------- /LGL1/LGL_Dmatrix.m: -------------------------------------------------------------------------------- 1 | %-------------------------------------------------------------------------- 2 | % LGL_Dmatrix.m 3 | % determines approximate differentiation matrix for Legendre-based method 4 | % with LGL nodes 5 | %-------------------------------------------------------------------------- 6 | % D = LGL_Dmatrix(tau) 7 | % tau: LGL nodes 8 | % D: differentiation matrix 9 | %-------------------------------------------------------------------------- 10 | % Primary Contributor: Daniel R. Herber, Graduate Student, University of 11 | % Illinois at Urbana-Champaign 12 | % Link: https://github.com/danielrherber/basic-multiple-interval-pseudospectral 13 | %-------------------------------------------------------------------------- 14 | function D = LGL_Dmatrix(tau) 15 | % number of nodes 16 | N = length(tau)-1; 17 | 18 | % See Page 110 of the book: J. Shen, T. Tang and L. Wang, Spectral Methods: 19 | % Algorithms, Analysis and Applications, Springer Series in Computational 20 | % Mathematics, 41, Springer, 2011. 21 | % Uses the function: lepoly() 22 | % Original function: D = legslbdiff(n,x) located at 23 | % http://www1.spms.ntu.edu.sg/~lilian/bookcodes/legen/legslbdiff.m 24 | n = N + 1; 25 | if n==0, D = []; return; end; % null differentiation matrix 26 | xx = tau; y = lepoly(n-1,xx); 27 | D = (xx./y)*y'-(1./y)*(xx.*y)'; % compute L_{n-1}(x_j) (x_k-x_j)/L_{n-1}(x_k); 28 | % 1/d_{kj} for k not= j (see (3.203)) 29 | D = D + eye(n); % add the identity matrix so that 1./D can be operated 30 | D = 1./D; 31 | D = D - eye(n); 32 | D(1,1) = -n*(n-1)/4; D(n,n) = -D(1,1); % update the diagonal entries 33 | 34 | end -------------------------------------------------------------------------------- /LGL2/LGL_Dmatrix.m: -------------------------------------------------------------------------------- 1 | %-------------------------------------------------------------------------- 2 | % LGL_Dmatrix.m 3 | % determines approximate differentiation matrix for Legendre-based method 4 | % with LGL nodes 5 | %-------------------------------------------------------------------------- 6 | % D = LGL_Dmatrix(tau) 7 | % tau: LGL nodes 8 | % D: differentiation matrix 9 | %-------------------------------------------------------------------------- 10 | % Primary Contributor: Daniel R. Herber, Graduate Student, University of 11 | % Illinois at Urbana-Champaign 12 | % Link: https://github.com/danielrherber/basic-multiple-interval-pseudospectral 13 | %-------------------------------------------------------------------------- 14 | function D = LGL_Dmatrix(tau) 15 | % number of nodes 16 | N = length(tau)-1; 17 | 18 | % See Page 110 of the book: J. Shen, T. Tang and L. Wang, Spectral Methods: 19 | % Algorithms, Analysis and Applications, Springer Series in Computational 20 | % Mathematics, 41, Springer, 2011. 21 | % Uses the function: lepoly() 22 | % Original function: D = legslbdiff(n,x) located at 23 | % http://www1.spms.ntu.edu.sg/~lilian/bookcodes/legen/legslbdiff.m 24 | n = N + 1; 25 | if n==0, D = []; return; end; % null differentiation matrix 26 | xx = tau; y = lepoly(n-1,xx); 27 | D = (xx./y)*y'-(1./y)*(xx.*y)'; % compute L_{n-1}(x_j) (x_k-x_j)/L_{n-1}(x_k); 28 | % 1/d_{kj} for k not= j (see (3.203)) 29 | D = D + eye(n); % add the identity matrix so that 1./D can be operated 30 | D = 1./D; 31 | D = D - eye(n); 32 | D(1,1) = -n*(n-1)/4; D(n,n) = -D(1,1); % update the diagonal entries 33 | 34 | end -------------------------------------------------------------------------------- /LGL1/LGL_nodes.m: -------------------------------------------------------------------------------- 1 | %-------------------------------------------------------------------------- 2 | % LGL_nodes.m 3 | % determines Lagrange-Gauss-Lobatto (LGL) nodes 4 | %-------------------------------------------------------------------------- 5 | % tau = LGL_nodes(N) 6 | % N: number of nodes minus 1, should be an integer greater than 0 7 | % tau: LGL nodes 8 | %-------------------------------------------------------------------------- 9 | % Examples: 10 | % tau = LGL_nodes(1) 11 | % -1 1 12 | % tau = LGL_nodes(2) 13 | % -1 0 1 14 | % tau = LGL_nodes(3) 15 | % -1 -0.44721 0.44721 1 16 | %-------------------------------------------------------------------------- 17 | % Primary Contributor: Daniel R. Herber, Graduate Student, University of 18 | % Illinois at Urbana-Champaign 19 | % Link: https://github.com/danielrherber/basic-multiple-interval-pseudospectral 20 | %-------------------------------------------------------------------------- 21 | function tau = LGL_nodes(N) 22 | % See Page 99 of the book: J. Shen, T. Tang and L. Wang, Spectral Methods: 23 | % Algorithms, Analysis and Applications, Springer Series in Computational 24 | % Mathematics, 41, Springer, 2011. 25 | % Uses the function: lepoly() 26 | % Original function: [varargout] = legslb(n) located at 27 | % http://www1.spms.ntu.edu.sg/~lilian/bookcodes/legen/legslb.m 28 | 29 | % Compute the initial guess of the interior LGL points 30 | thetak = (4*[1:N]-1)*pi/(4*N+2); 31 | sigmak = -(1-(N-1)/(8*N^3)-(39-28./sin(thetak).^2)/(384*N^4)).*cos(thetak); 32 | ze = (sigmak(1:N-1)+sigmak(2:N))/2; 33 | ep = eps*10; % error tolerance for stopping iteration 34 | ze1 = ze+ep+1; 35 | 36 | while max(abs(ze1-ze))>=ep, % Newton's iteration procedure 37 | ze1 = ze; 38 | [dy,y] = lepoly(N,ze); 39 | ze = ze-(1-ze.*ze).*dy./(2*ze.*dy-N*(N+1)*y); % see Page 99 of the book 40 | end; % around 6 iterations are required for n=100 41 | 42 | tau=[-1,ze,1]'; % column vector 43 | 44 | end -------------------------------------------------------------------------------- /LGL2/LGL_nodes.m: -------------------------------------------------------------------------------- 1 | %-------------------------------------------------------------------------- 2 | % LGL_nodes.m 3 | % determines Lagrange-Gauss-Lobatto (LGL) nodes 4 | %-------------------------------------------------------------------------- 5 | % tau = LGL_nodes(N) 6 | % N: number of nodes minus 1, should be an integer greater than 0 7 | % tau: LGL nodes 8 | %-------------------------------------------------------------------------- 9 | % Examples: 10 | % tau = LGL_nodes(1) 11 | % -1 1 12 | % tau = LGL_nodes(2) 13 | % -1 0 1 14 | % tau = LGL_nodes(3) 15 | % -1 -0.44721 0.44721 1 16 | %-------------------------------------------------------------------------- 17 | % Primary Contributor: Daniel R. Herber, Graduate Student, University of 18 | % Illinois at Urbana-Champaign 19 | % Link: https://github.com/danielrherber/basic-multiple-interval-pseudospectral 20 | %-------------------------------------------------------------------------- 21 | function tau = LGL_nodes(N) 22 | % See Page 99 of the book: J. Shen, T. Tang and L. Wang, Spectral Methods: 23 | % Algorithms, Analysis and Applications, Springer Series in Computational 24 | % Mathematics, 41, Springer, 2011. 25 | % Uses the function: lepoly() 26 | % Original function: [varargout] = legslb(n) located at 27 | % http://www1.spms.ntu.edu.sg/~lilian/bookcodes/legen/legslb.m 28 | 29 | % Compute the initial guess of the interior LGL points 30 | thetak = (4*[1:N]-1)*pi/(4*N+2); 31 | sigmak = -(1-(N-1)/(8*N^3)-(39-28./sin(thetak).^2)/(384*N^4)).*cos(thetak); 32 | ze = (sigmak(1:N-1)+sigmak(2:N))/2; 33 | ep = eps*10; % error tolerance for stopping iteration 34 | ze1 = ze+ep+1; 35 | 36 | while max(abs(ze1-ze))>=ep, % Newton's iteration procedure 37 | ze1 = ze; 38 | [dy,y] = lepoly(N,ze); 39 | ze = ze-(1-ze.*ze).*dy./(2*ze.*dy-N*(N+1)*y); % see Page 99 of the book 40 | end; % around 6 iterations are required for n=100 41 | 42 | tau=[-1,ze,1]'; % column vector 43 | 44 | end --------------------------------------------------------------------------------