1
00:00:00,120 --> 00:00:05,939
So previously we have seen that we can actually fit all data using blinds.

2
00:00:06,570 --> 00:00:13,170
And actually, we used polynomials, blinds and these polynomials were of the cubic order.

3
00:00:13,170 --> 00:00:15,900
So the highest term was access to the power of three.

4
00:00:16,500 --> 00:00:22,440
And you see, when we have these individual points here, they are all connected and they are also steady

5
00:00:22,440 --> 00:00:23,940
in their derivative.

6
00:00:24,060 --> 00:00:28,110
So it's a very smooth function and every point is fitted perfectly.

7
00:00:28,920 --> 00:00:35,550
And also, I told you that this is sometimes not what we want physically, but still it is a nice result,

8
00:00:35,550 --> 00:00:36,000
I think.

9
00:00:36,570 --> 00:00:43,830
And here I'm going to show you another method how we can accomplish this, and we will not distinguish

10
00:00:43,830 --> 00:00:50,040
these individual segments here, and we will not try to fit third all of polynomials for these individual

11
00:00:50,040 --> 00:00:54,840
segments, but instead we will fit a single polynomial to all the points.

12
00:00:55,770 --> 00:01:03,030
So you can imagine if you have, for example, 10 points or in our case, I think it was 20 or something

13
00:01:03,030 --> 00:01:09,540
like that, then we need a very high order polynomial to really fit all the points because we need many

14
00:01:09,540 --> 00:01:13,050
minima maxima and it's pretty difficult to fit this.

15
00:01:13,800 --> 00:01:18,060
However, from a mathematical point of view, it's actually not so difficult.

16
00:01:19,020 --> 00:01:25,470
And also, let me tell you that I created this lecture later on after I have created, after I've recorded

17
00:01:25,470 --> 00:01:31,800
even the whole section because I thought it is a nice additional information and especially it introduces

18
00:01:31,800 --> 00:01:35,220
to you another method that's very important for computational physics.

19
00:01:35,610 --> 00:01:39,300
And this is solving a system of linear equations.

20
00:01:40,290 --> 00:01:41,280
So let's get started.

21
00:01:42,030 --> 00:01:46,710
The first thing that we will do is we will reduce our number of points.

22
00:01:47,250 --> 00:01:48,700
So this is just for simplicity.

23
00:01:48,720 --> 00:01:54,780
So because I'm writing down here some equations, and I don't want to make these equations very difficult,

24
00:01:54,780 --> 00:02:01,530
so I will just reduce the number of data points and I decided to use here all the components.

25
00:02:01,530 --> 00:02:10,680
So this will be X and Y, and I will only show you these seven data points and we can even look at them,

26
00:02:10,680 --> 00:02:11,100
of course.

27
00:02:11,730 --> 00:02:18,150
So they will be in the range from X minus one point five to one point five, approximately.

28
00:02:18,630 --> 00:02:20,970
So these will just be these enough points here.

29
00:02:21,840 --> 00:02:27,960
And the reason is now we have seven data points, and this means if we want to fit a polynomial, we

30
00:02:27,960 --> 00:02:32,130
need at least a 6th order polynomial for a perfect interpolation.

31
00:02:32,760 --> 00:02:38,670
So this is because our our sixth order polynomial has seven coefficients.

32
00:02:39,000 --> 00:02:41,220
So we have a zero, which is a constant term.

33
00:02:41,220 --> 00:02:46,170
And then these six other coefficients in front of these x x square and so on.

34
00:02:46,920 --> 00:02:52,950
So this means we have seven degrees of freedom, which we can tune and fit to our data points, and

35
00:02:52,950 --> 00:02:54,180
we have seven data points.

36
00:02:54,660 --> 00:03:00,990
So you see, it's really a differential equation with seven parameters that we have to determine and

37
00:03:00,990 --> 00:03:02,030
seven equations.

38
00:03:02,040 --> 00:03:04,510
So it really makes sense to do this.

39
00:03:05,670 --> 00:03:12,060
And we also note that our polynomial here is supposed to give us the correct value for all the data

40
00:03:12,060 --> 00:03:17,750
points which have to coordinates X one or X, Y and Z.

41
00:03:17,760 --> 00:03:23,430
And why are you and I goes, of course, from zero to six so that it covers all of these data points

42
00:03:23,430 --> 00:03:23,680
here.

43
00:03:24,210 --> 00:03:27,000
And this means we have seven of these equations here.

44
00:03:27,930 --> 00:03:30,980
And um, these are the equations here.

45
00:03:30,990 --> 00:03:39,900
So you can start writing why zero is equal to A0 plus A1 times x zero plus eight, two times x zero

46
00:03:39,900 --> 00:03:41,100
squared and so on.

47
00:03:41,520 --> 00:03:43,980
And this is what they did here in terms of matrices.

48
00:03:44,910 --> 00:03:54,330
So you see here for the first line, we have why zero is equal to one times a zero plus x zero times

49
00:03:54,330 --> 00:03:58,780
A1 plus x zero squared times a two.

50
00:03:58,800 --> 00:04:03,480
So these aren't really all these terms that I have just mentioned to you and we have seven of these

51
00:04:03,480 --> 00:04:04,080
equations.

52
00:04:05,250 --> 00:04:14,010
And so this is a matrix equation, and this means we can now basically just define these individual

53
00:04:14,430 --> 00:04:17,790
things here as a vector, as a matrix and as another vector.

54
00:04:18,510 --> 00:04:24,270
So right now, it's not really clear why I do this, but we will use this in a second for an umpire

55
00:04:24,270 --> 00:04:30,540
routine, which helps us solving this system of linear equations very quickly.

56
00:04:31,140 --> 00:04:36,150
So for this routine, we need the input as a matrix, and these matrices are exactly these that I have

57
00:04:36,150 --> 00:04:36,930
written down here.

58
00:04:37,980 --> 00:04:44,100
So we begin by programming this matrix here, which I call X, because all the terms here are X.

59
00:04:44,670 --> 00:04:51,600
And actually here you could also write x zero to the power of zero x one to the power of zero, because

60
00:04:52,410 --> 00:04:54,510
something to the power of zero is always one.

61
00:04:56,400 --> 00:04:59,490
So we can write that x of a whole matrix here.

62
00:05:00,020 --> 00:05:05,360
Is essentially an array, so MP Todd Parade.

63
00:05:07,580 --> 00:05:16,120
And this array basically consists out of our data points, so Datafolha, but only the X components,

64
00:05:16,130 --> 00:05:25,910
of course, so we write zero and then all the data points like this and then the first term, as I just

65
00:05:25,910 --> 00:05:27,860
mentioned, you could write to the point of zero.

66
00:05:28,130 --> 00:05:32,570
You could actually write also an array of ones, but I will do it like this.

67
00:05:33,020 --> 00:05:36,410
And then the second entry will be the same thing to pull off one.

68
00:05:36,770 --> 00:05:40,040
And then we continue until we are the power of six.

69
00:05:40,670 --> 00:05:44,960
So of course, you could do this more elegantly, but this is just a quick practice.

70
00:05:44,970 --> 00:05:49,790
So I think it's OK if it just do it by hand and doesn't take too long.

71
00:05:51,050 --> 00:05:57,830
But of course, if you would take here 200 points and you would need a polynomial of order 200 approximately

72
00:05:58,460 --> 00:06:03,440
actually one hundred ninety nine, then you would have to write down your many terms.

73
00:06:03,440 --> 00:06:07,610
And then it, of course, would be better to just automate this and not write it down by hand.

74
00:06:08,510 --> 00:06:09,710
But anyway, here's the key.

75
00:06:10,070 --> 00:06:13,550
So this is not our array, and actually we have to transpose it as well.

76
00:06:13,830 --> 00:06:16,970
It's all right and p dot transpose.

77
00:06:19,330 --> 00:06:25,690
And then close it in the end, and maybe we can make this look a bit nicer.

78
00:06:29,650 --> 00:06:31,540
Like this, for example.

79
00:06:33,130 --> 00:06:33,480
OK.

80
00:06:34,600 --> 00:06:37,840
And I will do it like this.

81
00:06:39,130 --> 00:06:39,460
OK.

82
00:06:39,520 --> 00:06:40,230
So I hope you OK.

83
00:06:40,250 --> 00:06:44,620
You see what I did here and I can run this now and we can have a look.

84
00:06:46,690 --> 00:06:53,260
So you see, it's now an array and you have here these individual terms and you see the first column.

85
00:06:53,260 --> 00:06:57,160
So first entry of each of these rows here is always one.

86
00:06:57,490 --> 00:06:59,290
So this is exactly what we have here.

87
00:06:59,590 --> 00:07:05,500
And then for the second entry, we have just the X values of our data, which are these values here

88
00:07:05,500 --> 00:07:07,070
and then so on and so on.

89
00:07:07,180 --> 00:07:10,210
Squared cubed two are four or five six.

90
00:07:10,900 --> 00:07:16,720
So now we have two Matrix X and we want to determine the Vector A with the individual parameters here

91
00:07:16,720 --> 00:07:17,830
a zero to a six.

92
00:07:18,370 --> 00:07:25,810
And of course, we also need to program here the vector of Y, which is basically just this one here.

93
00:07:26,320 --> 00:07:27,790
So that's very easy to program.

94
00:07:28,090 --> 00:07:34,870
Y is equal to data pulley, then the Y component and all the data points.

95
00:07:37,020 --> 00:07:38,820
OK, so then I wrote.

96
00:07:38,880 --> 00:07:45,150
Now we can solve the system of linear equations and linear means linear in a.

97
00:07:45,630 --> 00:07:51,930
Of course, it's not linear in X because you see we have here squared cubed and so on, but it's linear

98
00:07:51,930 --> 00:07:52,350
and eight.

99
00:07:52,920 --> 00:07:58,560
So you see this matrix here, even though you have here squared part two three four five six.

100
00:07:59,100 --> 00:08:04,680
These are all just numbers, as we have just explored when we had had a look at this because these are

101
00:08:04,680 --> 00:08:11,250
really points coordinates and when you calculate the force power of them, they are still just numbers.

102
00:08:11,880 --> 00:08:18,330
So this is here a matrix of numbers, and this is here actually the parameter for which we try to solve

103
00:08:18,330 --> 00:08:19,140
these equations.

104
00:08:19,770 --> 00:08:24,810
So the equations are really linear in the zero eight, one, eight two and so on.

105
00:08:25,500 --> 00:08:31,050
And of course, I think you know this already from high school, you learned, is actually very early

106
00:08:31,050 --> 00:08:36,030
in mathematics how to solve such linear equations or a system of these linear equations.

107
00:08:36,390 --> 00:08:41,760
And a typical way to do this is to use Gaussian elimination methods.

108
00:08:42,030 --> 00:08:44,990
And if you want, you can really program this whole method yourself.

109
00:08:45,000 --> 00:08:50,430
Just remember how you did it in school and then just program this using loops, for example.

110
00:08:51,060 --> 00:08:55,200
But of course, there are also more elaborate methods to solve this.

111
00:08:55,650 --> 00:09:00,900
And if you write it down in terms of a matrix as here, then typically in computational physics, one

112
00:09:00,900 --> 00:09:06,350
uses these so-called l.u decomposition, which means lower upper factorization.

113
00:09:06,360 --> 00:09:14,340
So basically, we try to express this matrix as a product of a matrix that only has a lower triangle

114
00:09:14,910 --> 00:09:16,200
and an upper triangle.

115
00:09:16,890 --> 00:09:19,830
And this leads us then directly to the solution.

116
00:09:20,730 --> 00:09:25,320
So I don't want to go into the details here on how this works, because in this course, we don't really

117
00:09:25,320 --> 00:09:28,530
need to solve systems of linear equations.

118
00:09:28,530 --> 00:09:32,850
But for some problems, a bit more simple problems, it can be useful.

119
00:09:32,850 --> 00:09:38,940
So if you're interested, please just look on Wikipedia, for example, to decomposition, and you could

120
00:09:38,940 --> 00:09:42,450
also try to program this Gorshin elimination method yourself.

121
00:09:43,290 --> 00:09:48,460
However, as I said, we will use here and numpy routine, and it's actually very easy.

122
00:09:48,480 --> 00:09:51,990
It's just and offline arc dot solved.

123
00:09:52,470 --> 00:09:56,970
And then we provide the matrix this one and then the left hand side vector.

124
00:09:57,750 --> 00:10:06,450
So just like this and we want to start a solution which is a vector assay, and now we have already

125
00:10:06,450 --> 00:10:07,080
our solution.

126
00:10:07,080 --> 00:10:10,380
It didn't even take a tenth of a second.

127
00:10:11,280 --> 00:10:14,550
So we can now look at the solutions and these are the solution.

128
00:10:14,550 --> 00:10:18,720
So a zero is 13, a one, two, four and so on.

129
00:10:19,770 --> 00:10:25,140
So we don't really know if this makes sense and if this is correct, but we can, of course, have a

130
00:10:25,140 --> 00:10:26,640
look by plotting.

131
00:10:27,480 --> 00:10:33,690
So let me copy here a few lines of code that I have prepared previously that you can, of course, also

132
00:10:33,690 --> 00:10:34,200
write down.

133
00:10:34,620 --> 00:10:41,160
So you see here, first thing is an X label and then here a specified a limit of the year of the Y axis

134
00:10:41,160 --> 00:10:44,160
goes from zero to 30, as we have learned before.

135
00:10:44,760 --> 00:10:50,700
And then I have a plot similar to our previous plots like this one, for example, very a specified

136
00:10:50,820 --> 00:10:51,510
X list.

137
00:10:51,960 --> 00:10:57,750
And then I plot the function here and then I plots our data points.

138
00:10:58,320 --> 00:11:03,000
But of course, I have adapted this to our current problem, so I have changed these data points to

139
00:11:03,000 --> 00:11:03,300
data.

140
00:11:03,300 --> 00:11:07,680
Polly and I have calculated the why list.

141
00:11:07,680 --> 00:11:13,230
So the Y components of this function, which are actually these values here.

142
00:11:13,650 --> 00:11:20,190
So basically, when you take these functions here or the single function and you take all different

143
00:11:20,190 --> 00:11:24,120
values for X from our X list, then this will be the function.

144
00:11:24,690 --> 00:11:35,340
So a zero times one basically so x list power of zeros one then plus a one times x plus a two times

145
00:11:35,340 --> 00:11:38,970
x squared plus a three times X to the power of three and so on.

146
00:11:39,420 --> 00:11:49,290
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

147
00:11:49,290 --> 00:11:49,980
20 even.

148
00:11:51,450 --> 00:11:58,410
OK, so now you see, we have now are a seven data points, which are exactly the data points from here

149
00:11:58,410 --> 00:11:59,370
close to zero.

150
00:11:59,370 --> 00:12:01,350
So these are really these data points here.

151
00:12:04,000 --> 00:12:09,140
And you see, we have fitted a sixth order polynomial because we have seven coefficients.

152
00:12:09,520 --> 00:12:13,060
And you see it really fits all the data points perfectly.

153
00:12:14,320 --> 00:12:21,730
However, of course, you can also see that when we get rid of this town far away from these data points,

154
00:12:22,330 --> 00:12:27,670
the polynomial will give totally incorrect results in terms of our data points.

155
00:12:27,970 --> 00:12:34,450
So you see here we have data points in the range of minus 32 30, maybe minus 40 to 40, but here it

156
00:12:34,450 --> 00:12:36,070
goes up to several thousand.

157
00:12:36,820 --> 00:12:43,240
So it's such a method is nice, mathematically speaking, because we are really fitting these data points

158
00:12:43,240 --> 00:12:48,010
perfectly with a single function and not just piece wise, as we did with the spleens.

159
00:12:48,640 --> 00:12:55,480
However, it's not really useful to use this for extrapolation.

160
00:12:55,930 --> 00:13:01,660
So everything that's outside of the range of these data points will give a totally inaccurate result,

161
00:13:02,260 --> 00:13:03,400
and it gets even worse.

162
00:13:03,730 --> 00:13:10,210
We could, for example, fit these data points with the even higher polynomial where we just arbitrarily

163
00:13:10,210 --> 00:13:17,200
choose some of the parameters, and then we could basically get any behavior for these ranges outside

164
00:13:17,200 --> 00:13:18,100
of the data range.

165
00:13:19,240 --> 00:13:19,630
OK.

166
00:13:19,990 --> 00:13:24,130
So but this was just an add on, as I told you, just a nice mathematical method.

167
00:13:24,140 --> 00:13:31,510
And also, I want to tell you how you can use the Nampai module in October solve to solve systems of

168
00:13:31,510 --> 00:13:34,300
linear equations just in case you need it sometimes.

169
00:13:35,320 --> 00:13:37,180
And of course, we will not actually need it.

170
00:13:37,600 --> 00:13:41,020
But yeah, it's still a good tool to have.

171
00:13:41,770 --> 00:13:49,120
And also, it's it's a very nice result that we are not able to fit our data perfectly with just a single

172
00:13:49,120 --> 00:13:51,550
function, which is a higher order polynomial.

