1
00:00:00,150 --> 00:00:01,260
So here's my solution.

2
00:00:01,560 --> 00:00:04,170
If you had problems, it's really no problem.

3
00:00:04,590 --> 00:00:10,650
Maybe you can follow along with me and after each sell that I program, maybe you could stop and think

4
00:00:10,650 --> 00:00:13,200
about how you would continue from there.

5
00:00:13,890 --> 00:00:18,720
So the first thing that I will do, that's not really necessary, but I will do it here is that I will

6
00:00:18,720 --> 00:00:20,280
generate the three data sets.

7
00:00:20,880 --> 00:00:24,750
So for the three oscillators, the first one would be an array.

8
00:00:25,590 --> 00:00:34,560
And then for the X values, we have, the physical T values, which would be solution underscores three

9
00:00:34,560 --> 00:00:45,060
for C T and for the Y values we have solution on to score three or C Dot Y and then zero.

10
00:00:45,900 --> 00:00:50,940
And of course, for the other two datasets we have and the other two indices.

11
00:00:51,240 --> 00:00:52,560
So here one and two.

12
00:00:53,610 --> 00:00:59,190
So this is not a data, and I will show it to you first for data one, which is the oscillator with

13
00:00:59,190 --> 00:01:00,170
the index zero.

14
00:01:00,180 --> 00:01:01,320
So the very left one.

15
00:01:02,700 --> 00:01:06,210
So first of all, we must define, of course, the model function.

16
00:01:06,660 --> 00:01:14,040
So as before we write def function model like in the previous section, but not the previous one, but

17
00:01:14,040 --> 00:01:15,510
one of the earliest sections.

18
00:01:16,110 --> 00:01:22,800
And then we have here the argument, which is T and our case, and we have two parameters which will

19
00:01:22,800 --> 00:01:33,300
be a vector which we call a f and we return a zero times and p dot cosine.

20
00:01:33,660 --> 00:01:35,760
By the way, you could also use sine here.

21
00:01:35,760 --> 00:01:40,650
It doesn't matter because this is just a matter of the faced and we will fit anyway.

22
00:01:40,660 --> 00:01:42,820
So you could write here really cosine.

23
00:01:42,840 --> 00:01:43,410
It doesn't matter.

24
00:01:43,890 --> 00:01:45,210
Of course, I know sine doesn't matter.

25
00:01:46,590 --> 00:01:54,840
So then we multiply by T and we add the factor phase, which will be a one, and then we add the other

26
00:01:54,840 --> 00:02:02,280
two functions so it will copy and then fix it two to three.

27
00:02:04,580 --> 00:02:07,550
Four, three and five.

28
00:02:09,449 --> 00:02:09,860
OK.

29
00:02:09,970 --> 00:02:10,470
Looks good.

30
00:02:12,060 --> 00:02:19,500
No error, so this seems to work, and now we can define the error function, which the courts previously

31
00:02:20,250 --> 00:02:20,600
fit.

32
00:02:22,080 --> 00:02:26,010
And of course, the arguments are to function, which will be our model function.

33
00:02:26,520 --> 00:02:33,960
Then we have the coefficients, which will be eight and we have the data, which will be data one.

34
00:02:35,280 --> 00:02:40,170
So here we had some comments, which I will not write, which explained the parameters, but I think

35
00:02:40,170 --> 00:02:41,040
it's pretty clear.

36
00:02:41,400 --> 00:02:42,450
So we would just go ahead.

37
00:02:42,750 --> 00:02:52,620
And since it's a sum, we will start with the finding this to be zero and then looping for I in range

38
00:02:54,180 --> 00:02:57,450
length data zero.

39
00:02:57,600 --> 00:03:03,960
If you copy this from the previous notebook, then you already have this year and then the error will

40
00:03:03,960 --> 00:03:08,550
be the old error, plus the individual contributions.

41
00:03:09,280 --> 00:03:15,390
And so now we have to, you know, basically program this one here.

42
00:03:16,590 --> 00:03:23,610
So will be the brackets squared and then later I comma one.

43
00:03:23,880 --> 00:03:28,050
So this will be the other way around one comma.

44
00:03:28,050 --> 00:03:39,120
I will be the Y component minus F and then data zero comma I, which will be the X component and then

45
00:03:39,610 --> 00:03:42,300
has the argument for the function DX coefficients.

46
00:03:45,430 --> 00:03:45,760
OK.

47
00:03:46,390 --> 00:03:53,200
And then we print the summer so as not to print the picture.

48
00:03:53,590 --> 00:03:56,780
So as I told you earlier, you don't have to do it the loop.

49
00:03:56,800 --> 00:04:00,190
You can also define an array, of course, and then use to some command.

50
00:04:01,600 --> 00:04:03,500
So this will be our error.

51
00:04:03,520 --> 00:04:06,280
And now I want to just test if everything works.

52
00:04:06,910 --> 00:04:11,980
So I will just use some values for the starting parameters.

53
00:04:14,470 --> 00:04:14,820
I don't know.

54
00:04:14,840 --> 00:04:20,560
We could take random values, but maybe it's good here that we can compare with each other, that I

55
00:04:20,560 --> 00:04:22,210
just take some values.

56
00:04:22,750 --> 00:04:23,290
I don't know.

57
00:04:24,070 --> 00:04:29,800
And then I write error a fit.

58
00:04:32,000 --> 00:04:37,480
Function model, comma a comma data one.

59
00:04:39,380 --> 00:04:46,910
And this will be the era with these, with this value here as a starting value.

60
00:04:48,020 --> 00:04:57,740
So the thing is the arrow should probably be in a similar range for you, but it's probably not exactly

61
00:04:57,740 --> 00:05:04,100
the same value because we have taken random starting conditions for the simulation here.

62
00:05:04,110 --> 00:05:10,310
So your data will look a bit different compared to mine, which is why your number for the error may

63
00:05:10,310 --> 00:05:11,150
be a bit different.

64
00:05:12,470 --> 00:05:20,290
OK, now the next thing will be to define the gradient def arrow gradient or fits gradient we called

65
00:05:20,290 --> 00:05:20,960
it previously.

66
00:05:22,370 --> 00:05:24,410
Then we have here the same arguments.

67
00:05:28,920 --> 00:05:32,580
And once again, it's a song, so I wrote Total equal to zero.

68
00:05:32,880 --> 00:05:37,500
So as I told you previously, you probably copy this just from the other notebook and then you don't

69
00:05:37,500 --> 00:05:39,690
have to write all of what I'm writing right now.

70
00:05:40,020 --> 00:05:45,690
But I think it's better that I just write it one more time so that you can really see what's going on

71
00:05:45,690 --> 00:05:49,590
here in case you do not have the old notebook or you don't want to look it up.

72
00:05:51,120 --> 00:05:51,480
OK?

73
00:05:52,290 --> 00:06:13,920
So we basically have to add up now total the old total plus data one comma I minus half data, zero

74
00:06:14,310 --> 00:06:14,730
comma.

75
00:06:14,730 --> 00:06:17,690
I come out coefficients.

76
00:06:17,690 --> 00:06:20,660
So this is similar thing compared to here.

77
00:06:20,670 --> 00:06:22,140
So this is basically this one.

78
00:06:23,010 --> 00:06:27,360
And then we have to multiply only by this one here.

79
00:06:27,900 --> 00:06:33,090
So this I will call the FDA, which we have to define, of course, the FDA.

80
00:06:33,780 --> 00:06:41,310
And then in the end, we return minus two times total so that we have this factor here.

81
00:06:42,840 --> 00:06:53,250
OK, now we only have to define this one, the FDA, that's an array because it's a vector and you see

82
00:06:53,250 --> 00:06:54,110
what the vector is.

83
00:06:54,120 --> 00:06:54,960
It's just this one.

84
00:06:55,620 --> 00:06:57,120
So now it's a bit annoying.

85
00:06:57,120 --> 00:07:04,710
I just have to tag along and be sine omega one times data as well.

86
00:07:04,710 --> 00:07:11,610
B t so our T list theta zero comma I and then plus a one.

87
00:07:13,680 --> 00:07:17,130
And this we have several times.

88
00:07:17,130 --> 00:07:19,770
But first, let me program this one here.

89
00:07:20,100 --> 00:07:29,070
This will be minus a zero times and p dot sign of this whole thing.

90
00:07:30,120 --> 00:07:39,600
So and p told Sign and now we just have to copy this two more times and then fix, of course, the indices.

91
00:07:41,760 --> 00:07:42,330
All right.

92
00:07:42,690 --> 00:07:43,680
So I copy this.

93
00:07:43,890 --> 00:07:47,130
Here we have our first entries that we had before.

94
00:07:48,240 --> 00:07:55,170
Now, to the other ones, this will be omega two and a three.

95
00:07:57,210 --> 00:08:02,430
Here we have eight to omega to a three.

96
00:08:04,200 --> 00:08:16,110
And finally, Omega three, a five, a four, omega three and a five.

97
00:08:16,860 --> 00:08:18,930
OK, good.

98
00:08:18,940 --> 00:08:22,170
So now this is our gradient so we can call it arrow.

99
00:08:22,260 --> 00:08:25,410
Or maybe just let me copy this that I don't make any typos.

100
00:08:25,980 --> 00:08:33,600
Our fate gradient and then our function model a comma data one.

101
00:08:34,500 --> 00:08:41,039
And so once again, you probably will have different values here because you have a different dataset.

102
00:08:42,150 --> 00:08:46,620
And now I will test what happens if we do one iteration, one step.

103
00:08:47,310 --> 00:08:49,560
So we must defines a learning rate.

104
00:08:49,680 --> 00:08:57,210
So, h, we called it previously, it takes some small value and as I said, I will do one test step.

105
00:08:58,260 --> 00:09:04,080
So a the parameters will be updated according to a change of minus page times.

106
00:09:04,740 --> 00:09:06,810
And then what you just wrote?

107
00:09:08,910 --> 00:09:10,740
So we do this.

108
00:09:10,740 --> 00:09:16,800
And then afterwards, I will print the value of a profit.

109
00:09:18,120 --> 00:09:19,590
So basically this one.

110
00:09:20,730 --> 00:09:25,050
So right now, we have a row of 360, 2.5 four.

111
00:09:27,900 --> 00:09:35,880
Now we have an error of 295 Dot six or five, which is smaller when I rerun it.

112
00:09:36,660 --> 00:09:45,900
It will get smaller and smaller and smaller and smaller until eventually it will turn to zero.

113
00:09:46,260 --> 00:09:54,390
And then the parameters will hopefully look good so that I don't have to type enter over and over.

114
00:09:54,390 --> 00:10:00,120
I write iterations is equal to, I don't know.

115
00:10:00,750 --> 00:10:05,230
Here, of course, it depends sometimes a bit on the starting conditions, how many iterations you need.

116
00:10:05,280 --> 00:10:07,290
Maybe we try out one thousand.

117
00:10:07,830 --> 00:10:08,700
Let's see if it works.

118
00:10:09,270 --> 00:10:14,310
For I in range iterations.

119
00:10:15,060 --> 00:10:20,340
So it could also happen that in your case, you need more or less iterations because you have a different

120
00:10:20,340 --> 00:10:20,790
data.

121
00:10:21,570 --> 00:10:26,790
And also, if you rerun the whole notebook, it could also mean that you need a different number of

122
00:10:26,790 --> 00:10:27,540
iterations.

123
00:10:28,000 --> 00:10:33,970
So it's never bad to have too many iterations, but of course, and it takes much more time.

124
00:10:34,930 --> 00:10:38,080
So we just loop over what we've written here.

125
00:10:40,300 --> 00:10:40,660
OK.

126
00:10:40,780 --> 00:10:54,110
And then finally, we can look at the horrific gradient, and we can also look at the arrow fits, which

127
00:10:54,490 --> 00:10:56,890
can both be zero, approximately.

128
00:10:57,340 --> 00:10:59,800
And then we can have a look at the fit itself.

129
00:10:59,840 --> 00:11:00,240
It's not.

130
00:11:00,250 --> 00:11:06,820
I've run these two cells and for 1000 iterations took for me, maybe like three or four seconds.

131
00:11:07,330 --> 00:11:11,200
And you see, the arrow is now really, really small, close to zero.

132
00:11:11,560 --> 00:11:14,730
Maybe we could iterate a bit more, but hopefully it's OK.

133
00:11:14,740 --> 00:11:17,880
And you can also see the gradient is pretty small.

134
00:11:17,890 --> 00:11:22,930
All of them are below one, except for this one, which is still a bit high.

135
00:11:23,170 --> 00:11:26,920
So maybe 10000 would be better, but let's see what it looks like.

136
00:11:27,580 --> 00:11:38,380
So Peltier here, let me just scroll up to our previous plotting that we copy this, so we need basically

137
00:11:38,380 --> 00:11:38,920
this one.

138
00:11:38,920 --> 00:11:39,610
I would say

139
00:11:43,360 --> 00:11:51,800
it's curled up, and now I will change this year because we have used our data.

140
00:11:51,840 --> 00:11:56,800
We could, of course, leave it as it is, but I think it's nicer to just use data that we actually

141
00:11:56,800 --> 00:12:03,370
used for fitting some data one with the index, zero Theta, one with the index one.

142
00:12:04,000 --> 00:12:09,520
And now we want to plot our Yeah or Fitbit data.

143
00:12:10,210 --> 00:12:20,380
So I real right here t if all this was what we have used for the for the years over.

144
00:12:20,800 --> 00:12:25,090
So I want to show you this so that you believe me here.

145
00:12:25,150 --> 00:12:32,350
When we solved it numerically, we wrote in the integrates dot solve on the score IVP TVL is equal to

146
00:12:32,350 --> 00:12:39,800
this Lind's based function here, so I will copy this go back down to if all is equal to this one.

147
00:12:41,050 --> 00:12:57,520
And now we can plot this Piatti dot plot for T underscore evolved from a function Model T underscore.

148
00:12:59,580 --> 00:13:00,120
You've all.

149
00:13:02,080 --> 00:13:05,500
Common a common threat.

150
00:13:08,240 --> 00:13:10,820
And this one, I will change to a scatterplot.

151
00:13:13,220 --> 00:13:20,120
And you see, this is the solution of the Fit's, which is the rent curve and the blue ones are our

152
00:13:20,120 --> 00:13:22,510
data points that we have used for fighting.

153
00:13:23,450 --> 00:13:30,400
You see, we have used a model function with, which is a superposition of three cosine functions.

154
00:13:30,410 --> 00:13:32,480
And indeed it worked.

155
00:13:32,580 --> 00:13:34,310
It's it's a pretty good fit.

156
00:13:34,310 --> 00:13:40,430
As I said, we could maybe improve it just a little bit more by using more iterations to decrease the

157
00:13:40,430 --> 00:13:40,760
error.

158
00:13:40,760 --> 00:13:47,270
But I think it looks pretty good now and it's a I think it's a remarkable, remarkable fit and really

159
00:13:47,930 --> 00:13:53,720
a nice thing that we have seen when we just take our eigen frequencies, the three Agria frequencies

160
00:13:53,720 --> 00:14:00,710
that we have and we superimpose them, then we can recover the numerical results so we can have a look

161
00:14:00,710 --> 00:14:03,620
at the values of AA, which are the fitted parameters.

162
00:14:04,310 --> 00:14:07,940
And you see you, they are just some, some values.

163
00:14:07,940 --> 00:14:14,480
So this amplitude amplitude and another amplitude, and then we have here the face factors.

164
00:14:16,250 --> 00:14:22,310
So of course, now we can do the exact same thing with the oscillator number two.

165
00:14:22,640 --> 00:14:24,470
And this I will show to you in a second.

166
00:14:24,920 --> 00:14:28,500
So here you can see I have basically copied the code from above.

167
00:14:28,520 --> 00:14:36,950
So basically just starting once again with the first set of parameters and then looping over the gradient

168
00:14:36,950 --> 00:14:44,330
descent where our fit gradient would be this one with now the data set two and then I have plotted everything

169
00:14:44,330 --> 00:14:47,960
once again, and you see, it works very, very well.

170
00:14:48,860 --> 00:14:55,250
And when we look at the parameters, we see that all of them are reasonable values and this one here

171
00:14:55,700 --> 00:14:57,260
is actually very, very small.

172
00:14:58,100 --> 00:15:00,350
So it's much smaller than the other.

173
00:15:00,360 --> 00:15:03,440
So we could assume that it's probably zero.

174
00:15:03,830 --> 00:15:06,920
And when you look at it, what this means is to get the third entry.

175
00:15:07,250 --> 00:15:09,890
And if we go back, what was the third entry?

176
00:15:10,310 --> 00:15:12,530
This was this one.

177
00:15:13,190 --> 00:15:18,620
This was the amplitude for the second function, so the amplitude for the cosine function with omega

178
00:15:18,620 --> 00:15:19,100
two.

179
00:15:19,850 --> 00:15:25,580
And so indeed, this was the omega two frequency here.

180
00:15:25,910 --> 00:15:32,900
And you see, for the second oscillator, this one for the eigenvectors doesn't have an entry for this

181
00:15:32,900 --> 00:15:34,820
particular frequency.

182
00:15:35,530 --> 00:15:40,190
You can also see here in the full transform that the second oscillator does not have a peak here.

183
00:15:40,940 --> 00:15:47,900
So it makes sense that also in the fit, the second cosine function with the Omega two does not is not

184
00:15:47,900 --> 00:15:48,920
taken into account.

185
00:15:49,370 --> 00:15:51,110
And we didn't tell this to the program.

186
00:15:51,110 --> 00:15:56,270
We just said, please find the optimal fit using these three cosine functions.

187
00:15:56,270 --> 00:16:02,420
And it told us that the second cosine function or did we get the best fit if we don't take the second

188
00:16:02,420 --> 00:16:03,860
cosine function into account?

189
00:16:04,220 --> 00:16:09,350
And it's the same result as we got from the you transform and from the eigenvectors.

190
00:16:10,220 --> 00:16:18,320
So basically, we have now another way, or we have found another way that confirms our previous results.

191
00:16:18,680 --> 00:16:21,110
We have first solved the problem numerically.

192
00:16:21,590 --> 00:16:28,670
Then we have solved the eigenvalue problem by calculating the eigenvalues of the matrix that characterizes

193
00:16:28,670 --> 00:16:29,930
the equations of motion.

194
00:16:30,350 --> 00:16:37,010
And from there, we have calculated the EIGEN frequencies, and now we have taken the cosine functions

195
00:16:37,010 --> 00:16:40,340
with the ion frequencies and have superimposed them.

196
00:16:40,880 --> 00:16:45,230
And we just had to fit the parameters a and fine.

197
00:16:45,650 --> 00:16:48,110
And we have seen, OK, it really works.

198
00:16:48,110 --> 00:16:55,190
We can use three harmonic functions to recreate our solution, and everything agrees very, very well.

