okay so this is the third and final lecture on data simulation what I want to do with this lecture is actually show you how to implement the data simulation texture architecture in practice and so I just want to recall that what we're trying to do is we're thinking about we the fact that we have a model we have some initial conditions as well to that model and then we finally we also have measurements of the system let's call this generically it's represented like this so we're given a model we know it's incomplete we have measurements we know it's noisy and we have measurements this initial state estimation which we know is not quite accurate and we also have here measurements as we go in time of this system so the modification of this model right is to think about q1 being some unmodeled physics I don't have a model for it I know I have error in my model and this is just my approximation and so I'd like to understand the impact on this our initial state estimation instead of being Y naught is actually there's a noisy measurement so we're off by some amount q2 and then finally every time we take a measurement it's not quite accurate so this is the generic framework for thinking about data simulation there in the model the air and initial measurements or initial state of the system system to initialize this model and then of course measurements as we go and they are in fact noisy measurements so we talked about last time in the last two lectures very simple format for both using a vector nature of this and thinking about Coleman filtering what we want to do today there's actually program it up and show how it works in practice what we're particularly going to do is just sort of highlight the model we're going to assume that in fact we have a perfect model and the model we're going to work with is the Lorenz equation so we're in assume I have a perfect model and what I'm assuming though is that my initial state has some air q2 we're going to assume is that we have some kind of random Gaussian noise distribution for the measurements so we have the measurement plus some random noise which will be Gaussian and every time we take a measurement of the system to do our data a simulation it also will have some measurement error when we take estimates of the of the system okay so that's going to be the basic framework so all we care about now is q2 q3 and we're going to run a model with imperfect initial data compare it to measurements and try to figure out the data simulation strategy so that we get the best forecast possible given these error metrics okay so I'm gonna pull up MATLAB and we'll start playing within the framework of MATLAB to try to model this phenomena all right so let me pull this up here pull up our MATLAB go there we go alright so here's what our system is a pre-programmed of our Lorenz equation I will run a simulation of this just so you're familiar with it although we're using it quite extensively in this course is a nice toy model so hopefully it's fairly familiar to you and so here's the code so code basic that is clear I'll close all CLC I'm gonna run it from time 0 to time 20 I'm gonna parameterize this with three parameters Sigma and let me in fact I can close this down push this down there so just have that open so we're gonna do is we're gonna have three parameters Sigma's 10b is 8/3 ours 28 so these parameters are typically picked this is where we get chaotic dynamics in the Lorenz attractor we're gonna specify initial condition of 5 5 5 the initial x y&z location are perfectly known at this point let's say there five each and now we can simulate that so you Zoey for five you call the right hand side function lor underscore right hand side you tell it the time you want to solve it for you give us the initial state you can prescribe the tolerances here and then you've passed in your three variables now what we get out is going to be our truth we're gonna sum ajan that 555 is actually the state of the system so this will be our truth measurement and what we do in simulations is we're gonna have we're not gonna have to be able to get five five five we're gonna have some five five five plus some noise on top of that and that's what's going to be make it tough to forecast them and actually to match the true solution the true solution is here the true X is the first column true by the second column true C is a third column and then we can plot this so let's go ahead and do this first I will very briefly show you what the right hand side looks like here it is so right hand side of the Lorenz equations is Sigma here's a quadratic term X 1 plus X sorry linear term minus X 1 X 2 then we have a quadratic here X 1 times X 3 which is the X in the Z term plus our x1 minus x2 and then here's another quadratic term x1 x2 minus B X 3 so that's right hand side we pass everything in March it forward in time and so hopefully everybody's somewhat familiar with how to do that and so let's just run this thing see what we get so we run it here's what our simulation produces it's the classic Lorenz attractor I think I would try to rotate this so you get a better view okay there we go all right so this is Wow can't see that so well so let me line with to that thing because I don't I want to make sure we all can get a good view of it or maybe I can make it just smaller ok come here and let's put here line with oops cut it off true okay let's try that yeah that's a little better there we go yeah I could rotate it okay so this is your trajectories of the Lorenz attractor you started here at 555 this thing took off and is starting to be on this strange attractor so this is the dynamical system we want to look at and part of what we'd like to do is say well I would like to follow the trajectories not just qualitatively but quantitatively but the problem is I'm going to have a measurement error on my initial conditions and so as I go for a long we know that this system is very sensitive to initial conditions being off as I'll show you in a minute and so we're going to fall off of our trajectory pretty fast and so what we'd like to do is figure out a way to improve that and we're going to do the data simulation to do so okay so let's go ahead and close this down and now that I have this basic code here for solving this things along what we can start thinking about doing and saying okay I want to take this basic architecture but now I want to run a bunch of initial conditions through it I already got the true solution so now let's come here I'm going to comment first out this code because we don't really need it anymore and now we're going to do is make a loop what we're going to do with this loop is going to produce eight different initial conditions so that is our objective right now is I'm going to go through this eight times I'm going to show you if you hit this with noise how eight different viewpoints of this looks like with just a little bit of noise on it okay so we're going to take those initial conditions that we had previously which were the five five five and now we're going to perturb them with a little bit of white noise okay so how to do this so first we get to decide the strength of our noise so I'm gonna make the noise here it's called Sigma 2 remember this is the variance of that q2 variable the q2 in that the white noise I'm going to add is going to be given by here we will make this of strength 1 so strength of noise now here I'm going to take X naught which we had up there and say X naught okay the initial conditions to this thing effect let's call this X I see for the initial conditions will be that X naught I had plus R and N with Sigma 2 in front of it and this thing here is 1 by 3 okay and we're going to send it now X I see so what I've done here is I've determined the strength of the noise and we can play around with this we can make it stronger noise weaker noise whatever we want to do we're going to go through eight times through the loop X I see is what the original initial initial condition was which was 5 5 5 and now I add some random a random variable to it and when I go through this I solve I'm going to get a solution and then I'm going to come back through run it again with a different noise and I'm going to compare the truth with what I get here and so to make this easy comparison easier I'm just going to look at the X variable and just see how well the X is modeled from this what happens to the X as a function of time with 8 different noise realizations versus up here which is the truth of what X should be okay so what we've got to do now is going to go ahead and come here and we're going to go ahead and start running this through the system here so first of all every time I run this I want to come here and I want to plot it so part of our goal is a subplot 2 4 2 J so I'm going to do eight plots ok let me make a little more room here for us so I'm gonna make eight plots here each subplot is for one of these different noise realizations oops okay let me bring that up a little okay so once I get that then I say plot T versus X true okay and we'll make that in let's say blue actually let's do magenta that's gonna be the truth solution and then T versus X saw first column let's make that in magenta and green how's that and line with two okay so I go through this thing eight times and we're gonna compare those against each other so let's run that see if this thing here oh there we go okay so here is our comparison of these two so remember magenta is what it's supposed to be and green is what my model gives now because my measurement air is slightly off in fact I'm very close to the actual starting value my my noise realization puts me very close to the original X naught but you can see in these eight different realizations that in fact I don't do a very good job in the future state prediction so just consider this one up here look what the magenta is doing compared to the green very quickly I cannot use this green which is my model with an imperfect initial realization to do any meaningful prediction of what's going on with the magenta and you can see that's the case for all of this data here so this is the problem that we have when we have complex systems where small initial condition perturbations do not allow us to do forecasting in any reasonable manner and this is a prototype example which is clearly whatever I have does not do it very good job predicting the future and what I'd really like to do is make long estimates of the future state and get better forecasting capabilities and to do that I've got to figure a way around this issue what data simulations going to do is take measurements occasionally and once you take those measurements you're going to update the state of the system relative what your model is saying versus what your measurement is saying remember the first two lecture this showed you exactly how to do use your model prediction with an innovation informed by your error or by your measurement of the system directly to give you a better state estimation so that's what we're going to do next so let's go ahead and kill this and now what we're going to do is build a new model here come here okay so now what we're going to do is start taking this code and taking it in this direction and start doing some data simulation so you've seen that I have problems and so now I'm going to take measurements so what I did previously is I ran the code my time step is 0. 01 but let's say I have a measurement every 1/2 unit of time every T equals 0. 5 so at 0.
5 I'm going to assume that my measurement is the truth plus a little bit of noise those can be determined by my q3 that we had an initial data simulation format and so we're going to have some measurement here so let's go ahead and build a model for this error okay so what we're going to do noisy observations so let's say that what I want is T data which will be T from 1 to 50 to end so what have I done here remember my t variable is the OD solver step 2 every point a 1 so every 50 steps of my model is point five units so that's where I'm gonna collect my data this is where I'm going to assume I have measurement to help me do the data simulation portion okay so T data are the measurement times okay now the length of this thing I need it because I'm going to do a loop to go through this is N and what I want to do is now start thinking about I'm gonna make three variables at each time step remember I have a measurement and have air at time point five time one 1. 5 to 2. 5 so as I go for each one of the variables X Y Z I have a measurement error and so what I want to do is make a vector where I'm going to make a vector of the error I'm going to put on to the actual true measurements at those times okay so here are the vectors X n R and N and by one yn is equal R and N and by one and then C of n he goes round and and by y1 okay so if these are our vectors tell me what the area is going to be that I'm going to put on each of the time points okay I can also determine how much noise I want on the same let's say Sigma 3 is equal to strength for so air so or measurement error so if I have a very good sensor this 4 would be very small but if I have a bad sensor this gets big so this is the variance of my measurement from the sensor itself so every time I take a measurement I know it's in perfect this number tells me how imperfect it is okay so I'm just picking some numbers here it doesn't matter so much you can play around with it later and so my x data points are x1 every step of 50 to the end plus Sigma 3 times xn so I think what I've is bid this is now my measurements necks its what the truth was plus Sigma 3 xn ok and I got to make sure my truth is still there so let's go back up here so this is going to be so actually I should just use X true okay all right so it's the truth plus some noise and I'm going to have one for both X Y & Z so Y data is equal to Y true 150 to the end plus Sigma 3 times yn and then finally Z data equals Z true 150 to the end plus Sigma 3 times y n so now what I have these are my measurements what I'm going to show you really quickly as we run this is I'm just going to plot the actual time series versus where the measurements are so let's do this for X data versus the truth so what we can do here is do the following plot plot it's T data versus X data we're going to put a red circle there that's where the measurement is and then we're going to compare that to T versus X true and we'll make that a black line line with two and let's run that so now you see what I'm doing I'm running through this thing oops let's do a new figure here look close the old figure before so okay let's run that had the other figure open okay here we go and out comes this okay so now this is a telling picture and so this is kind of telling you the story a little bit and what it's telling you is the black is the truth so even if you had in the red is measuring the truth or trying to measure the truth and it has a certain error so you can see that the red is off of that black and again this is with the Sigma equals four if you make it smaller it will shrink down on to the on to the black line and as you make it bigger it gets further and further off the black so the question is the following I have a model with imperfect and initial conditions I have those red measurements how can I use their measurements to keep me on this solution remember that I fall off of this solution almost immediately by the time I'm here if I just if I make that initial condition error that I have I'm already way off of the solution by the time I get to in this region here so that my prediction and my model don't agree at all okay but here what I have the possibility of doing is saying well if I have those red measurements maybe I can simulate the future even if I don't have the right initial data and so that when I go out into the future I get a good prediction of that future state because it's going to be pinned to these red balls these red dots are telling me that's where the measurement is so your model better be close to this or my simulation frame that's going to pull it into that okay so let's go ahead and do welcome back that sound you heard was the battery or the microphone going dead so I'm now here to clean up after yesterday's guy you know the dude wearing the Paisley purple tie I mean who does that but I'm now they're gonna finish it off this data simulation lecture and so apologies but at least this is a fix up so where we left off is I showed you the truth simulation and I showed you some red dots with where the measurements are so when I walk through now is a code that's gonna do the data simulation itself okay so I've actually got the code here and what I wanted to walk through is just how we're going to construct this so I want a data a simulated solution I have measurements and I have a model prediction so I have those two but now I want to integrate them together so what I'm going to do is come up with a variable called X da so this is my X data a simulated solution so that's what the DA is there for so initially started off as being an empty field and I put nothing in there and what I'm going to do is I'm going to start to simulate my model for half a unit of at a time the reason I have half a unit is because that's where my measurements are every half unit I actually have a sensor giving me a recording that I can use to change the state of my system right so to give me an innovation to what my model says right so one way to think about this whole process is I'm gonna simulate for a while reset my state in other words restart a new initial conditions run the model again restart the new initial conditions and run it again with the idea that every time I do this I'm pinning my model back to the measurements and the hope is that can make a long time prediction to the future so here's what I do I'm going to do a for loop one to the length of T data minus one now T data are the number of measurements so presumably when I start at time zero that's the first measurement I already have initial condition so I don't need to simulate there I just take my initial measurement and starts it going forward so that's why I go t data minus one so there's still T data minus 1 points in the future that I got measurements at and what I'm going to do now is simulate my model for T span from zero to time point five and again steps 0.
01 so I'm going to run half a unit out into the future and so what the T span is and so how you run the OD four five model for half a unit into the future I go run it and what I pull out of there is my solution vector which is now going to be the initial condition look like that's this this was what my initial condition would be for my next model run from time 0. 5 to 1 and then the next loop abhi from 1 to 1. 5 and what this initial condition is the last point of this so it's X all and 1x all n2x all and 3 so that's my x y&z positions after running at half a unit in time okay I'm also going to save my data into this X data file remember this is going to be now take X stat is going to be I'm going to take so sorry here so what I'm going to do here is I'm gonna start writing in this is where my data was collected remember that the data file X dot can I'm gonna write my where my data actually is so X data J plus 1 Y data and Z data this is where my x y and z measurements okay I'm gonna say that is X data it's a vector with three numbers because I'm gonna measure XY and Z at the J Plus first point so remember I'm gonna go through this loop J is 1 so we're running out to 0.
5 I'm gonna go grab that measurement at 0. 5 that's the J Plus first point now here comes the big trick with this with data simulation I have measurements X stat I have a model prediction X I see not those are the two things I have telling me what the solutions should look like at point five at time one at one point five and I want to compare them so the way the data simulation framework works is by creating the calman filter K which is the variance of my model divided by the sum of the variance of my model plus the variance of my measurement and in this case since they're randomly distributed Gaussian variables with variance Sigma 2 and Sigma 3 I know exactly what those are what the K is so I can compute the common filter directly normally in applications you would actually have to come back and actually do some statistical estimates of your measurements and of your model to compute the variance of the model and the measurements and compute that quantity there which is the Komen filter once I have that then I can say actually my initial condition then at time point 5 or time one where mrm is simulating is its xic which can be the initial condition to go back to do a run again is the value of my model plus calman filter times the innovation which is the difference between my measurement and my model so you go back to the previous two lectures this was exactly what we wrote down for the simple example of colon filtering and here it is again here so it's a three vector my measurement locations and my model locations are the same and I just improve my model with that innovation which takes into account the statistics of my measurement error against the statistics of my model air okay once I get that I say that so now so I guess my initial condition where I'm going to run it again but in the meantime I'm going to save my simulation data X all so I'm gonna take the XDA which my data simulated solution so X da is what it was before and I pinned to it X all all of the rows and others all the time points except for the lab 1 why because when I go from time 1 to 0. 5 I don't need a save point 5 because time point 5 is actually the initial point for the next simulation so I don't want to double count the time slot 0.
5 or the slime top time slot 1 or 1. 5 I want to actually go ahead and save the data up to just below point 5 and then the next simulation starts from point 5 all the way just to below one and then from one just below 1. 5 and then 1.