Hello, I\'ve been having touble trying to find out how to write the MATLAB code
ID: 3403802 • Letter: H
Question
Hello, I've been having touble trying to find out how to write the MATLAB code for the function given as:
Function c = reimsum(P,a,b,n)
which accepts as inputs, a polynomial P and three scalars a, b, and n. Returning a scalar c which gives the Riemann sum approximation of the definite integral. So the function will use Riemann sums to approximate the definite integral of the polynomial on the interval [a,b]. Then it will use n subintervals of equal length with the value of the function taken at the midpoint of each subinterval.
Since the polynomials must be typed through the standard basis, the commandx=sym(‘x’) is needed from my understanding.
The function could use for loop, the commandsym2poly, and the MATLAB function polyval.
Explanation / Answer
function r=reimsum(f,a,b,n)
dx=(b-a)/n;
% initialize r to f(a) (the left-end point of the first sub-interval
% [a,a+dx])
r=f(a);
% need only consider the n-1 remaining sub-intervals
for k=1:n
c=a+k*dx;
r=r+f(c);
end
r=dx*r;
end
F = @(x) 2*x;
>> reimsum(F,0,1,10)
ans =
1.1000
F = @(x) (2.*x.^2+x+1);
>> reimsum(F,0,1,10)
ans =
2.4200
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.