Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(MATLAB) The following questions need to be programmed using MATLAB. I need help

ID: 3801438 • Letter: #

Question

(MATLAB) The following questions need to be programmed using MATLAB.

I need help in questions 1 and 2(a, b). I am aware that there is a solution for these questions founded in the textbook solutions on Chegg, but please do not use their codes! I would like to see different methods used to answer these questions. I will report for plagiarism if it's the same! Thank you.

This is one of the solution code:

function T = Trapezoid_Uniform(f, a, b, n)

h = (b-a)/n;
x = [a+h: h: b-h];
T = (h/2)*(2*sum(feval(f,x)) + feval(f,a) + feval(f,b));

function y = f(t)
y = exp(t);

Obviously, the last line can be changed according to the question function. This code can be ran in the command window with typing Trapezoid_Uniform('f', 0, 1, 20).

HOWEVER, please do NOT use these code as answers. Is there any way this code can be rewritten in a completely different format but still get the same outputs?

1. Write real function Trapezoid Uniform f, a, b, n) the composite trapezoid rule with n equal subintervals. 2. (Continuation) Test the code written in the preceding computer problem on the following functions. In each case, compare with the correct answer. sinx dx ab. l et dx c. arctan x dx J 0 J 0

Explanation / Answer

Part 1 matlab function Trapezoid_Uniform.m

function Result = Trapezoid_Uniform(f, a, b, n)
    x = linspace(a,b,n); % Creating a x vector of intervels
    h = x(2)-x(1); % The length of the intervel
    S = 0; % set a variable S to zero
    for k = 2:n-1 % Eliminating the first and first and last points in x
        % evaluating sum of twice the functional values of internal nodes
        S = S + 2*f(x(k));
    end
    Result = h*(S+f(x(1))+f(x(n)))/2; % adding extearnal nodes and computing Result
end

Part 2 a Testing the function

>> f = @(x) sin(x);
>> a = 0;
>> b = pi;
>> n = 200;
>> Result = Trapezoid_Uniform(f, a, b, n)

Result =

    2.0000

>> quad(f,a,b) % Matlab inbuilt function to compare the result we obtained

ans =

    2.0000

>>

Part 2 b Testing the function

>> f = @(x) exp(x);
>> a = 0;
>> b = 1;
>> n = 200;
>> Result = Trapezoid_Uniform(f, a, b, n)

Result =

    1.7183

>> quad(f,a,b) % Matlab inbuilt function to compare the result we obtained

ans =

    1.7183

>>