So the previous lecture, we have defined our model function, which is supposed to be a polynomial.
And then we have tested that if we use the parameters that we used for the beginning, that indeed we
get the correct function.
So this is, of course, not the solution that we are going to use because, yeah, we are trying to
figure out these parameters and we don't just want to use the correct solution right away.
This was just for testing if our model function works and we see it works quite well.
So say, we don't know these values yet and say we use here instead some other values, for example,
a starting point could be just ones.
So like these values, then you see the function looks totally different and there is a deviation from
the actual data points.
So how would we now say how good is a fit for this?
We need an error function.
So let me just for the notebook.
Restore this and let's come to the error function.
We will now define the error, which basically also defines the quality of our fit.
As I wrote down, there are actually many reasonable definitions of an error function, but a very common
choice, especially in the beginning, is such a square into differences methods.
So basically, you sum up of all your data points and you just take the difference in the Y values and
you square all of the values and then you add them up.
And this gives you, of course, always a positive number.
And so since you square it up when there is a single point with a large difference, this will give
you a very large error.
So it's such an error of function tries to basically minimize the distance between fit and data for
all points at the same time, and mainly it tries to avoid some very large distance and one of these
terms.
So the function F is the fit function that is determined by the coefficients.
And we have several coefficients a i so in our case, we have a zero, a one, a two and a three because
we have a third order polynomial.
So for parameters.
But the way that we have programmed everything is very generous.
We could also use a ninth order polynomial.
It would work.
And the points XIII and y i.
These are the data points that we tried to fit.
And I think there we had 21 data points between minus five and plus five.
So now let's go ahead and just take this definition of the error and define it.
So I will call this error fit.
The name isn't really that important.
You could give it another name and the arguments will be if the function that we have, the fish shoes
and the data points, of course.
So if it will be the fit function or the model, then the coefficients will be the values that we try
to optimize.
So the MRI that we try to optimize and then we have the data, which will be our data.
We try to fit.
And once again, we just add things up here.
So you see, whenever we have these sons, we can just use a loop.
And as I said, there are other methods, but I think the loop is just fine, even though it's a bit
clumsy, maybe, but doesn't matter here because all of these calculations are very quick anyway.
So it doesn't really matter.
If we try to optimize things here, we can just go ahead.
So here is maybe a thing where you could make a mistake if you're not careful.
So we calculate here some and we have an index AI that ranges from.
And then here I wrote, down from one to end.
But in our case, we have programmed it to start at zero and it goes to end.
So this, I would say, is totally fine.
So since we don't include and we go from zero to and minus one.
So it's the same thing.
Basically here and here, you have to be careful because if you write in range length of data, then
this value would just be too because we have X and Y, so the length would then be just too.
But if we now just look at the X coordinates, for example, then we have the actual numbers of our
points.
So make sure you don't forget this one here.
Otherwise, the court will still work, but it will give you an incorrect error because it will only
take into account the first two data points, which is, of course, not good.
But anyway, since we have it correct now, we can have data on error, so we will be old error plus
and then we just write the square here in the brackets, we have data one which will be the Y coordinates
and then the five point minus f of.
Date Zero Comma II, which the X coordinates, and then we have the coefficients, which are just two
coefficients coefficients.
And then we could say just for testing purposes, we want to print the arrow just to see if it works,
because this will, we will delete later on and then we can return the error after the loop.
So we run this and now we can write a fit of our colleague in non-lethal model.
Then we use a zero for the starting point for the parameters, and then we use the data points for the
data.
And when I run this.
What's going on?
I think for some reason, this is a markdown cell, so this is just my fault.
They have somehow missed something else.
Let's copy it in an actual cell that is a code cell, and now it works.
Yes.
So you see here the error will be added up and we start from a very small value.
And then you see it just keeps on increasing, which is clear since we are adding here square numbers.
So these can only be positive and the error will increase.
And then at some point the loop will stop and it will ultimately give us the error.
All right.
So it seems to me reasonable.
So that's common.
This one, we don't need it anymore.
And so now the error just gives us a single value.