Summary:
Write a program that will determine the optimal (highest accuracy) way to apply the different numerical integration rules: Trapezoidal, Simpson's 1/3, and Simpson's 3/8, Boole's:
1) define two arrays 't' and 'Q' using the data from the homework
Note that for simplicity I will NOT bother with converting the units to Liters/second, but you could do that here if you want to match your homework.
2) use the 'diff' command to measure the 't' intervals in the data:https://www.mathworks.com/help/matlab/ref/diff.html
The result should be:
500 500 500 700 700 700 700 900 1300 500 500
3) use the 'accepted answer' here:https://www.mathworks.com/matlabcentral/answers/394454-how-to-count-the-number-of-consecutive-repetitions-of-an-array
to measure how many times each interval size repeats consecutively. Note that you will need to create a free MathWorks account herehttps://www.mathworks.com/mwaccount/register
In order to download the 'runlength.m' subroutine from the Matlab FileExchange:https://www.mathworks.com/matlabcentral/fileexchange/241-runlength-m
Place this subroutine into the 'current working folder' in which your main script is located in:https://www.mathworks.com/help/matlab/ref/filebrowser_currentfolderbrowser.pngin order for matlab to be able to use the 'runlength' command.
Apply the'runlength' command to the result from the last step, in order to obtain the 't' interval repetitions. The result should be:
3 4 1 1 2
4) Determine how many unique interval size repetitions you have in your data. You can do this by applying the 'length' to the result from the previous step.
https://www.mathworks.com/help/matlab/ref/length.html
The answer should be 5
5) Create a'for' loopthat cycles over the number interval interval sizes. Note that the loop index should increase from 1 to the result from the previous step. For a different problem, it could be different than 5, so you should NOT just say for i=1:5. Instead of the number 5 you should use avariable name, so that your code would work for a different problem.
6) For each iteration of the for loop, extract the 't' and 'Q'subset that belongs to it. For example, for the first iteration you will consider the four points which are consecutively equispaced with an interval of delta_t = 500. That is the answer should be:
t_THIS_subset =0 500 1000 1500
Q_THIS_subset =10.5500 9.5760 9.0720 8.6400
Note that the data subset extraction should be done programmatically, NOT manually. Here is how you can extract the data for thefirstiteration:
definesubset_start = 1andsubset_end =subset_start + # of segments for the iteration
since there arethreeequal segments in this subset of data, the variablesubset_end = 4for thefirst iteration.
The values ofsubset_startandsubset_endwill bedifferentfor every iteration!
7) For each iteration in thefor loop, determinethe optimal (highest accuracy) integration rule (i.e.,Trapezoidal, Simpson's 1/3, or Simpson's 3/8, Boole's) that should be applied to this particular subset of your data. You should use an if/elseif/else/end structure to do this, which checks either the number of intervals or the number of data points belonging to the particular subset of your data. For example, if the subset has just one interval --> Trapezoidal, elseif two intervals --> simpsons 1/3, and so on.
For example, in the first iteration show in the previous step, you have 4 data points and 3 intervals -- > Simpson's 3/8 is best for this subset
8) Once the best integration method is identified, apply the corresponding formula from Tbl 19.2 on pg 507 for each case of yourif/elseif/else/end structure and store the result in a variable called 'I_this_subrange'.
Note that the value of this variable will be updated each new iteration. For thefirstiteration you should get:I_this_subrange =14087.625 using theSimpson's 3/8 rule.
9) Keep a running sum of the integral values calculated for each subset. It should look something like this:
I_entire_range = 0;
for i = 1:n
SOME CODE...
I_entire_range = I_entire_range + I_this_subrange;
end
10) Display 'I_entire_range' from the previous step to the command window and compare this result to Matlab's 'trapz' command applied to the entire dataset:
https://www.mathworks.com/help/matlab/ref/trapz.html
Note that you may NOT bypass programming of any step by simply copying any numerical result that I gave you above into your code, as I will consider this to becheating!