So the following is an introduction to using Python arrays and also plotting in order to begin. We need to use the Numpy library as most of the functions that we use with erase will be from the Numpy Library to use this. We type import numpy as NP, and then all the functions will be NP dot whatever function we have. Python has some built-in arrays, but we’re just gonna use numpy and be done with it since they’re much more flexible and powerful in numpy than in regular pipeline. There’s a number of good links here that you can click on to find more information and tutorials and whatnot. We create arrays and numpy by typing X equals, for example, X equals NP array and then the array array is a function and we give it a list type inside the parentheses and to indicate an array raised are going to go inside square brackets. So 1 comma 2 comma 3 comma 4 So it’s a comma separated list of elements between square bars, square brackets. You can think of an array is a collection of variables or a vector so in math were familiar with this concept of a scalar, which is a single number. We can also have vectors, which are collections of numbers, and in this case, we would have a four-dimensional vector or we call this a an array with four elements in it. This is convenient. If we have large sets of data and we want to treat them as single objects and this would be analogous to a column of data in excel, where we have a number of values in each cell, we can make two-dimensional arrays. A two-dimensional array is like a 1d array, except each element is itself an array so here you can see the outer square brackets and inside the first element is an array so bracket 1 comma, 2 and bracket, and then the second element is another Brett as another array bracket 3 comma 4 And when we print it, we get this two-dimensional array. We can create an array using the a range command, which is the analog of the range function we type. Np a range 3 comma 20 1 comma 3 This behaves just like the range function and that this means we’ll go from 3 up to 20 in steps of 3 we can create a two-dimensional array by using a 1 by reshaping a 1 dimensional array. So in the above Example & Peeta, a range, we can the result of this command and Peeta a range is an array and so we can operate on that array by typing array don’t reshape 3 comma 2 We can create an array of zeros that are of size 6 by simply typing in P dot zeros. This is great for initializing Now. You can also use NP empty. It’s supposedly faster to create because it doesn’t have to initialize the values to anything you can see. This just creates an array of zeros. How you can make an array of ones like array not. Np Dot one’s six. That’ll create an array of six months and that’s useful If you want to initialize an array. I’ll have all the same values. So if you type X equals 5.0 X ones and P dot one’s six, it’ll create an array of six on once, each of which have the value with each element, having the value five. Another great function is linspace. One space will go from two to eight inclusive with five points, so you’ll have five evenly spaced points between the numbers of two and eight, and in this case, both two and eight are included in the list. These don’t have to be integers. Well, the number that you use would be an integer, but the starting and ending values don’t need to be, so we use this all the time, So for instance, if we want to create a list of X between 0 and some value would use one space, for instance, just linear spacing values that increased in time. Okay, so that’s how to create arrays several different functions that we’ve shown and there’s others available, but these are the main ones that we’ll use we can access elements of an array using square brackets with an index number array indexing always starts zero, So if I have an array, X equals NP a range 3 21 3 then the value of X is shown on the right and the indices that correspond to those values are 0 1 2 3 4 and 5 If we want to access element with Index 2 we type X Square Bracket 2 and that will return 9 which is the value corresponding to in debt – again, starting at zero. If we have a two-dimensional array in this case, X is a 2d as a matrix 0 1 2 3 we access it again with brackets with commas, so this would be like X element. This would be Row 1 column 0 so we would get this Row Row. Row 1 Column 0 gives us value 2 so this has the same notation that we used in linear algebra. A IJ where I is the row and J is the column here. We have an example of how to access arrays using array, slicing as it’s called. Suppose we have the array. X that we used above. If we print X with brackets colon, the colon means everything, so it’s gonna access everything in X. We could also just type print X without the bracket colon, but the colon means everything if we do to colon that will grab Index 2 up to the end of the array, so it’ll get everything from element 2 to the end. If we go to Colon 4 that will get indices 2 & 3 Skipping 4 So that behavior is similar to what we’ve seen with the range function. We can also go backwards from the end, so X minus 1 will give us. The last element in the array, minus 2 will give us the second to the last element in the array, etc, So you can count backwards using these negative indices, and we can also use an array of indices to access which elements were interested in so in this case. I want X and then bracket. And then I have an array, which is 1 comma 2 comma 5 This will tell me get the values in indices 1 2 and 5 and you can see. We get sick 9 and 18 so 1 2 & 5 give us 6 9 and 18 and that’s what’s out, but so this can be especially handy if we want to access particular values in the array, suppose we have an array of positions like X, and we want to find the array of differences between consecutive points So here I have an array called X, and it varies from Point 1 to 7 There’s 10 10 elements in the array and I want to find the difference in positions between subsequent elements. So I want to know. The first element in the DX Array would be 0.2 minus 0.1 and the second AA would be point 4 minus point two in the third 0.8 minus point four, etc. One convenient way to do. This is to type. DX equals x1 Comp 1 : minus x0 : minus 1 So this will grab one to the end will make an array out of all of these points, starting at this value point two and then we’re going to subtract from that all the points from 0 up to, but not including the last so that will go from Point 1 up to this one. And when we line those two up and subtract them, we get and store it in Array called DX. Then we have the array of Delta Xs so rather than having to go through and make a for loop where we evaluate the difference. Each one at a time in this case, we can just do a simple one-line or using array, slicing to say, take the difference between these points and these points and, of course, DX will have 9 elements instead of 10 because there’s only 9 differences between 10 points another way that we could do. This is simply assign the indices of interest to a variable. So here I have an array of indices, there go from 0 to 9 and an array of indices that go from 1 to 10 and then I can write. DX is equal to X of the I. +1 indices minus the array of indices X. I and this will do the same thing as above, which can also be convenient In both cases, we get DX shown in the same values or output when we do this approach and this approach, we can find the shape of an array using MPI shape the shape function for a 1d array. The shape function is a tuple of values in this case. It’s just 10 because it’s a 1d array. We can also ask for the size of 1d array and thatll. Tell us how many elements are there the? Len function is also available. Le N. And that will tell us that the size is 10 so this is very convenient. If we have a two dimensional array, we can have. We can use the shape function and notice that the shape of the two dimensional array is the number of rows and the number of columns so 3 by 2 the size function, though in a 2d array, doesn’t give us the length. It gives us the total number of elements, so we get 6 instead of 3 or 2 but the shape function will return 3 and a 2 and this is like an array. It’s called a tuple and you can access either element 0 or only 1 to figure out how many rows or how many columns there are so and PJP, this part returns a tuple and then the 0 means give me the first element, and the 1 means give me the second element, so I can recover 3 or 2 operations are done on entire arrays, which is very convenient, so I can just type. If FX is an array I can square the whole array element wise and I get X squared will square every element. So 3 becomes 9 36 81 144 etc, as shown here, so this app is very convenient, much more convenient than writing a nested for loop for each row in each column if we multiply two arrays together, so Y is minus two times, the X array, then we can multiply these things together and again it multiplies the known element wiesen, which is very convenient, much faster than the corresponding code in languages like C++, for example, we can take the dot product of two vectors using the NP dot dot function, so if X and Y are two different arrays and P dot dot will take the dot product, this is equivalent to typing in P dot sum of X Times Y so X times y we multiply a and then Samos on all the elements up and the output is a scalar as expected. We can use ax equals. B systems to solve or we can use these arrays to solve linear systems of equations. So if we set an array a which is a 2d matrix and an array B, then we can solve solve X in the ax equals B system using V and P dot Lin alg. Dot solve function and we just simply pass it a and B in and outputs the array X. And if we like we can test to see that we recover B when we type the dot product of matrix a and vector X and we recover when we do that where you recover B, which was our B array, it’s very important with arrays that you’re very careful about assignment Pythons a little different than many languages in this way, for example, if we have X equals an array 1 2 3 4 and we type y equals X. You might think that we have a completely separate array called. Y that also has the same values as were in X, but we can see in the following code that if we change. One of the elements of Y Y Index 3 is 44 and then print X and print Y both of the arrays changed. Not just Y so. X is 1 2 3 44 And so is why, even though only changed why, and this is because when we assign Y equals X were just giving the underlying object. This array 1 2 3 4 another name, and when we go Y equals 3 were changing the same object that both Y and X point to this is often not what we want to have happen, so in order to avoid that we would do y equal’s X copy, and that would make a completely separate object for Y and X, and then when we tape Y equals Y 3 is 44 and print X and Y we can see that. X is not changed, but Y is, which is usually what we want. There are tons of other functions available. You can see the links at the top of the document. There’s rep. Matt, which will make new arrays out of existing existing one’s. Repeat Matrix. There’s H stack and V stack, which are very convenient for combining arrays together, there’s sort. I died! AG transpose, etc, Many other functions that you can browse through that are available so plotting in Matlab. This is a little different in order to plot in Matlab. We need to import the relevant libraries again and this library that we import is called Matplotlib pipe lot and we import it as PLT, so this will allow us to use all the plotting functions as Plt Dot function name, and then if we’re using the Jupiter notebook, this this line % matplotlib in line is also needed. That’s just a way to make our plots show up, so this little bit of boilerplate here. Suppose we have a function array X and the corresponding array Y a very simple plot is to type PLT dot plot X comma Y, and it outputs a an XY plot very simple. Now we normally want to format this up a little bit. We’d like to be able to change the font sizes. We’d like to be able to add some lead some axis labels and maybe a legend and we can do that as follows and maybe change the line style. Plt dot plot XY. And then this string k o -. This is the line style. The K means black. The O means symbol and the dash means a solid line. So you if you just wanted symbols, you believe off the dash. If you want a different color, you’d change. K to something else. And if you wanted a different symbol than circles, you’d use a different character here. Other colors are BR GCM for blue, red, Green Cyan and Magenta, and there’s other line styles, a dashed line, a dashed dotted line and a dotted line, The columns, a dotted line and other symbols, a dot, a square, a triangle, a downward triangle, a star diamonds and other values. Xs and pluses and left triangles, right triangles and other kinds of shapes that we can put in. We can give it a label with Plt X label and then a string, telling us the value P LTI label and a string, telling us the value and a title Plt Dot title some title and this is what gets output. So you can kind of format here. If chart is wanted, you can draw multiple curves on the same plot and do some other things. If you want multiple curves on the same plot, you simply call this plot command more than once. And in this case, we can add the optional. Argument label equals to tell it legend for what we want to call the curve. So in this case, we can change the y scale to be log. Plt that y scale is log so that it will be on the log scale. We can have curve 1 + / – defined with the label. Here we have a black dashed line and a blue, solid line and those show up. We can even put Greek symbols into the X label using latex style code. This R here is an important keyword, so our quote with symbols and then this dollar sign Alpha underscore S / beta up s and you can see what the result of that is. Well, we can even change the font size that we use this top command. Plt to our C font. Size equals 16 tells us that we want everything to have a font size of 16 so you’ll notice some of these fonts that are pretty small. This will make all the fonts 16 and then in a couple of places here. I’ve changed the 16 to be 20 so they’re even a little bigger. These two legends to tell it that we want to include the legend on the plot we type. Plt Dot legend and legend can take a couple of options like where you want to put the legend and whether or not you want to draw a frame around P this little box around the legend. You can get an axis handle If we type. X equals. P Ltd, CA GCA stands for get current axis and then once we have an axis handle, we can add other functions like X dot set X limits X set while in and these are an array, telling us what the limits both in X&Y should be. So you can see this isn’t quite as straightforward as using something like Excel where you just double click and tell it what values you want. The power, however, is that you have full control over this and can make as many plots as you want using once you setup the code and I can have these done automatically, which can save a lot of time. Not really saved a ton of time. If you if you’re only doing one plot, but if you’re doing tons of plots, that might be that might be a good thing to do and most of this syntax here follows that of MATLAB. That’s why the the library is called matplotlib. It’s because it’s patterned after the kind of syntax that’s used that’s widely used in Matlab. We can make a table of plots using these sub plots command. In this case, we use the command. Plt Dot subplot, the 2 comma 2 comma 1 says set up a two two row by two column table of plots and make the first one the active plot. So this indexing doesn’t start at 0 starts at one consistent with Matt Plot or with Matlab. So this is plot one, two, three four, so right now plot one is the active plot and so any commands that follow happen on this plot. Then we go over to the next plot subplot. Two, two two now. This is the active long this final two and then any commands apply to this plot here. I’ve added an X label and then two to three and two to four and then occasionally the output will not look good because these labels will overlap with the plot. So if you do the PLT, that type underscore layout kind of makes the plots a little more pretty, you can customize any and all things relating to these plots where they’re placed. How big they are, etc and matplotlib is a very large library of tons and tons of customization options. Finally, we can make 3d contour plots, so it’s a 2d plot. But the coloring represents a third dimension and the following code. Here illustrates how to do that so here, basically. I created an array of X’s an array of Ys, and then I turn those into matrices of Xs and YS, so I get the X value at every single point in the grid, the Y value at every single point in the grid and that’s done using this handy function called mesh grid, and then I can find the Z as a function of X and Y at every point by using in this case, this handy function that makes a pretty plot. And then I can in this case. I’ve made a subplot. That’s one row by two columns, and I’ve made a contour plot X y&z with XX contour levels shown, and then I have a filled contour plot so instead of contour, its contour F X y&z again with XX. And then I can put a color bar on there.