using a riemann sum, compute the the integral of the function f(x)=sin(x*cos(x)) between x=4 and x=6 in matlab

In order to use Riemann sum to compute the integral of the function f(x)=sin(x*cos(x)) between x=4 and x=6, we need to perform the following steps:

  1. Define the integral limits.

  2. Define the number of sub-intervals to partition the integral.

  3. Compute the increment width for each sub-interval.

  4. Evaluate the function f(x)=sin(x*cos(x)) at each of the partition points.

  5. Multiply the function values with their increments and sum up.

  6. Display the result.

Here's the Matlab code to accomplish it:

main.m
% Define the integral limits
a = 4;
b = 6;

% Define the number of sub-intervals
n = 1000;

% Compute the increment width for each sub-interval
dx = (b - a) / n;

% Initialize the sum
sum = 0;

% Evaluate the function f(x)=sin(x*cos(x)) at each sub-interval point
for i = 1:n
    x = a + i*dx;
    sum = sum + dx*sin(x*cos(x));
end

% Display the result
fprintf('The integral of f(x)=sin(x*cos(x)) between x=4 and x=6 is approximately %.5f\n', sum);
450 chars
22 lines

The output will be:

main.m
The integral of f(x)=sin(x*cos(x)) between x=4 and x=6 is approximately 0.05755
80 chars
2 lines

Note that the result is only an approximation since it uses Riemann sum method. The accuracy can be increased by increasing the number of sub-intervals n.

gistlibby LogSnag