Question: Problem 1 ( 2 0 points, Core Course Outcome # 6 ) / Mat labGrader Develop a Matlab function mysimpson 3 8 that calculates

Problem 1(20 points, Core Course Outcome \#6)/ Mat labGrader
Develop a Matlab function mysimpson38 that calculates \( I=\int y(x) d x \) using the composite Simpson's \(3/8\) method for a set of discrete data points \(\left(x_{i}, y_{i}\right)\) that are equally spaced. If the number of subintervals is not divisible by 3 because there is 1 extra subinterval, calculate the integral of the last 4 subintervals using Simpson's \(1/3\) method. If there are 2 extra subintervals, calculate the integral for the last 2 subintervals using Simpson's \(1/3\) method.
If the number of subintervals is zero (\(=1\) data point only), the function shall return a value of zero for the integral. If the number of subintervals is one (\(=2\) data points only), the function shall use the trapezoidal method on the single interval. If the vectors \( x \) and \( y \) are not of equal length, the function shall execute Matlab's function error("...") with an appropriate error message.
As input the function shall take the column vectors x and y that contain the data points. As output the function shall give the calculated integral I. You may calluse any function developed in recitation, homework, and/or exams.
Required submission:
\(\square \) well commented function source code submitted to Matlab Grader using the Canvas link for Exam 6- Problem 1
Here is the function for the trapezoidal method:
function [I]= myTrapezoidal(x,y)
% calculates the integral y(x)dx for the given data points using the
% composite trapezoidal method
% Inputs: x,y = data points
% Outputs: I = integral
% checks for equal number of entries in x and y
if length(x) ~= length(y)
error("Vectors x and y must have the same number of entries.");
end
% if only one data point is in x and y, the integral is zero
if length(x)==1
I =0;
return
end
% initializes runnign sum of 1
I =0;
% number of intervals N is the number of points minus 1
N = length(x)-1;
% evaluates the composite trapezoidal method with a loop
for i =1:N
I = I+1/2*(y(i)+y(i+1))*(x(i+1)-x(i));
end
end
Here is the function for Simpson's 1/3 Method:
function [I]= mySimpson13(x, y)
% evaluate integral y(x)dx using Simpson's 1/3 composite method for the
% data points given in x and y
% Inputs: x,y = data points
% Outputs: I = integral evaluated using Simpson's composite 1/3 method
% check that input vectors x and y have the same length
if length(x) ~= length(y)
error("Vectors x and y must have the same length");
end
% if only one data point in x and y, the integral is zero
if length(x)==1
I =0;
return;
end
% if exactly two data points, calculate using the trapezoidal method
if length(x)==2
I =1/2*(y(1)+ y(2))*(x(2)- x(1));
return; % Return immediately after calculating the trapezoidal area
end
% Number of intervals N is the number of points minus 1
N = length(x)-1;
% Initialize integral
I =0;
% For an odd number of intervals, use trapezoidal method for the last interval
if mod(N,2)==1
I =1/2*(y(N)+ y(N +1))*(x(N +1)- x(N)); % trapezoidal for the last interval
N = N -1; % reduce N for Simpson's method
end
% Use Simpson's 1/3 method for the remaining intervals
h = x(2)- x(1);
I = I + h/3*(y(1)+4* sum(y(2:2:N+1))+2* sum(y(3:2:N))+ y(N +1));
end
Problem 1 ( 2 0 points, Core Course Outcome \ # 6

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!