Integration over discontinuities. Integrate the function f(x) = 0 for x < 1, f(x) = exp(x − 1) + exp(2 − x) for 1 ≤ x ≤ 2, f(x) = 0 for x > 2 from x = 0 to x = 3, for varying step sizes, using the trapezoidal method. Plot the error as a function of step size. A Matlab function calculating this function, f_r13a.m, is available on Blackboard. Note that the integral is equal to 2e − 1. use matlab and show me the codes
Question
Integration over discontinuities. Integrate the function f(x) = 0 for x < 1, f(x) = exp(x − 1) + exp(2 − x) for 1 ≤ x ≤ 2, f(x) = 0 for x > 2 from x = 0 to x = 3, for varying step sizes, using the trapezoidal method. Plot the error as a function of step size. A Matlab function calculating this function, f_r13a.m, is available on Blackboard. Note that the integral is equal to 2e − 1. use matlab and show me the codes
Solution
Here is a simple MATLAB code that calculates the integral using the trapezoidal method and plots the error as a function of step size.
% Define the function
f = @(x) (x >= 1 & x <= 2) .* (exp(x - 1) + exp(2 - x));
% Exact integral value
exact_integral = 2*exp(1) - 1;
% Initialize arrays to store step sizes and errors
step_sizes = logspace(-2, 0, 50);
errors = zeros(size(step_sizes));
% Loop over different step sizes
for i = 1:length(step_sizes)
% Current step size
h = step_sizes(i);
% Define the x values
x = 0:h:3;
% Calculate the integral using the trapezoidal method
integral = h * (0.5*f(x(1)) + sum(f(x(2:end-1))) + 0.5*f(x(end)));
% Calculate the error
errors(i) = abs(integral - exact_integral);
end
% Plot the error as a function of step size
loglog(step_sizes, errors, 'b.-');
xlabel('Step size');
ylabel('Error');
title('Error as a function of step size using the trapezoidal method');
grid on;
This code first defines the function f using an anonymous function. It then calculates the integral for different step sizes using the trapezoidal method and stores the errors in an array. Finally, it plots the error as a function of step size on a log-log plot.
Please note that you need to adjust the step sizes and the range of integration according to your specific problem. Also, the function f is defined using logical indexing, which might not be familiar to beginners.
Similar Questions
Integration over an infinite interval. Integrate exp(−x) from x = 0 to x = ∞, by numerically integrating from x = 0 to increasing values of x. Plot a graph of the error vs the value of x you integrate to. Compare results for a fixed step size and a fixed number of steps. use matlab to finish show me the codes
Apply the Midpoint and Traperold Rules to the following integral Make a table showing the approximations and errors for 48, 16, and 32. The exact value of the ntegral g for computing the enointegrate Ex * e ^ (- 2x) dx from 0 to 1/2 = 3(1 - e ^ - 5) = 2.9f(t) * 6Complete the following tableCrpe brogers or decimals Round to four decimals places as needed)T(-)Absolute Error in Mn)Absolute Error in T(x)
Estimate the area under the graph of f(x) = 2/x from x = 1 to x = 2 using four approximating rectangles and right endpoints. (Round your answer to four decimal places.)
Select the fourth function, y = 1x2 + 1, and set the interval to [−3, 2].(a) Find the approximate net area for 5 subintervals using left-endpoint rectangles. Find the approximate net area for 5 subintervals using right-endpoint rectangles.
Use Simpson's Rule with 10 subintervals to estimate the area under the graph of y = 0.2x2 for −4 ≤ x ≤ 2.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.