%Matlab code for finding integration using different
method
clear all
close all
%function for which integration have to do
f1=@(x) exp(x);
%Displaying the function for which integration have to do
fprintf('Function for which integration have to do f(x)=')
disp(f1)
%upper and lower limit
a=0; b=1;
%Function for exact integration value
f_ext=exp(1)-exp(0);
fprintf('Exact integration value for the function %f\n',f_ext)
%loop for all integration values
for i=1:20
n=2^i+1;
val_simp=Simpson(f1,a,b,n);
val_trap=Trapizoidal(f1,a,b,n);
val_mid=Midpoint(f1,a,b,n);
%error for all method
err_simp(i)=abs(f_ext-val_simp);
err_trap(i)=abs(f_ext-val_trap);
err_mid(i)=abs(f_ext-val_mid);
k(i)=n;
end
%Log log plot for error vs. step size
loglog(k,err_simp)
hold on
loglog(k,err_trap)
loglog(k,err_mid)
legend('Simpson method','Trapizoidal method','Mid point')
xlabel('Number of steps')
ylabel('Error of integration')
title('log plot of error vs steps using different method')
%integral value using matlab built in integral
val_int=integral(f1,a,b);
%error in integration
fprintf('Error in integration for matlab built in integral is
%e.\n',abs(val_int-f_ext))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for mid point integration
function val=Midpoint(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=1:length(xx)-1
xx1=(xx(i)+xx(i+1))/2;
val=val+dx*double(func(xx1));
end
end
%%Matlab function for mid point 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 mid point 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
%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%
1. Approximate the following integral, exp(r) using the composite midpoint rule, composite trapez...
Numerical Methods Consider the integral 2 (a) [16 marks] Use the composite Simpson's rule with four intervals to calculate (by hand) approximate value of the integral Calculate the maximum value of the error in your approximation, and compare it with the true error. (b) 19 marks] Determine the number of subintervals n and the step size h so that the composite Simpson's rule for n subintervals can be used to compute the given integral with an accuracy of 5 ×...
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with the specified value of n. (Round your answers to six decimal places.) 5 3 cos(6x) n = 8 dx, X 1 (a) the Trapezoidal Rule (b) the Midpoint Rule (c) Simpson's Rule
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with the specified value of n. (Round your answers to six decimal places.) 4 In(1 + ex) dx, n = 8 Jo (a) the Trapezoidal Rule X (b) the Midpoint Rule (c) Simpson's Rule 8.804229
4. This question is about using the composite Simpson's Rule to estimate the integral 1 = (exp() dr to ten decimal places. (a) Enter and save the following Matlab function function y = f(x) y =exp(x/2); end [O marks) (b) Now complete the following Matlab function function y = compSR (a,b,N) end The function is to return the estimate of I found by applying Simpson's Rule N times. The Matlab function from the previous part of the question should be...
equidistant subdivision of [0, 2] in 20 subintervals to approximate 1. Using an sin(z) dr by the midpoint rule, estimate the absolute total error. equidistant subdivision of [0, 2] in 20 subintervals to approximate 1. Using an sin(z) dr by the midpoint rule, estimate the absolute total error.
Objective The usual procedure for evaluating a definite integral is to find the antiderivative of the integrand and apply the Fundamental Theorem of Calculus. However, if an antiderivative of the integrand cannot be found, then we must settle for a numerical approximation of the integral. The objective of this project is to illustrate the Trapezoidal Rule and Simpson's Rule. Description To get started, read the section 8.6 in the text. In this project we will illustrate and compare Riemann sum,...
Let EM represent the error in using the Midpoint Rule with subintervals to approximate S. f(x) dx. Then K(b - a) TEM 24n2 where K is the maximum number that the absolute value of IF"(x) achieves for asx<b. Use this inequality to find the minimum number, 17 of subintervals necessary to guarantee that the Midpoint Rule will approximate the integral dx to be accurate to within 0.001. 80 O 358 253 114
Help Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with the specified value of n. (Round your answers to six decimal places.) V 1 + x2 dx, n = 8 Jo (a) the Trapezoidal Rule 2.41379 (b) the Midpoint Rule 1.164063 (c) Simpson's Rule 1.17
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with the specified value of n. (Round your answers to six decimal places.) 1/2 0 10 sin(x2) dx, n = 4
Use the Trapezoidal Rule, the Midpoint Rule, and Simpson's Rule to approximate the given integral with the specified value of n. (Round your answers to six decimal places. pi/2 3sqrt(1 + cos(x))dr, n = 4 0