%Matlab code for finding integration using Simpson Trapizoidal
and Guassian
%Quadrature method
clear all
close all
%Function for which integration have to find
func=@(x) 79.13./(5.30+2.24.*x.*x);
fprintf('Function for which integration have to find
f(x)=')
disp(func)
%Upper and lower limit of integration
a=-0.62; b=1.55;
fprintf('Upper limit a=%f and lower limit b= %f.\n',a,b)
%All n values
n=[2 3 4 6];
%Exact integral value
ext_int=integral(func,a,b);
fprintf('Exact integral value is %f.\n',ext_int)
for i=1:length(n)
%integration using Gaussian Quadrature
method
v_Gauss(i)=Gaussian_quadrature(func,a,b,n(i));
%Printing the result
fprintf('For N=%d\n',n(i))
fprintf('\tIntegration using Gaussian quadrature
method =%2.15f\n\n',v_Gauss(i))
end
%Function for which integration have to find
func=@(x) 63.52./(4.07+2.23.*x.*x);
%All n values
n=[4 8];
fprintf('Function for which integration have to find
f(x)=')
disp(func)
%Upper and lower limit of integration
a=-0.15; b=1.56;
fprintf('Upper limit a=%f and lower limit b= %f.\n',a,b)
for i=1:length(n)
%integration using Gaussian Quadrature
method
v_trap(i)=trapizoidal(func,a,b,n(i));
%Printing the result
fprintf('For N=%d\n',n(i))
fprintf('\tIntegration using Trapizoidal method
=%2.15f\n\n',v_trap(i))
end
err=1; nn=4;
while err>=0.0000005
v_trap1=trapizoidal(func,a,b,nn);
v_trap2=trapizoidal(func,a,b,nn+1);
err=abs((v_trap2-v_trap1)/v_trap2);
nn=nn+1;
end
fprintf('In Trapizoidal method Final result in this process is %f
with n=%d\n',v_trap2,nn)
%Function for which integration have to find
func=@(x) 59.18./(8.79+1.24.*x.*x);
%All n values
n=[4 8];
fprintf('Function for which integration have to find
f(x)=')
disp(func)
%Upper and lower limit of integration
a=-0.92; b=1.37;
fprintf('Upper limit a=%f and lower limit b= %f.\n',a,b)
for i=1:length(n)
%integration using Gaussian Quadrature
method
v_simp(i)=simpson(func,a,b,n(i));
%Printing the result
fprintf('For N=%d\n',n(i))
fprintf('\tIntegration using Simpson method
=%2.15f\n\n',v_simp(i))
end
err=1; nn=4;
while err>=0.0000005
v_trap1=simpson(func,a,b,nn);
v_trap2=simpson(func,a,b,nn+2);
err=abs((v_trap2-v_trap1)/v_trap2);
nn=nn+1;
end
fprintf('In Trapizoidal method Final result in this process is %f
with n=%d\n',v_trap2,nn)
%%Matlab function for Trapizoidal integration
function val=trapizoidal(func,a,b,N)
% func is the function for integration
% a is the lower limit of integration
% b is the upper limit of integration
% N number of rectangles to be used
val=0;
%splits interval a to b into N+1
subintervals
xx=linspace(a,b,N+1);
dx=xx(2)-xx(1); %x interval
%loop for Riemann integration
for
i=2:length(xx)-1
xx1=xx(i);
val=val+dx*double(func(xx1));
end
val=val+dx*(0.5*double(func(xx(1)))+0.5*double(func(xx(end))));
end
%%Matlab function for Simpson integration
function val=simpson(func,a,b,N)
% func is the function for integration
% a is the lower limit of integration
% b is the upper limit of integration
% N number of rectangles to be used
%splits interval a to b into N+1
subintervals
xx=linspace(a,b,N+1);
dx=xx(2)-xx(1); %x interval
val=(dx/3)*(double(func(xx(1)))+double(func(xx(end))));
%loop for Riemann integration
for
i=2:length(xx)-1
xx1=xx(i);
if mod(i,2)==0
val=val+(dx/3)*4*double(func(xx1));
else
val=val+(dx/3)*2*double(func(xx1));
end
end
end
%Function for Gaussian quadrature
function val=Gaussian_quadrature(func,a,b,N)
%
% 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);
%
N=N-1;
N1=N+1; N2=N+2;
xu=linspace(-1,1,N1)';
% Initial guess
y=cos((2*(0:N)'+1)*pi/(2*N+2))+(0.27/N1)*sin(pi*xu*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;
val=0;
for ii=1:length(x)
val=val+w(ii)*func(x(ii));
end
end
%%%%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%%%%
Multiple choices Use Gaussian Quadrature to find the value of the integral of: f(x) = 79.13...
3. Approximate the following integral using the two-point Gaussian quadrature rule 2 (x + a)?e(x-1)2-Bdx
Show work by hand and also using MATLAB code.
Model 1 Given a polynomial f(x) Write a first-order approximation of f(x), given the value of f(x) at two points Plot the polynomial and the first-order approximation on a graph Write a second-order approximation of f(x), given the value at three points. Plot the polynomial, the first-order and second-order approximations on a graph Find the integral Exactly Using trapezoidal rule Using composite trapezoidal rule Using Simpson's 1/3 rule . Calculate the...
Problem 1. Consider an integral of the form 1 (f) = f0 /if (x) dx. Show that the one-point Gaussian quadrature rule for integrals of the form Jo VRf (x) dx has the node x1and weight w3
Problem 1. Consider an integral of the form 1 (f) = f0 /if (x) dx. Show that the one-point Gaussian quadrature rule for integrals of the form Jo VRf (x) dx has the node x1and weight w3
If f has a continuous second derivative on [a, b], then the error E in approximating by the Trapezoidal Rule is (b- a 12n rmax x)1. asxsb. JE s Moreover, if f has a continuous fourth derivative on [a, bl, then the error E in approximating by fix) dx Simpson's Rule is b-a)s 180a lrmax (x. asxsb. Use these to find the minimum integer n such that the error in the approximation of the definite integral is less than or...
Alpha=9 beta=3 yazarsin
3. ( 15p.) Approximate the following integral using the two-point Gaussian quadrature rule À (x + a)?e(x-1)2-3 da 4. Consider the following system.
Use n = 4 to approximate the value of the integral by the following methods: (a) the trapezoidal rule, and (b) Simpson's rule. (c) Find the exact value by integration. 1 - x 3x e dx 0 (a) Use the trapezoidal rule to approximate the integral. 1 Joxe -x² dx~ 0 (Round the final answer to three decimal places as needed. Round all intermediate values to four decimal places as needed.)
3. (15p.) Approximate the following integral using the two-point Gaussian quadrature rule | (2 + a)*e¢8–1)-+de 2 B=1 ju a=8 0
If f has a continuous second derivative on tə, b), then the error E in approximating f(x) dx by the Trapezoidal Rule is IELS (-a) [max 1f"(x)), a sxs b. 12n2 Moreover, if f has a continuous fourth derivative on (a, b), then the error E in approximating Rx) dx by Simpson's Rule is IES (6-a) [max 1(1)(x)), a sxs b. 1804 Use these to find the minimum Integer n such that the error in the approximation of the definite...
3. (1 point) This is 2-point Gaussian Quadrature for any f(x) dx. The weights and the nodes do not change when we integrate a new function. So...does it work? Does this actually lead to a method that is good for intergating functions in general? Use 2-point Gaussian quadrature to approximate the following integral: I e-ra da. -1 The exact value of this integral rounded to 2 decimal places is 1.49. Show your work when computing the approximation. Report the answer...
Problem 4 A definite integral I is given as .b I=| f(x) dr a=0 b=2 f(x) = e-r' ; ; ; Evaluate the integral using the three-point Gaussian quadrature method Solution:
Problem 4 A definite integral I is given as .b I=| f(x) dr a=0 b=2 f(x) = e-r' ; ; ; Evaluate the integral using the three-point Gaussian quadrature method Solution: