Question: Problem 1 ( 2 0 points, Core Course Outcome # 6 ) / Mat labGrader Develop a Matlab function mysimpson 3 8 that calculates
Problem points, Core Course Outcome # Mat labGrader
Develop a Matlab function mysimpson that calculates Iint yx d x using the composite Simpson's method for a set of discrete data points leftxi yiright that are equally spaced. If the number of subintervals is not divisible by because there is extra subinterval, calculate the integral of the last subintervals using Simpson's method. If there are extra subintervals, calculate the integral for the last subintervals using Simpson's method.
If the number of subintervals is zero data point only the function shall return a value of zero for the integral. If the number of subintervals is one 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, andor exams.
Required submission:
square well commented function source code submitted to Matlab Grader using the Canvas link for Exam Problem
Here is the function for the trapezoidal method:
function I myTrapezoidalxy
calculates the integral yxdx for the given data points using the
composite trapezoidal method
Inputs: xy data points
Outputs: I integral
checks for equal number of entries in x and y
if lengthx ~ lengthy
errorVectors 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 lengthx
I ;
return
end
initializes runnign sum of
I ;
number of intervals N is the number of points minus
N lengthx;
evaluates the composite trapezoidal method with a loop
for i :N
I Iyiyixixi;
end
end
Here is the function for Simpson's Method:
function I mySimpsonx y
evaluate integral yxdx using Simpson's composite method for the
data points given in x and y
Inputs: xy data points
Outputs: I integral evaluated using Simpson's composite method
check that input vectors x and y have the same length
if lengthx ~ lengthy
errorVectors x and y must have the same length";
end
if only one data point in x and y the integral is zero
if lengthx
I ;
return;
end
if exactly two data points, calculate using the trapezoidal method
if lengthx
I y yx x;
return; Return immediately after calculating the trapezoidal area
end
Number of intervals N is the number of points minus
N lengthx;
Initialize integral
I ;
For an odd number of intervals, use trapezoidal method for the last interval
if modN
I yN yN xN xN; trapezoidal for the last interval
N N ; reduce N for Simpson's method
end
Use Simpson's method for the remaining intervals
h x x;
I I hy sumy::N sumy::N yN ;
end
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
