So welcome back.
This is the solution of the exercise, we want to calculate the velocity and the acceleration of some
object.
So you see, I have just loaded the same file.
You also have the template and you see here's again the plot and the task.
And I just started to type along with my solution.
And here you see the first solution where I program the forward differences methods.
And first of all, I think it's very important to understand how we can access the individual columns.
So this means the time points T and D coordinates X, and we can do this by writing data, all the points
and then zero full time and one for coordinate.
So you see here, for example, the time goes from zero to 100 in steps of zero point one.
And then I already gave you the hint that we cannot specify here any step with age because this is already
determined by the data.
So the step with will always be the step size from one point to another.
So in this point, since it in this case, since it's equidistant, it will be always zero point one.
And this means if we want to program this whole thing by a function, we only have a single inputs,
which will be our data file.
We do not need each anymore.
OK.
And just another hint I programmed this using a function which I called forward for for differences
methods.
But you don't have to do this.
You could also just do it with a loop, with a while loop, for example, and you don't need to program
this with a function.
So as long as in the end, you get the correct result.
Everything is good.
So this is just my proposal of a solution.
OK, so the first task was to calculate VE and DNA using for what differences methods?
Here's how I did it.
So you see, I returned here again, an array which consists out of data all points zero, which are
two points of time.
And then here I do not return to coordinates, but instead the derivative, and I start by defining
this variable as an array, which consists only of zeros.
So this is basically like the initialization of the array with a zero values, and it will have the
length, which is the same as this one.
And also of this one.
So this is what's happened here and now we can use different ways on how to calculate this.
So since we have accurate lists and data, we can also do it like this.
We could say first with the final step with age, which is always the same.
So for example, we can say calculate the difference of this element and this element.
So it's zero point one, and then the velocity will basically be take all the points, except the last
one.
This is how you can write it.
So it means start from the very first entry and then go until minus one, which is actually the last
one.
But the way the Python syntax works is that this point here is actually excluded.
So it means all the points except for the last one.
And you calculate this by taking the data file or the data entry at the same points and then you subtract.
So here I see actually, this is incorrect, which I didn't notice because I commented, So actually,
it has to be the other way around.
So it means we have to calculate here the difference of the whole dataset for the next point.
So this means basically take these indices and shift them by one.
So this means take all the indices except for the first one, because here zero is not included.
And then subtract the previous data point always and divide it by the step size.
So this works for equidistant data.
But if you want to have a general where the data is not equidistant, so this means here you have and
or a list of points that, for example, would be zero or 0.2 0.3, then you couldn't do it like this.
You explicitly put here everywhere.
The step size, which can be variable.
OK.
And you see here, it's the same thing as in this column.
It's just a different index.
So here is to coordinate index.
Here's the time index, OK?
And then the only problem is that we cannot easily do this for the very last data point, because then
you cannot do format differences, methods.
So you could say now, OK, I just leave it at zero.
I don't care about it.
You could say I just set it to the second last value because it's probably very similar.
Or you could say, OK, for this last point, I used it backwards differences, methods.
So this is what I did here.
But don't worry, I did not specify that you should handle the last data point in a specific way.
So if you didn't do it, it's no big deal.
It's just a single data point.
It's different then.
OK, so now we have a function forward of data, and now we can use it to our dataset to calculate the
velocity forwards.
So this will now be a dataset which has the same shape as this one.
But here we will not have to coordinate the velocity.
And then we can actually apply this function twice.
And so this means we get the second derivative.
And so we have now two two data sets velocity forward acceleration.
And then we can do the same thing using the central difference as methods and the Richardson methods,
which I think I don't have to explain here.
Very much so you see here, for example, for the central difference is methods.
We take all the points except for the first and the last.
And then we shift here to indices upwards and downwards and tier the same upwards downwards.
And here we take the coordinate in next time index.
And so this calculates the derivative and then for the first and for the last value, we cannot use
central differences.
So I decided to use for differences for the first value and backwards differences for the last value.
But to be honest, you could just say these are zero or you could take the previous value.
It would all just be fine.
OK, and now you can.
We can use this new function and again apply it to the dataset and get a new dataset to Velocity Central
Acceleration Central and for the Richardson method.
It's, of course, also very similar.
Here we have to shift by to an.
Sees so this means we take all the values, except for the first two and the last two, so this means
we shift into the positive direction by two, we shift by one, we shift by minus one and we shift by
minus two, and then we divide by 12 times the step size.
And you see that this is pretty much the same way we have programmed this previously.
So you see there was this equation where we had to shift by to age by one age minus one age minus two
age and the factors minus one eight minus eight one.
So if you don't remember, just have a look at our previous notebook.
It's really the same thing here.
OK, and once again, we can now calculate our new data sets velocity Richardson, acceleration Richardson.
And finally, we can now plot all of these things.
So you see here I have two plots.
The first one is velocity versus time, and I plot here three curves on top of each other.
And I plot, of course, velocity, forward velocity, central velocity, Richards and then the same
thing with the acceleration.
And this is what we get.
You see, first of all that all the points I hear really on top of each other.
So especially for the velocity you see we have here a yellow curve, a green curve and a blue curve.
And they are all on top of each other, which means on this zoom, on this magnification, we do not
really see a difference of the methods.
And also we see that in the beginning, the velocity is positive, but it decreases to zero.
Almost four, I would say, even below zero.
And then it oscillates, and in the end it will be positive and it has a pretty large value.
And this makes sense if you look at the data file.
First of all, the velocity is positive, but then decreases.
So this means here the car is maybe even going a tiny bit backwards.
And then in the end, it will increase because the velocity is positive.
And here we have some backwards and forwards motion because the velocity is positive and also the negative
at points.
So it makes sense for the acceleration.
We take the derivative of this curve.
So you see in the beginning, the velocity decreases, which means the acceleration is negative.
But then at a later point of time here, the acceleration has to turn positive.
So there is a zero here.
And you see forward acceleration.
We have the same phenomenon that all the three curves are on top of each other, except for the last
two or three points.
All right.
Yeah, that's two points, actually.
So you see it in here.
We have a blue point, a green point in the yellow point.
And this is because the acceleration calculates the second order time derivative.
So the problem is here that we did not calculate the first and the last value in the proper way.
We had to cheat here a bit because our method was not working because we didn't know the next value
that would come after the last value.
So the problem is, now that this value and this value are actually not really accurate.
And if we continue to calculate the derivative, then it gets even worse from the last value and also
for the second last value.
This is where these differences come from.
So I decided to rebuild everything and to leave out the two first values and the two last values for
the velocity and the full first and for last value, for the exploration.
And then you see everything fits perfectly well, maybe not perfectly.
So now is the end of your first task if you have accomplished this really, really nice.
And if not, please have a look again at my solution.
I hope this helps you to find your ranch or your mistake.
And now I want to show you a bit of an additional analysis of the error.
So here I can tell you now how I have generated the data set to offload it.
This was actually here's the function that they used.
So a cosine function power two to five, power two to four and power to one, and some coefficients
which you see here.
And I have programmed this, actually, I have changed this one here later on.
So the changes?
OK.
That's correct.
And this is the function that I have programs and you see, it gives us the same dataset that we have
loaded previously starts from approximately one goes to almost 16.
This was also what we had here.
And in fact, I have here in the data set, and this is actually how I export it, the data set that
you have downloaded and if you look at the function this one, we can actually calculate the derivative
and the second derivative analytically.
So here we will get a minus sign and a derivative will be a he would get five times b t to the power
of four four times C over three and D.
And this is what I've written down here.
And then what we derive again, we get minus cosine.
Another in a derivative.
So we have a squared.
Here we have five times for beta two to power three four times, three times c t to the power of two
and then zero for this term.
So again, I have programmed these two functions and we have plotted them.
So this is here the analytical solution.
So if you remember, it looks a bit similar to what we had previously.
So in fact, it should look exactly the same, and I hope it does.
But we can figure this out by just taking this data set.
So T list a list and T list and B list and we subtract our numerically calculated values.
For example, make this a write t list for the x axis and for the y axis I plot velocity forward minus
the analytical v list.
So this gives us note the arrow.
So we have you the arrow for velocity and for the acceleration.
And you see this is what the plot gives us.
So the first curve, the blue one, which is the forward difference, is methods will have the largest
arrow.
It will be 0.0 two and four the acceleration and will be zero point zero zero one.
And then the other two curves almost have no arrow.
So maybe we maybe let's look at the curves again.
What means an arrow of zero point zero zero three?
So you see on velocity and error of zero point zero zero three is really tiny.
So this is why we cannot see it.
Still, we figure out that the central difference is methods, and the Richardson methods are far more
accurate.
And if you look carefully, you see that the Richardson method is even better.
So to further quantify this, I calculate here to total error, and I do this by calculating the the
the total square of the error.
So for every data point, I calculate the error I squirts and then I added up.
And this is what I wrote here, and this is what we get for the values.
So for the central, the story for what difference is methods.
We have a total error of zero point zero zero two.
Then we have the central differences methods approximately nine times 10 to the power of minus eight.
And Richardson 10 to the power of minus 16.
So you really see how this scales with the step size and how to our drastically reduces for the Richardson
methods and also for these central differences methods.
It's way smaller than for the forward differences methods.
So this is what is meant by saying that there is an error of the order of each of each square and even
higher.
And in our previous example, in previous lectures where we knew the function, this wasn't really a
big deal because we could always just decrease the step size.
So even for the full what difference this methods, we could achieve a very accurate result by just
decreasing the step, size, age.
But here, since we work with a fixed data set, we cannot do this.
So this is where really these Richardson method and also the central difference, this method where
they shine compared to the other methods, because working with the same data file, you get a much
higher precision.
And then for the acceleration, we get a similar observation, wasteful error here.
OK, and then you had the second task.
Let me scroll up to the tasks again, determine the maximum value of the acceleration and the corresponding
point of time.
So we basically look at this graph here acceleration, and we see the maximum value is here almost 0.06
meters per square second.
And that point of time is seventy eight seventy nine, maybe.
So let's see if we can figure this out using our methods, and we can do this by loading one of our
of our data sets actually here A-roads velocity, but its acceleration.
And this is our dataset, and now we can use several methods, of course, to do this.
And one of the most easy ones is to, first of all, look at the maximum value of this column here.
So we write maximum of exploration Richards and all the values and then the exploration column here
one.
And this gives us a value which I have plotted here.
So this is zero point zero five eight eight nine.
This corresponds to the value on the acceleration axis.
So, for example, here this one.
Yeah, so it really is, you see almost zero point zero six.
So it's correct.
And then we can write, find out the index.
So basically, the number of the elements, the index where it is values reached.
So we say please find and p dot max of this dataset acceleration Richardson, all the values and one.
So now we have the index, which is seven hundred ninety six.
So since we know our step sizes zero point one, we already know, okay, the time has done ninety seven
point six seconds.
But if we wouldn't know the step size and if it would be more homogenous than we could now, right?
Just take the acceleration of Richardson.
So this is our data sets here.
And then for the first index, we used the index, of course, which we have just figured out.
So take the the seven hundred ninety 796 and then take the first values.
So take the time.
And so the time value is ten seventy nine point six seconds.
All right.
So we have now solved both of these tasks.
We have calculated the velocity and the acceleration using different methods, and we have figured out
the maximum of the acceleration.
And furthermore, I have also shown you a brief analysis of the error here, and I hope you believe
me now that the Richardson method and also other methods are sometimes way better than the.
What differences methods.
And it is worth it to really use these advanced weapons methods from time to time, especially if you
have to work with the fixed dataset where you cannot use the step size.
So there it's really good and really essential to decrease the error and to improve the result.
So I hope you took the time and you also tried to do this yourself, and I hope you had some success.
If not, then I hope that now, since you have seen this solution, these differences or these problems
that you had are now resolved and you can continue with the next lecture of the course.