(Solved) : Gaussquadm Function Xx Ww Gaussquad N B Gaussquad Gaussian Quadrature Integration X W Gaus Q28594702 . . .
![1. Implement Gaussian quadrature in MATLAB, and use it on the test problem 1+2 dr. Use the Gaussian quadrature nodes and weights generated by the file guassquad.m You can use the command [x w] = gaus squad (n, a, b) to generate the Gauss nodes r and the Gauss weights for n-point Gauss quadrature. You can find the exact value of the integral using a u-substitution. The errors for n-point trapezoid rule are included in the table just to illustrate how good Gaussian quadrature can be n trap erro error 0.083 5 0.053 0.037 7 0.027 8 0.021 0.016 10 0.013 6](https://d2vlcm61l7u1fs.cloudfront.net/media%2Fd22%2Fd2264905-4a1b-4273-b447-f94dc89541e8%2FphpaA1J9K.png)
gaussquad.m
function [xx, ww] = gaussquad(n, a, b)
%GAUSSQUAD Gaussian quadrature integration.
%
% [X, W] = GAUSSQUAD(N, A, B) returns the base points X and weightfactors W
% for integrating a function from A to B using an N-point Gaussianquadrature
% rule. This integral is exact for a polynomial of degree 2*N-1 orlower.
%
% [X, W] = GAUSSQUAD(N, C) assumes the interval is from 0 toC.
%
% [X, W] = GAUSSQUAD(N) assumes the interval is from -1 to 1.
%
% GAUSSQUAD(…) with no output arguments plots the base points Xand the
% weight factors W.
% This program is based on an anonymous MATLAB program found onthe MathWorks
% FTP server, ftp://ftp.mathworks.com, extended and rewritten forspeed.
%
% According to the original program, concepts for finding the basepoints and
% weight factors can be found on page 93 of “Methods ofNumerical
% Integration” by Philip Davis and Philip Rabinowitz.
% Author: Peter J. Acklam
% Time-stamp: 2004-02-02 08:36:19 +0100
% E-mail: pjacklam@online.no
% URL: http://home.online.no/~pjacklam
% Check number of input arguments.
% error(nargchk(1, 3, nargin));
% Assign default values to missing arguments.
switch nargin
case 1 % GAUSSQUAD(N)
b = 1;
a = -1;
case 2 % GAUSSQUAD(N, C)
b = a;
a = 0;
end
u = 1 : n-1;
u = u ./ sqrt(4*u.^2 – 1);
% Same as A = diag(u, -1) + diag(u, 1), but faster (noaddition).
A = zeros(n, n);
A( 2 : n+1 : n*(n-1) ) = u;
A( n+1 : n+1 : n^2-1 ) = u;
% Find the base points X and weight factors W for the interval[-1,1].
[v, x] = eig(A);
[x, k] = sort(diag(x));
w = 2 * v(1,k)’.^2;
% Linearly transform from [-1,1] to [a,b].
x = (b – a) / 2 * x + (a + b) / 2; % transform base points X
w = (b – a) / 2 * w; % adjust weigths
% If output arguments are given, return output and exit.
if nargout
xx = x
ww = w
return
end
% Plot base points and weight factors.
userdata = ‘Gaussian Quadrature Base Points and WeightFactors’;
fig = findobj(0, ‘Type’, ‘figure’, ‘UserData’, userdata);
if isempty(fig)
fig = figure(‘UserData’, userdata);
end
figure(fig);
h = plot(x, zeros(1, n), ‘.’, x, w, ‘.’);
set(h, ‘MarkerSize’, 12);
set(gca, ‘XLim’, [a b]);
title(sprintf(‘Gaussian Quadrature with %d points’, n));
xlabel(‘Base points’);
ylabel(‘Weight factors’);
1. Implement Gaussian quadrature in MATLAB, and use it on the test problem 1+2 dr. Use the Gaussian quadrature nodes and weights generated by the file guassquad.m You can use the command [x w] = gaus squad (n, a, b) to generate the Gauss nodes r and the Gauss weights for n-point Gauss quadrature. You can find the exact value of the integral using a u-substitution. The errors for n-point trapezoid rule are included in the table just to illustrate how good Gaussian quadrature can be n trap erro error 0.083 5 0.053 0.037 7 0.027 8 0.021 0.016 10 0.013 6 Show transcribed image text
Expert Answer
Answer to Gaussquadm Function Xx Ww Gaussquad N B Gaussquad Gaussian Quadrature Integration X W Gaus Q28594702 . . .
OR