This question is simple, but not as simple as it looks at first sight.
the function x you have defined
has a reference vector
your attempt to integrate beyond 5 does not work unless the reference vector of the function covers such interval.
To get the integration results q1=2 q2=6 q3=10 q4=14 the function you should integrate could be
and reference vector
the problem here is that MATLAB interpolates the following way
despite showing an apparently staired stem plot
and the integration results along the intervals are biased with constant 2:
to solve the excessive interpolation that MATLAB performs by default, here linear interpolation is excessive, you may want to do the following:
1.- increase resolution
2.- be aware that if you define the following function
it seems to work but it does not. If you try
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
despite one by one
func1 seems to work fine with scalars but not with vectors. You need custom defined functions to work correctly with vectors to pass the handle to the integrating function quad or integral.
if you try quad(@func1,a,b) with the previous function func1 on the sought intervals the results are wrong.
4.- So, to generate the values of the function to integrate correctly you have to introduce the following loop in the function that calculates the stair:
now if you plot y you get the right function:
just did it, use the following function
now you get the right results:
If you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance