 So last time we met, we were talking about lists and I showed you a few examples of the ways that you can manipulate lists or query them for certain of their elements. I'm going to show a couple of more little list manipulation commands here at the start and then we'll talk about matrices or if you like two dimensional lists and start to see some nice things that you can do with them. Okay, so I'm going to first show you how you can join two lists together. So I'm going to define the first one as fruits. So I'll say apples, comma, oranges and then I'll define vegetables or veggies equals cucumbers and squash and now if I want to put those two together I can use the command join so I can for example define a new list all equals join and then I list the lists that I want to join sorry that should be a square bracket so it would be fruits and veggies. Okay, so now you see I've got a list where veggies, these two elements have been concatenated to the end of the fruits. All right, so that's nice if you have a couple of sets of data and you want to put them together into a single list. All right, now sometimes it's nice to be able to sort lists, okay, so you may have a list of numbers and you might like to order them from high to low or low to high or if you have a list of names such as this list here you may want to alphabetize. So let's see how those things work. All right, so first of all let's just try sort all, all right. So now you see when you have list of things that begin with letters the sort command will order them in alphabetical order, right, A, C, O, and S. How about if you have numbers? So let's remind ourselves how the table command works so I'm going to define squares is going to be equal to the list of the squares of the first 10 integers, so we did this last time. This is a handy way of making that list, all right, and now what I can do is actually let's make this in decreasing order, so I put the increment minus 1 and then start from 10 and go back to 1 and now you see it's arranged from high to low. If I want to reorganize that and make it go from low to high I can say sort squares. Oops, I have to spell it correctly. And now you see it orders from low to high, all right, so let me make this equal to something else. I'll call it new. Now what if I had a list like this that went from low to high but I actually wanted to sort it from high to low? Well, there's an option to the sort command so let's define newer equals sort new and if you say greater with a capital G that will sort from high to low. So now you see this list new has been reordered from high to low in newer by including this option greater. Okay, so these manipulations can be useful. All right, now so that's about all I'm going to tell you about manipulating simple lists, okay? There's obviously a lot more that you can presumably do but these are some of the more useful ones and many of which we will actually use at some point in the course. All right, so the next thing I want to do is introduce you to matrices, okay? So a matrix is a two-dimensional array of numbers, all right? And the way that they are defined in Mathematica is as a list of lists, all right? So what I'm going to do now is we'll type one in and then I'll tell you a little bit about the format and how to interpret it, okay? So I'm going to define a matrix now which is called data, okay? And it's going to have three rows and two columns, okay? So in Mathematica format you enter a row as a list and a matrix as a list of rows, all right? So I'm going to show you that now and then you'll see how it works. Okay, so a list of lists means I should have a curly bracket and then within that a curly bracket for the first list or first row and in this case it's going to be 0, 10, okay? So that's the first row in my matrix, then a comma and then the second one, 1, 25 and then the third row is going to be 2, 50, okay? If I enter that all it does is regurgitate to me what I entered, okay? Now there's a couple of different ways that you can re-display a matrix so that it actually looks more like what you intended when you input the data. So for example, if I type data and then use the post fix, so that's double slash and then type table form, what this does is it will re-display this matrix which doesn't look so pretty in mathematical format in a more appealing form. And now you can see that in fact this corresponds to an array, two-dimensional array of numbers with three rows, two columns and the first row, 0, 10, second, 1, 25, third, 2, 50, okay? Now there's another way to display a matrix that looks even more like the mathematical version of a matrix and we'll learn later on in the course how to actually do linear algebra using matrices in Mathematica. That command is matrix form, okay? So what that does is it puts parentheses around the table form and now you can see if any of you have had experience with matrices in Math classes that that looks like what you write when you define a matrix in Math class, okay? Now there are many, many ways to manipulate and do calculations with matrices and we'll save that for later on in the course but there's one thing that can already be useful early on, there's one operation and that's what's known as the transpose. So I want to show you how that works. So if you type transpose and then the name of your matrix, what happened there? Well it took a matrix that had three rows and two columns and reorganized it, the elements within it such that it now has two rows and three columns, you could see that if you put the post fix here, table form, okay? So essentially what it does is it moves elements across the diagonal of the matrix, okay? Maybe that's a little easier to see if we were to just make a 2 by 2 here. You can, whoops, I typed in the wrong spot here. If you just nuke this last row, okay, and then enter this command again, you can see that the diagonal is the same but one and ten have been exchanged across the diagonal, okay? It's just a way to reorganize the elements in the matrix, okay? So that's the transpose. Now, the next thing that I want to introduce you to is the idea of using a matrix as a way to store and manipulate data. So for example, it could be some kind of experimental data, all right? And also to display it in a nice fashion. So what we're going to do is we're going to type in a matrix and this matrix is meant to correspond to data that you might measure in a chemical kinetics experiment where you're trying to figure out the rate law and the rate constant for a chemical reaction by measuring the concentration of one of the species as a function of time. So for example, with some kind of spectrometer, all right? Okay, so what I'm going to do is clear data because I like to use that name. And now, I apologize for this but it's worth going through the pain once in a while to appreciate what it's like to enter data in by hand and we'll see more efficient ways of doing it shortly. I'm going to define a matrix so you can think of us as having been in the lab earlier today, made the measurements, we wrote them down in our lab notebook and now we're going to copy those values from the lab notebook into Mathematica and then later on, we're going to play with those values and do something useful, okay? So what this is, as I said, it's going to be concentration versus time. So basically, what I'm going to have is you can think of this as being time in seconds and then there's going to be the concentration of some product in molar, okay? And then I'm going to have the values, all right? So I can think of this as being a matrix that consists of two columns and the number of rows is going to be dictated by the number of data points that I have, okay? So that's what we're going to enter in now. All right, so data equals, first point is zero seconds and 0.01 molar and then we wait 1,000 seconds and we take another measurement and we get 0.00625, so our reactant concentration is going down and then we take another point at 1,800 seconds and the value there is 0.00476 and then a little later, at 2,800 seconds, we measure 0.0037 and then at 1 hour, 3,600 seconds, we measure 0.00313 and then 4,400 and 0.00270 and then two more points, 5,200 that value is 0.00241 and then finally the last point at 6,200 seconds, we measured 0.00208, all right? So there is our matrix and it looks like I have a little problem here. I need a bracket here, thank you, all right? Okay, all right, so I can enter that and now what I want to do is I want to make a nice looking table. In general, maybe it's not worth doing but I just want to show you that you can actually do things like format a table and put headings on the table, all right? So I'm going to type in a command here and then I'll explain to you how it works. Okay, so I'm going to say table form data and then I'm going to put in an option that allows me to make some headings on my table. So table headings, arrow, okay, and now I can put in, first of all, a set of headings for the rows but in this case I don't want any because the time is basically a good indicator of the rows. So for the rows I'm going to say none and then I'm going to put in a heading for each of my columns, okay? So for the time, I'll put in time in seconds and then comma and then concentration of A in molar, okay? Now if we enter this, we get a nice looking table, all right? So if we would have just said table form data, we would have a table without any headings so all we did was make it look nice. We didn't put any rows, labels on the rows, not necessary but we have these nice things that help us to see what we actually entered into our matrix, okay? Now next thing I want to do is plot these points, okay? So we have a new plot command and the difference between this new command and the one we already learned about is that the plot command we learned about previously plots a function that's evaluated over a range that we specify. Here we don't have a function, we just have a series of points, data points, in other words a list of data points and so we have a different plotting function, it's called list plot and it has many of the same options as the plot. The only distinction being what's the nature of the thing that's being plotted, okay? So when you have discrete data points you use the command list plot, okay? So here's how it works, list plot, bracket, you give the name of your matrix and if we want we can put some labels so I'll put axis label arrow and the x-axis can be time in seconds and the y-axis concentration in molar, okay? And now if you plot that, you see that it plots discrete points. Those are the ones that we entered in, okay? Now one thing that I like to do is to make these dots a little bigger. So Mathematica has a default and I remember what it is, it's probably 1% of the plotting width or something and as you can see those are pretty small dots so I like to increase it and we can do that by adding another option which is plot style arrow and then point size and I believe, whoops, that should be a capital S if I put in 0.02 so the fraction of the plot frame that'll basically double the size of those dots so if you enter that you see that your dots are much more visible, okay? All right, now suppose, okay so just to recapitulate here, so this is some data that you may have measured something very similar in G-chem lab in an experiment in general chemistry, all right? Now suppose I have a good idea that this reaction is either first order or second order in this reactant A. Let's see if anybody can remember and tell the rest of us how it is that I can determine the reaction order by looking at the data, yes. What he said is you look at the basically the functional form if it's exponential it's first order, what about if it's second order? Right, so remember we have from chemical kinetics we learn about integrated rate laws which tells us how the concentration of a reactant or a product depends on time and later on we'll remember how it is we derive those but for now let me just write down the formulas, okay? So first order, the concentration of A at time t is equal to the log of that actually is equal to the log of the concentration at time 0 minus the rate constant times time, is that what's familiar, okay? What about for second order? Well in that case 1 over the concentration at time t is equal to 1 over the initial concentration plus the rate constant, the second order rate constant times time. Now what these formulas tell us is in both cases we can make plots to tell whether or not the reaction is first or second order because here if I define y as the log of concentration at time t and log of concentration at time 0 is equal to the intercept, the y intercept then this tells me that I should have, so if I define x as t, m now is minus the rate constant, the slope is minus the rate constant, okay? So if I plot log of concentration versus time I should get a straight line whose intercept is log of the initial concentration and whose slope is equal to minus the rate constant. For the second order case I plot 1 over the concentration and then I should get a line whose slope is equal to the rate constant and whose y intercept is equal to 1 over the initial concentration, okay? So what I want to do next then is manipulate those data either by taking the log of the concentration or the reciprocal of the concentration and we'll plot them and we'll see if one of them gives us a line in which case we can conclude what the order of the reaction is and then probably maybe, yeah, next time we'll see how we can determine the rate constant by fitting the data, all right? So this next exercise is to learn how to take a matrix of data that we've entered and then manipulate it. In this case we're going to either take the log of the concentration or the reciprocal. All right, now the way we're going to do this, so now we have the matrix data, all right? Here it is. Concentration is a function of time. What I'm going to do is I'm going to copy that matrix into a new matrix and then in the second column I'm going to put the log of the concentration in place of the concentration itself, okay? So now I'm going to make a new matrix called new data and I'm just going to call that data, all right? That's just going to duplicate it. Now I'm going to type in something and then I'll explain to you what it does, okay? So what I'm going to do is say new data, bracket, bracket, all comma 2, bracket, bracket equals log, bracket, data, bracket, bracket, all comma 2. Now I'm going to go ahead and put a semicolon and then we'll just look at new data in table form, okay? We're missing a bracket here, all right? Now what happened? So this thing inside the double brackets, this is a way to refer to all rows second column, all right? So what I'm basically saying is take all rows in the second column of data, take the log of each one of those and put them into the corresponding all rows second column of new data, all right? So now what I have here, it's not the concentration anymore, it's the log of the concentration. So this is kind of a nifty way to do an operation on, in this case, the second row, I mean second column, that's why the comma 2, all in one shot. And now I can simply plot new data and see if it's a straight line. If it's not then I would conclude this reaction is not first order, okay? So let's do that. So we can say list plot, new data. And you can kind of tell by eye whether or not that's a straight line. Does anybody in here think that looks like a nice straight line? Not really, all right? So what do we conclude? That these data that we entered are not consistent with the first order integrated rate law. All right, so now let's check second order. So what do we need to do for second order? We need to create instead of the log of the concentration of the reciprocal. And if we plot that versus time and it's a straight line, then we conclude that the second order rate law is consistent with these data, all right? And we can do it more or less the way we did here. So I'm just going to mouse all this in, put it down here. And now instead of taking log, all I have to do is say 1 over. I don't need these brackets because I don't have a function here, okay? So now we've replaced the concentration by the reciprocal of the concentration, okay? And now let's go ahead and plot that, okay? Now, does that look like a straight line? It looks like a pretty good straight line to me. All right, so then we would conclude, in fact, that these data are consistent with the second order integrated rate law. And like I say, shortly, not yet, but pretty soon, we'll figure out how to actually fit the data to a straight line and get out the rate constant, which is going to be equal to the slope, okay? So any questions on entering matrices, doing simple arithmetic operations on them? And plotting them using list plot, okay, good. All right, now the next topic is going to be fitting, okay? And so I'm going to explain to you what it is that we're going to do. How many people have ever done least squares fitting? Anyone? No? Okay, good. Then you'll learn something useful. All right, okay, so what I'm going to suppose that I have is a series of points, okay? So each of these is just a set of points in the XY plane and I could be anywhere from 1 to the length of my data, all right? And the objective of a fit would be to see if I can find a function, an analytical function that traces these data, in other words, that goes through the points very closely, hopefully, okay? And so this, all right, this is the data and the fitting function I'm going to call, I'll just call it half of X, okay? And so the idea behind least squares fitting is that it optimizes the parameters of the function F of X, so it's going to have some parameters in it, I'll explain that in a second, such that it fits the data as well as possible. And so now I'm going to tell you what is as well as possible, okay? So there's a quantity, which we denote chi squared and what this is is going to be a sum over all the data points of the absolute value of a quantity known as the residual, okay? And the residual is just the difference between our function F of X evaluated at XI minus the actual value of Y sub I, okay? So this is a measure for each point of how far my fitted function deviates from the actual data, okay? And this is the sum of that quantity squared over all the data points. And the idea behind least squares fitting, as the name suggests, we minimize chi squared with respect to the parameters, okay? Now there are different types of least squares fitting. The simplest one is called linear least squares fitting. And the nice thing about linear least squares fitting is that you are guaranteed to get the best possible answer. It's an analytically solvable problem, okay? So you choose a function F of X. You perform a linear least squares fit and you're guaranteed that the parameters that come out are the best possible parameters for that function that you chose. There's another version which we'll talk about later in the course, which is called nonlinear least squares fitting, and that one you're not necessarily guaranteed to get the best answer, so you have to be a little more careful with that. But I want to give you an introduction to fitting by showing you the simplest version, which is already very, very useful, as I hope you'll see, okay? So what do I mean when I say linear? This means linear with respect to the parameters, okay? So for example, if I have F of X equals MX plus B, so that's just our formula for a straight line, this function is both linear and X, and it's also linear in the parameter M and in the parameter B. If I had F of X equals AX plus BX squared, is that linear with respect to the parameters? Yes. It's not linear with respect to X, okay? It's quadratic with respect to X, but with respect to A and B, the parameters A and B, it is linear. So this is linear and X and A and B. So I can use both of these functions as trial functions to do a least squares fit. What about log or A log BX? Can I do a linear least squares fit with that function if the parameters are A and B? The answer is no, okay? So this is linear in A, but not in B. B is inside the log as written, but you can actually expand the log and make it into a linear function of parameters. You see how to do that? So I can define this as some other parameter C, and now I have a linear function, okay? Okay, one more example. Suppose F of X is equal to A cosine BX, same story. Linear in A, but not in B. And that one, I can't use the same trick of rearranging it to turn it into a linear function of parameters, okay? So the next thing we'll do then is to see a simple example of how to do linear least squares fitting in Mathematica and then we'll do a couple of more examples next time. Any questions on this? I know from experience that what is a linear function in parameters is not always obvious to you when you first see this, yeah. We're doing this because this is something that is done routinely in experimental science. So for example, for this problem, suppose now I want the rate constant. What I'm going to do is I'm going to fit these data, we won't do this right now, we'll do it next time, fit these data to a line, and then the rate constant's the slope, okay? But I want the best possible line that goes through those data if there's a little bit of noise going point by point it's going to give me a different answer at each point. I want to consider all the data at once, fit it to a line and extract out a meaningful parameter, in this case the rate constant, okay? And this is done all the time. And we'll see other examples of what you can do with it, okay? So then let's go ahead and see how it works, all right. So the first thing I'm going to do is I'm going to define a function that is a formula for a straight line, okay? So I'm going to define y of x underscore colon equals 2 times x plus 3, all right? And now I'm going to make a table of values of this function, okay? So I'm going to say my data equals table and in the first column I'm going to put x and in the second column I'm going to put y of x and I'm going to evaluate this at a series of discrete points, x goes from minus 1 to 5, okay? All right, so if I do that I get a matrix of data, okay? Now I want to fit this and so now I'm going to explain to you, sorry I keep going back and forth, how it is that we specify the functional form in Mathematica for the linear least squares fit. So I want to fit this function to the formula, a generic formula for a straight line, all right? So I'm going to have y of x is equal to b plus mx, okay? So in order to specify this function in Mathematica I need to specify for each term in my function what's the dependence on x, okay? The first term doesn't depend on x, all right? So it's proportional to x to the zero power or 1 and the second term is proportional to x, okay? So that term I'm going to specify by just putting in an x and if I had a third term that was like, you know, plus a x cubed I would add an additional term x cubed here but we don't have that for this one. We'll see that a little bit later, okay? So we have to specify these two terms and I'll show you how to do that now. So the command to do a simple linear least squares fit in Mathematica is just called fit, okay? So what I'm going to say is fit and you say, whoops, it's a bracket, excuse me, say what you want to fit so it's my data and now I put in the functional form and I do that by listing in curly braces the terms in my function so I have a term proportional to 1 and a term proportional to x, all right? And then I have to say what is the variable and here it's x, okay? So this here is how you specify the formula for a simple straight line in Mathematica and if there were additional terms that had other dependencies on x I would just list those here. Yeah? Mm-hmm. And we're looking at the equation above. Mm-hmm. It doesn't matter if you do that. The equation doesn't matter. It won't matter, it doesn't matter, no, no. It doesn't, he's asking if I wrote the function like this do I have to put the terms in the same order here and the answer is no. Mathematica is smart and can keep track of the terms and match them up appropriately. But in this case this is an artificial example so all I want to show you with this example is I've created exactly data that corresponds to this formula so when I do the fit I should get exactly this back, all right? We'll add some noise to it in just a second. This is just to show you that it works, okay? All right, so if we enter we get a formula and this formula is something we can actually use to do calculations now if I set this equal to something, all right? So this is a trivial example where I got back the answer I put in because my function corresponded perfectly to the 2x plus 3 form and so I get exactly that back, all right? So now I'm going to finish up just the last couple of minutes by adding some noise to this data and then we'll refit it, okay? And you'll see we won't get exactly the same answer back. We will get the best possible answer but it won't be exact anymore because there will be uncertainty in the data. Okay, so now what I'm going to do is I'm going to take the same function and I'm going to add some random numbers to it, okay? So here's how I do that. I'm going to say plus outside the bracket plus random, real and then bracket and curl is minus 1 to 1, okay? Now what does this do? What this is going to do is it's going to generate random numbers on the interval minus 1 to 1 and it's just going to add them to my function y, okay? So there you have it and now if you just do a list plot of my data what you see is it looks more or less like a straight line but notice it's got some jiggles in it and that's because I added random numbers in, okay? So this is the same line as before except now it has some random noise in it, okay? And now what we're going to do is we're going to fit that with exactly the same command and now you see I don't get exactly the same answer as this, okay? So my y intercept is actually 2.45 now and my slope is deviating slightly from the slope of 2, okay? But this is the equation for the best line that goes through those data. And now I'm going to show you quickly and we'll do it again next time because this is something that I ask you to do in the homework and I'll tell you how to do that next time. I'm going to show you how we can plot both of these, both the actual data and the line on the same plot so you can see how good or bad is to fit, okay? So first thing I'm going to do is I'm going to set this equal to a variable, data plot, okay? Reenter it. So data plot now contains the plot of my data and now what I'm going to do is I'm going to create another plot called fit plot. Well, first I have to store my fit, okay? So I'm going to call this fit, fit data equals, enter it and then fit plot is going to be equal to plot fit data. So fit data here is just this formula and it's a function or you know it's an assignment that I actually use with the plot command because it's not a series of data, it's a continuous function, all right? And now I'll plot it over the same range as my data which is x going from minus 1 to 5, okay? And if I enter that I get a line and now I can show you how to put them both on the same plot, there's a command called show and now I can say simultaneously plot on the same plot, my data plot and my fit plot and if you do that now you see your best fit line plotted on top of your data and you can see that in fact this line generated by a linear least squares fit goes through the data points rather well and later in the course we'll learn how to actually get and interpret some diagnostics to tell you just how good the plot is but for now we'll consider eyeballing it as a good judge of the goodness of fit, okay? So that's it for today, we'll continue with linear least squares fitting next time. So see you tomorrow.