Problem 2: Let a1 aud&-7. Integrate each of the following functious aualytically
ID: 3919958 • Letter: P
Question
Problem 2: Let a1 aud&-7. Integrate each of the following functious aualytically. Then appl the trapezoidal rule, Simpson's 1/ u and Simpson's 3/8 rule onea a) f(z)-7+2 Comment on your results Problem 3: Integrate the functious from parts e) and d) in problen 2 from-1 to b-7 using Romberg integration (two iterations, i.e., k-2). Use step sizes of hi-6 and h2-3. Comment on your bonus points: Integrate the function fro part d)smberg integration with results. 3 iterations. Ilse, h,-1.5. Problem 4: Apply the 2-point Gauss-Legendre formula to integrate each of the functions in prob- lem 2. Comment on your results.Explanation / Answer
If you post more than 1 question, as per cegg guidelines I have to solve only first question.
Ques 2.
(a)
----------------------------simpson_one_third_integration.m----------------
function [simp_val] = simpson_one_third_integration(y , h)
% store the sum of odd terms
sum_odd_terms = 0;
% calculate the sum of odd terms
for i = 2 : 2 : length(y) - 1
sum_odd_terms = sum_odd_terms + y(i);
end
% store the sum of even terms
sum_even_terms = 0;
% calculate the sum of even terms
for i = 3 : 2 : length(y) - 2
sum_even_terms = sum_even_terms + y(i);
end
simp_val = ( h / 3 ) * ( ( y(1) + y( length(y) ) ) + 4 * sum_odd_terms + 2 * sum_even_terms );
end
----------------------------------------simpson_three_eight_rule.m--------------------------------
function [simp_val] = simpson_three_eight_rule(y , h)
% store the value of y3 + y6 + ... + y(n-3)
sum3 = 0;
% calculate the value of y3 + y6 + ... + y(n-3)
for i = 4 : 3 : length(y) - 3
sum3 = sum3 + y(i);
end
% store the value of y1 + y2 + y3 + ... + y(n-1)
sum1 = 0;
% calculate the value of y1 + y2 + y3 + ... + y(n-1)
for i = 2 : length(y) - 1
sum1 = sum1 + y(i);
end
% store the value of y1 + y2 + y4 + y5 + ... + y(n-1)
sum2 = sum1 - sum3;
simp_val = ( 3 * h / 8 ) * ( ( y(1) + y( length(y) ) ) + 3 * sum2 + 2 * sum3 );
end
---------------------------------trapezoidal_integration.m---------------------------------
% h
% Trapezoidal Integration = ---[ ( y0 + yn ) + 2 * ( y1 + y2 + ... + y(n-1) ) ]
% 2
%
% y is the vector of values of function f(x) at points from l_bound to r_bound
% h is the width of the interval
function [trap_val] = trapezoidal_integration( y, h)
% store the sum y1 + y2 + ... + y(n-1)
sum1 = 0;
% calculate the sum y1 + y2 + ... + y(n-1)
for i = 2 : length(y) - 1
sum1 = sum1 + y(i);
end
% find the value of integration
trap_val = ( h / 2 ) * ( ( y(1) + y( length(y) ) ) + 2 * sum1 );
end
-------------------main.m-----------------
% h
% Simpson 1/3 rd Integration = ---[ ( y0 + yn ) + 4 * ( y1 + y3 + y5 + ... + y(n-1) ) + 2 * ( y2 + y4 + y6 + ... + y(n - 2) ) ) ]
% 3
%
% Integrate 7x + 2 in [1 , 7] with 9 intervals
%
l_bound = 1;
r_bound = 7;
% store the number of intervals
n = 8;
% create a vector for x in [1 , 7] with 9 intervals
x = linspace( l_bound , r_bound , n + 1 );
% create a vector of size 1 x length(x) initilized to 0
y = zeros(1 , length(x));
% create a vector of values of e^(-x^2)
for i = 1 : length(x)
y(i) = 7 * x(i) + 2;
end
% the integration is fom 0 to 6 where width of interval is 1
h = ( r_bound - l_bound ) / n;
simp_val = simpson_one_third_integration( y , h );
fprintf('Simpson 1/3 Integration : %f ', simp_val);
% 3h
% Simpson 1/3 rd Integration = -----[ ( y0 + yn ) + 3 * ( y1 + y2 + y4 + y5 + ... + y(n-1) ) + 2 * ( y3 + y6 + ... + y(n - 3) ) ) ]
% 8
%
% Integrate 7x + 2 in [1 , 7] with 9 intervals
%
l_bound = 1;
r_bound = 7;
% store the number of intervals
n = 8;
% create a vector for x in [1 , 7] with 9 intervals
x = linspace( l_bound , r_bound , n + 1 );
% create a vector of size 1 x length(x) initilized to 0
y = zeros(1 , length(x));
% create a vector of values of ( sinx - logx + e^x )
for i = 1 : length(x)
y(i) = 7 * x(i) + 2;
end
% the integration is fom 0 to 6 where no of interval is n
h = ( r_bound - l_bound ) / n;
simp_val = simpson_three_eight_rule( y , h );
fprintf('Simpson 3/8 Integration : %f ', simp_val);
% h
% Trapezoidal Integration = ---[ ( y0 + yn ) + 2 * ( y1 + y2 + ... + y(n-1) ) ]
% 2
%
% Integrate 7x + 2 in interval [1 , 7]
%
l_bound = 1;
r_bound = 7;
% store the number of intervals
n = 8;
% create a vector for x in [1 , 7] with 9 intervals
x = linspace( l_bound , r_bound , n + 1 );
% create a vector of size 1 x length(x) initilized to 0
y = zeros(1 , length(x));
% create a vector of values of 1 / ( 1 + x^2 )
for i = 1 : length(x)
y(i) = 7 * x(i) + 2;
end
% the integration is fom l_bound to r_bound where no of interval is n
h = ( r_bound - l_bound ) / n;
trap_val = trapezoidal_integration( y , h );
fprintf('Trapezoidal Integration : %f ', trap_val);
(b)
----------------------------simpson_one_third_integration.m----------------
function [simp_val] = simpson_one_third_integration(y , h)
% store the sum of odd terms
sum_odd_terms = 0;
% calculate the sum of odd terms
for i = 2 : 2 : length(y) - 1
sum_odd_terms = sum_odd_terms + y(i);
end
% store the sum of even terms
sum_even_terms = 0;
% calculate the sum of even terms
for i = 3 : 2 : length(y) - 2
sum_even_terms = sum_even_terms + y(i);
end
simp_val = ( h / 3 ) * ( ( y(1) + y( length(y) ) ) + 4 * sum_odd_terms + 2 * sum_even_terms );
end
----------------------------------------simpson_three_eight_rule.m--------------------------------
function [simp_val] = simpson_three_eight_rule(y , h)
% store the value of y3 + y6 + ... + y(n-3)
sum3 = 0;
% calculate the value of y3 + y6 + ... + y(n-3)
for i = 4 : 3 : length(y) - 3
sum3 = sum3 + y(i);
end
% store the value of y1 + y2 + y3 + ... + y(n-1)
sum1 = 0;
% calculate the value of y1 + y2 + y3 + ... + y(n-1)
for i = 2 : length(y) - 1
sum1 = sum1 + y(i);
end
% store the value of y1 + y2 + y4 + y5 + ... + y(n-1)
sum2 = sum1 - sum3;
simp_val = ( 3 * h / 8 ) * ( ( y(1) + y( length(y) ) ) + 3 * sum2 + 2 * sum3 );
end
---------------------------------trapezoidal_integration.m---------------------------------
% h
% Trapezoidal Integration = ---[ ( y0 + yn ) + 2 * ( y1 + y2 + ... + y(n-1) ) ]
% 2
%
% y is the vector of values of function f(x) at points from l_bound to r_bound
% h is the width of the interval
function [trap_val] = trapezoidal_integration( y, h)
% store the sum y1 + y2 + ... + y(n-1)
sum1 = 0;
% calculate the sum y1 + y2 + ... + y(n-1)
for i = 2 : length(y) - 1
sum1 = sum1 + y(i);
end
% find the value of integration
trap_val = ( h / 2 ) * ( ( y(1) + y( length(y) ) ) + 2 * sum1 );
end
-----------------------------main.m-------------------------
% h
% Simpson 1/3 rd Integration = ---[ ( y0 + yn ) + 4 * ( y1 + y3 + y5 + ... + y(n-1) ) + 2 * ( y2 + y4 + y6 + ... + y(n - 2) ) ) ]
% 3
%
% Integrate 3x^2 + 6x + 2 in [1 , 7] with 9 intervals
%
l_bound = 1;
r_bound = 7;
% store the number of intervals
n = 8;
% create a vector for x in [1 , 7] with 9 intervals
x = linspace( l_bound , r_bound , n + 1 );
% create a vector of size 1 x length(x) initilized to 0
y = zeros(1 , length(x));
% create a vector of values of 3x^2 + 6x + 2
for i = 1 : length(x)
y(i) = 3 * x(i) * x(i) + 6 * x(i) + 2;
end
% the integration is fom 0 to 6 where width of interval is 1
h = ( r_bound - l_bound ) / n;
simp_val = simpson_one_third_integration( y , h );
fprintf('Simpson 1/3 Integration : %f ', simp_val);
% 3h
% Simpson 1/3 rd Integration = -----[ ( y0 + yn ) + 3 * ( y1 + y2 + y4 + y5 + ... + y(n-1) ) + 2 * ( y3 + y6 + ... + y(n - 3) ) ) ]
% 8
%
% Integrate 7x + 2 in [1 , 7] with 9 intervals
%
l_bound = 1;
r_bound = 7;
% store the number of intervals
n = 8;
% create a vector for x in [1 , 7] with 9 intervals
x = linspace( l_bound , r_bound , n + 1 );
% create a vector of size 1 x length(x) initilized to 0
y = zeros(1 , length(x));
% create a vector of values of 3x^2 + 6x + 2
for i = 1 : length(x)
y(i) = 3 * x(i) * x(i) + 6 * x(i) + 2;
end
% the integration is fom 0 to 6 where no of interval is n
h = ( r_bound - l_bound ) / n;
simp_val = simpson_three_eight_rule( y , h );
fprintf('Simpson 3/8 Integration : %f ', simp_val);
% h
% Trapezoidal Integration = ---[ ( y0 + yn ) + 2 * ( y1 + y2 + ... + y(n-1) ) ]
% 2
%
% Integrate 7x + 2 in interval [1 , 7]
%
l_bound = 1;
r_bound = 7;
% store the number of intervals
n = 8;
% create a vector for x in [1 , 7] with 9 intervals
x = linspace( l_bound , r_bound , n + 1 );
% create a vector of size 1 x length(x) initilized to 0
y = zeros(1 , length(x));
% create a vector of values of 3x^2 + 6x + 2
for i = 1 : length(x)
y(i) = 3 * x(i) * x(i) + 6 * x(i) + 2;
end
% the integration is fom l_bound to r_bound where no of interval is n
h = ( r_bound - l_bound ) / n;
trap_val = trapezoidal_integration( y , h );
fprintf('Trapezoidal Integration : %f ', trap_val);
Sample Output
Simpson 1/3 Integration : 498.000000
Simpson 3/8 Integration : 518.783203
Trapezoidal Integration : 499.687500
(c)
----------------------------simpson_one_third_integration.m----------------
function [simp_val] = simpson_one_third_integration(y , h)
% store the sum of odd terms
sum_odd_terms = 0;
% calculate the sum of odd terms
for i = 2 : 2 : length(y) - 1
sum_odd_terms = sum_odd_terms + y(i);
end
% store the sum of even terms
sum_even_terms = 0;
% calculate the sum of even terms
for i = 3 : 2 : length(y) - 2
sum_even_terms = sum_even_terms + y(i);
end
simp_val = ( h / 3 ) * ( ( y(1) + y( length(y) ) ) + 4 * sum_odd_terms + 2 * sum_even_terms );
end
----------------------------------------simpson_three_eight_rule.m--------------------------------
function [simp_val] = simpson_three_eight_rule(y , h)
% store the value of y3 + y6 + ... + y(n-3)
sum3 = 0;
% calculate the value of y3 + y6 + ... + y(n-3)
for i = 4 : 3 : length(y) - 3
sum3 = sum3 + y(i);
end
% store the value of y1 + y2 + y3 + ... + y(n-1)
sum1 = 0;
% calculate the value of y1 + y2 + y3 + ... + y(n-1)
for i = 2 : length(y) - 1
sum1 = sum1 + y(i);
end
% store the value of y1 + y2 + y4 + y5 + ... + y(n-1)
sum2 = sum1 - sum3;
simp_val = ( 3 * h / 8 ) * ( ( y(1) + y( length(y) ) ) + 3 * sum2 + 2 * sum3 );
end
---------------------------------trapezoidal_integration.m---------------------------------
% h
% Trapezoidal Integration = ---[ ( y0 + yn ) + 2 * ( y1 + y2 + ... + y(n-1) ) ]
% 2
%
% y is the vector of values of function f(x) at points from l_bound to r_bound
% h is the width of the interval
function [trap_val] = trapezoidal_integration( y, h)
% store the sum y1 + y2 + ... + y(n-1)
sum1 = 0;
% calculate the sum y1 + y2 + ... + y(n-1)
for i = 2 : length(y) - 1
sum1 = sum1 + y(i);
end
% find the value of integration
trap_val = ( h / 2 ) * ( ( y(1) + y( length(y) ) ) + 2 * sum1 );
end
------------------------------main.m----------------------------
% h
% Simpson 1/3 rd Integration = ---[ ( y0 + yn ) + 4 * ( y1 + y3 + y5 + ... + y(n-1) ) + 2 * ( y2 + y4 + y6 + ... + y(n - 2) ) ) ]
% 3
%
% Integrate5x^3 + 2x^2 + x + 7 in [1 , 7] with 9 intervals
%
l_bound = 1;
r_bound = 7;
% store the number of intervals
n = 8;
% create a vector for x in [1 , 7] with 9 intervals
x = linspace( l_bound , r_bound , n + 1 );
% create a vector of size 1 x length(x) initilized to 0
y = zeros(1 , length(x));
% create a vector of values of 5x^3 + 2x^2 + x + 7
for i = 1 : length(x)
y(i) = 5 * x(i) * x(i) * x(i) + 2 * x(i) * x(i) + x(i) + 7;
end
% the integration is fom 0 to 6 where width of interval is 1
h = ( r_bound - l_bound ) / n;
simp_val = simpson_one_third_integration( y , h );
fprintf('Simpson 1/3 Integration : %f ', simp_val);
% 3h
% Simpson 1/3 rd Integration = -----[ ( y0 + yn ) + 3 * ( y1 + y2 + y4 + y5 + ... + y(n-1) ) + 2 * ( y3 + y6 + ... + y(n - 3) ) ) ]
% 8
%
% Integrate 5x^3 + 2x^2 + x + 7 in [1 , 7] with 9 intervals
%
l_bound = 1;
r_bound = 7;
% store the number of intervals
n = 8;
% create a vector for x in [1 , 7] with 9 intervals
x = linspace( l_bound , r_bound , n + 1 );
% create a vector of size 1 x length(x) initilized to 0
y = zeros(1 , length(x));
% create a vector of values of 5x^3 + 2x^2 + x + 7
for i = 1 : length(x)
y(i) = 5 * x(i) * x(i) * x(i) + 2 * x(i) * x(i) + x(i) + 7;
end
% the integration is fom 0 to 6 where no of interval is n
h = ( r_bound - l_bound ) / n;
simp_val = simpson_three_eight_rule( y , h );
fprintf('Simpson 3/8 Integration : %f ', simp_val);
% h
% Trapezoidal Integration = ---[ ( y0 + yn ) + 2 * ( y1 + y2 + ... + y(n-1) ) ]
% 2
%
% Integrate 5x^3 + 2x^2 + x + 7 in interval [1 , 7]
%
l_bound = 1;
r_bound = 7;
% store the number of intervals
n = 8;
% create a vector for x in [1 , 7] with 9 intervals
x = linspace( l_bound , r_bound , n + 1 );
% create a vector of size 1 x length(x) initilized to 0
y = zeros(1 , length(x));
% create a vector of values of 5x^3 + 2x^2 + x + 7
for i = 1 : length(x)
y(i) = 5 * x(i) * x(i) * x(i) + 2 * x(i) * x(i) + x(i) + 7;
end
% the integration is fom l_bound to r_bound where no of interval is n
h = ( r_bound - l_bound ) / n;
trap_val = trapezoidal_integration( y , h );
fprintf('Trapezoidal Integration : %f ', trap_val);
Sample Output
Simpson 1/3 Integration : 3294.000000
Simpson 3/8 Integration : 3428.854980
Trapezoidal Integration : 3328.875000
(d)
----------------------------simpson_one_third_integration.m----------------
function [simp_val] = simpson_one_third_integration(y , h)
% store the sum of odd terms
sum_odd_terms = 0;
% calculate the sum of odd terms
for i = 2 : 2 : length(y) - 1
sum_odd_terms = sum_odd_terms + y(i);
end
% store the sum of even terms
sum_even_terms = 0;
% calculate the sum of even terms
for i = 3 : 2 : length(y) - 2
sum_even_terms = sum_even_terms + y(i);
end
simp_val = ( h / 3 ) * ( ( y(1) + y( length(y) ) ) + 4 * sum_odd_terms + 2 * sum_even_terms );
end
----------------------------------------simpson_three_eight_rule.m--------------------------------
function [simp_val] = simpson_three_eight_rule(y , h)
% store the value of y3 + y6 + ... + y(n-3)
sum3 = 0;
% calculate the value of y3 + y6 + ... + y(n-3)
for i = 4 : 3 : length(y) - 3
sum3 = sum3 + y(i);
end
% store the value of y1 + y2 + y3 + ... + y(n-1)
sum1 = 0;
% calculate the value of y1 + y2 + y3 + ... + y(n-1)
for i = 2 : length(y) - 1
sum1 = sum1 + y(i);
end
% store the value of y1 + y2 + y4 + y5 + ... + y(n-1)
sum2 = sum1 - sum3;
simp_val = ( 3 * h / 8 ) * ( ( y(1) + y( length(y) ) ) + 3 * sum2 + 2 * sum3 );
end
---------------------------------trapezoidal_integration.m---------------------------------
% h
% Trapezoidal Integration = ---[ ( y0 + yn ) + 2 * ( y1 + y2 + ... + y(n-1) ) ]
% 2
%
% y is the vector of values of function f(x) at points from l_bound to r_bound
% h is the width of the interval
function [trap_val] = trapezoidal_integration( y, h)
% store the sum y1 + y2 + ... + y(n-1)
sum1 = 0;
% calculate the sum y1 + y2 + ... + y(n-1)
for i = 2 : length(y) - 1
sum1 = sum1 + y(i);
end
% find the value of integration
trap_val = ( h / 2 ) * ( ( y(1) + y( length(y) ) ) + 2 * sum1 );
end
-------------------------------main.m--------------------------------
% h
% Simpson 1/3 rd Integration = ---[ ( y0 + yn ) + 4 * ( y1 + y3 + y5 + ... + y(n-1) ) + 2 * ( y2 + y4 + y6 + ... + y(n - 2) ) ) ]
% 3
%
% Integrate x^4 + 7x^3 + x^2 + 4x + 8 in [1 , 7] with 9 intervals
%
l_bound = 1;
r_bound = 7;
% store the number of intervals
n = 8;
% create a vector for x in [1 , 7] with 9 intervals
x = linspace( l_bound , r_bound , n + 1 );
% create a vector of size 1 x length(x) initilized to 0
y = zeros(1 , length(x));
% create a vector of values of x^4 + 7x^3 + x^2 + 4x + 8
for i = 1 : length(x)
y(i) = x(i) * x(i) * x(i) * x(i) + 7 * x(i) * x(i) * x(i) + x(i) * x(i) + 4 * x(i) + 8;
end
% the integration is fom 0 to 6 where width of interval is 1
h = ( r_bound - l_bound ) / n;
simp_val = simpson_one_third_integration( y , h );
fprintf('Simpson 1/3 Integration : %f ', simp_val);
% 3h
% Simpson 1/3 rd Integration = -----[ ( y0 + yn ) + 3 * ( y1 + y2 + y4 + y5 + ... + y(n-1) ) + 2 * ( y3 + y6 + ... + y(n - 3) ) ) ]
% 8
%
% Integrate x^4 + 7x^3 + x^2 + 4x + 8 in [1 , 7] with 9 intervals
%
l_bound = 1;
r_bound = 7;
% store the number of intervals
n = 8;
% create a vector for x in [1 , 7] with 9 intervals
x = linspace( l_bound , r_bound , n + 1 );
% create a vector of size 1 x length(x) initilized to 0
y = zeros(1 , length(x));
% create a vector of values of x^4 + 7x^3 + x^2 + 4x + 8
for i = 1 : length(x)
y(i) = x(i) * x(i) * x(i) * x(i) + 7 * x(i) * x(i) * x(i) + x(i) * x(i) + 4 * x(i) + 8;
end
% the integration is fom 0 to 6 where no of interval is n
h = ( r_bound - l_bound ) / n;
simp_val = simpson_three_eight_rule( y , h );
fprintf('Simpson 3/8 Integration : %f ', simp_val);
% h
% Trapezoidal Integration = ---[ ( y0 + yn ) + 2 * ( y1 + y2 + ... + y(n-1) ) ]
% 2
%
% Integrate x^4 + 7x^3 + x^2 + 4x + 8 in interval [1 , 7]
%
l_bound = 1;
r_bound = 7;
% store the number of intervals
n = 8;
% create a vector for x in [1 , 7] with 9 intervals
x = linspace( l_bound , r_bound , n + 1 );
% create a vector of size 1 x length(x) initilized to 0
y = zeros(1 , length(x));
% create a vector of values of x^4 + 7x^3 + x^2 + 4x + 8
for i = 1 : length(x)
y(i) = x(i) * x(i) * x(i) * x(i) + 7 * x(i) * x(i) * x(i) + x(i) * x(i) + 4 * x(i) + 8;
end
% the integration is fom l_bound to r_bound where no of interval is n
h = ( r_bound - l_bound ) / n;
trap_val = trapezoidal_integration( y , h );
fprintf('Trapezoidal Integration : %f ', trap_val);
Sample Output
Simpson 1/3 Integration : 7819.453125
Simpson 3/8 Integration : 8124.432495
Trapezoidal Integration : 7931.074219
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.