So previously we have seen that we can actually fit all data using blinds.
And actually, we used polynomials, blinds and these polynomials were of the cubic order.
So the highest term was access to the power of three.
And you see, when we have these individual points here, they are all connected and they are also steady
in their derivative.
So it's a very smooth function and every point is fitted perfectly.
And also, I told you that this is sometimes not what we want physically, but still it is a nice result,
I think.
And here I'm going to show you another method how we can accomplish this, and we will not distinguish
these individual segments here, and we will not try to fit third all of polynomials for these individual
segments, but instead we will fit a single polynomial to all the points.
So you can imagine if you have, for example, 10 points or in our case, I think it was 20 or something
like that, then we need a very high order polynomial to really fit all the points because we need many
minima maxima and it's pretty difficult to fit this.
However, from a mathematical point of view, it's actually not so difficult.
And also, let me tell you that I created this lecture later on after I have created, after I've recorded
even the whole section because I thought it is a nice additional information and especially it introduces
to you another method that's very important for computational physics.
And this is solving a system of linear equations.
So let's get started.
The first thing that we will do is we will reduce our number of points.
So this is just for simplicity.
So because I'm writing down here some equations, and I don't want to make these equations very difficult,
so I will just reduce the number of data points and I decided to use here all the components.
So this will be X and Y, and I will only show you these seven data points and we can even look at them,
of course.
So they will be in the range from X minus one point five to one point five, approximately.
So these will just be these enough points here.
And the reason is now we have seven data points, and this means if we want to fit a polynomial, we
need at least a 6th order polynomial for a perfect interpolation.
So this is because our our sixth order polynomial has seven coefficients.
So we have a zero, which is a constant term.
And then these six other coefficients in front of these x x square and so on.
So this means we have seven degrees of freedom, which we can tune and fit to our data points, and
we have seven data points.
So you see, it's really a differential equation with seven parameters that we have to determine and
seven equations.
So it really makes sense to do this.
And we also note that our polynomial here is supposed to give us the correct value for all the data
points which have to coordinates X one or X, Y and Z.
And why are you and I goes, of course, from zero to six so that it covers all of these data points
here.
And this means we have seven of these equations here.
And um, these are the equations here.
So you can start writing why zero is equal to A0 plus A1 times x zero plus eight, two times x zero
squared and so on.
And this is what they did here in terms of matrices.
So you see here for the first line, we have why zero is equal to one times a zero plus x zero times
A1 plus x zero squared times a two.
So these aren't really all these terms that I have just mentioned to you and we have seven of these
equations.
And so this is a matrix equation, and this means we can now basically just define these individual
things here as a vector, as a matrix and as another vector.
So right now, it's not really clear why I do this, but we will use this in a second for an umpire
routine, which helps us solving this system of linear equations very quickly.
So for this routine, we need the input as a matrix, and these matrices are exactly these that I have
written down here.
So we begin by programming this matrix here, which I call X, because all the terms here are X.
And actually here you could also write x zero to the power of zero x one to the power of zero, because
something to the power of zero is always one.
So we can write that x of a whole matrix here.
Is essentially an array, so MP Todd Parade.
And this array basically consists out of our data points, so Datafolha, but only the X components,
of course, so we write zero and then all the data points like this and then the first term, as I just
mentioned, you could write to the point of zero.
You could actually write also an array of ones, but I will do it like this.
And then the second entry will be the same thing to pull off one.
And then we continue until we are the power of six.
So of course, you could do this more elegantly, but this is just a quick practice.
So I think it's OK if it just do it by hand and doesn't take too long.
But of course, if you would take here 200 points and you would need a polynomial of order 200 approximately
actually one hundred ninety nine, then you would have to write down your many terms.
And then it, of course, would be better to just automate this and not write it down by hand.
But anyway, here's the key.
So this is not our array, and actually we have to transpose it as well.
It's all right and p dot transpose.
And then close it in the end, and maybe we can make this look a bit nicer.
Like this, for example.
OK.
And I will do it like this.
OK.
So I hope you OK.
You see what I did here and I can run this now and we can have a look.
So you see, it's now an array and you have here these individual terms and you see the first column.
So first entry of each of these rows here is always one.
So this is exactly what we have here.
And then for the second entry, we have just the X values of our data, which are these values here
and then so on and so on.
Squared cubed two are four or five six.
So now we have two Matrix X and we want to determine the Vector A with the individual parameters here
a zero to a six.
And of course, we also need to program here the vector of Y, which is basically just this one here.
So that's very easy to program.
Y is equal to data pulley, then the Y component and all the data points.
OK, so then I wrote.
Now we can solve the system of linear equations and linear means linear in a.
Of course, it's not linear in X because you see we have here squared cubed and so on, but it's linear
and eight.
So you see this matrix here, even though you have here squared part two three four five six.
These are all just numbers, as we have just explored when we had had a look at this because these are
really points coordinates and when you calculate the force power of them, they are still just numbers.
So this is here a matrix of numbers, and this is here actually the parameter for which we try to solve
these equations.
So the equations are really linear in the zero eight, one, eight two and so on.
And of course, I think you know this already from high school, you learned, is actually very early
in mathematics how to solve such linear equations or a system of these linear equations.
And a typical way to do this is to use Gaussian elimination methods.
And if you want, you can really program this whole method yourself.
Just remember how you did it in school and then just program this using loops, for example.
But of course, there are also more elaborate methods to solve this.
And if you write it down in terms of a matrix as here, then typically in computational physics, one
uses these so-called l.u decomposition, which means lower upper factorization.
So basically, we try to express this matrix as a product of a matrix that only has a lower triangle
and an upper triangle.
And this leads us then directly to the solution.
So I don't want to go into the details here on how this works, because in this course, we don't really
need to solve systems of linear equations.
But for some problems, a bit more simple problems, it can be useful.
So if you're interested, please just look on Wikipedia, for example, to decomposition, and you could
also try to program this Gorshin elimination method yourself.
However, as I said, we will use here and numpy routine, and it's actually very easy.
It's just and offline arc dot solved.
And then we provide the matrix this one and then the left hand side vector.
So just like this and we want to start a solution which is a vector assay, and now we have already
our solution.
It didn't even take a tenth of a second.
So we can now look at the solutions and these are the solution.
So a zero is 13, a one, two, four and so on.
So we don't really know if this makes sense and if this is correct, but we can, of course, have a
look by plotting.
So let me copy here a few lines of code that I have prepared previously that you can, of course, also
write down.
So you see here, first thing is an X label and then here a specified a limit of the year of the Y axis
goes from zero to 30, as we have learned before.
And then I have a plot similar to our previous plots like this one, for example, very a specified
X list.
And then I plot the function here and then I plots our data points.
But of course, I have adapted this to our current problem, so I have changed these data points to
data.
Polly and I have calculated the why list.
So the Y components of this function, which are actually these values here.
So basically, when you take these functions here or the single function and you take all different
values for X from our X list, then this will be the function.
So a zero times one basically so x list power of zeros one then plus a one times x plus a two times
x squared plus a three times X to the power of three and so on.
Until we are at the last term and we can run this and see maybe we can even go to minus 10 here or minus
20 even.
OK, so now you see, we have now are a seven data points, which are exactly the data points from here
close to zero.
So these are really these data points here.
And you see, we have fitted a sixth order polynomial because we have seven coefficients.
And you see it really fits all the data points perfectly.
However, of course, you can also see that when we get rid of this town far away from these data points,
the polynomial will give totally incorrect results in terms of our data points.
So you see here we have data points in the range of minus 32 30, maybe minus 40 to 40, but here it
goes up to several thousand.
So it's such a method is nice, mathematically speaking, because we are really fitting these data points
perfectly with a single function and not just piece wise, as we did with the spleens.
However, it's not really useful to use this for extrapolation.
So everything that's outside of the range of these data points will give a totally inaccurate result,
and it gets even worse.
We could, for example, fit these data points with the even higher polynomial where we just arbitrarily
choose some of the parameters, and then we could basically get any behavior for these ranges outside
of the data range.
OK.
So but this was just an add on, as I told you, just a nice mathematical method.
And also, I want to tell you how you can use the Nampai module in October solve to solve systems of
linear equations just in case you need it sometimes.
And of course, we will not actually need it.
But yeah, it's still a good tool to have.
And also, it's it's a very nice result that we are not able to fit our data perfectly with just a single
function, which is a higher order polynomial.