numerical integration consider the velocity vs time data below it s a
Search for question
Question
Numerical Integration
Consider the velocity vs time data below. (it’s a picture .. you’ll have to enter it by hand..)
Plot the data and fit it with a cubic equation (use trendline and display constants)
Now you have a cubic expression .. integrate it analytically to find distance travelled
(If you’ve forgotten physics that’s integral (v dt)
Now lets try it with the trapezoidal rule .. on the spreadsheet …
Determine the average velocity from your answer to part c.(distance/time)
Notice that you cannot use Simpson’s rule on the data as it is not evenly spaced. However, you could use the cubic equation you created in part b and build a new table for values of v between 0 and 10 with a constant step size of 1. Do it! Now use Simpson’s rule .. on the spreadsheet not VBA to calculate the numerical integral.
OK NOW lets go to MATLAB. Write a function that performs a trapezoidal rule integration of a data set consisting of two columns .. x and f(x). These may be read in from excel or entered as two arrays .. say “Y” for f(x) and “X” for X.
Try it out on a simple function … say f(x) = 2x^3+3x^2-5x+20 from 0 to 10 . You will have to either precalculate the f(x) values and enter them, or use a For loop to calculate them and fill the arrays for Y and X. Compare your answer with the analytical.
NOTE : I will provide an example of how to fill an array , but if you wanted x = 1,2,3,4,5 you could just type x=1:5 .. that would do it . Then you could use a For loop ..
For i = 1:5
Y(i) = 2*x(i)^3+3*x(i)^2-5*x(i)+20
end
NOTE: As I said, this is essentially done in the notes … but you need to do this yourselves. You can use the notes as a guide, but make sure you know what you’re doing and why. I won’t penalize you if your program is too similar to mine, but if you just copy it you will be penalizing yourself come test time!
OK now lets add a feature. Using the part 2 answer, lets create a function called fofx(x) which calculates the polynomial. Remember this must come after the program code in the live script file. There are example of this in the materials I provided last week… or you can use the help screen for “function”
SO you would just use your code from part 2, and use the function to fill the Y array instead of calculating it manually for each X(i). You might try cutting and pasting this ..
Notice that when you raise an array to a power, you must put a dot after the name to do elementwise operations. Otherwise it thinks you want to square the matrix in matrix multiplication as opposed to squaring each term…of course you need to alter the equation below for the equation in part 2
x=1:5
Y=fofx(x);
function Y = fofx(x)
Y= x.^2 + 3 * x + 3
end
Remember that for trapezoidal rule you are doing a summation … like s= s + term where the term is the area under that “slice” of the trapezoid. Also remember to initialize the s with s= 0 before the For loop starts