Exercises 22.1 Using formmlas (2.1) and (22.2), for the integral Ji adz calculat
ID: 2291000 • Letter: E
Question
Exercises 22.1 Using formmlas (2.1) and (22.2), for the integral Ji adz calculate MMs and S, (by hand, but usc a calculator and a lot of digits). Find the percentage error of these approximations, using the exact value. Compare with exercise 21.1. a well-commented MATLAB function program mymidpoint that calculates the midpoint rule approximation for J f on the interval la,bJ with n subintervals. The inputs should be f, a, b and n. 22.2 Write Use your program on the integral Ji Va and the true value of the integral. de to obtain M, and M00 Compare these with exercise 22.1 22.3 Write a well-commented MATLAB function program mysimpson that caleulates the Simpson's rule ap proximation for J f on the interval la, b) with n subintervals. It should call the program mysimpuoightg to produce the coefficients. Use your program on the integral Ji Va da to obtain S4 and S1oo. Compare these with exercise 22.1, exercise 22.2, and the true value of the integral.
Explanation / Answer
content of mysimpweights.m is
----------------------------------------
function c = mysimpweights(n)
if rem(n,2) == 0
%for n intervals there will be n+1 points y0 to yn
c = ones(n+1,1); % initialize weights to 1
for i = 2:n
if rem(i,2)==0
c(i)=4; % W_odd = 4
else
c(i)=2; % W_even = 2
end
end
else
error('n must be even for Simpsons rule') %number of intervals, must be even
end
end
----------------------------------------
mysimpson.m
for n=4 S4
----------------------------------------
n=4; % no of interval
h=(2-1)/n; %step size
x=1:h:2;
S=h*dot(sqrt(x),mysimpweights(n)')/3 %calculating integral sqrt(x) from 1 2 using simpson's rule
----------------------------------------
mysimpson.m
for n=100 S100
----------------------------------------
n=100; % no of interval
h=(2-1)/n; %step size
x=1:h:2;
S=h*dot(sqrt(x),mysimpweights(n)')/3 %calculating integral sqrt(x) from 1 2 using simpson's rule
-----------------------------------------
S4 = 1.2189
S100 = 1.2190
Note: use smart intend to structure code
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.