Transcript for:
Module 8 Video - Polyfit: Exponential Trendlines

To round out our coverage of generating trend lines and trend line equations in MATLAB, we will consider an exponential model, and specifically, this will be a model of the decay of the brightness of a phosphorescent strontium aluminate compound over time. Following our housekeeping commands, we will define a set of data First, we have t, which will be a set of points in time when measurements of the brightness were made, and these times are measured in hours. And then we will have a set of measurements called LR, which is relative brightness. It's the current brightness divided by the initial brightness. Therefore at time zero, the initial brightness divided by the initial brightness is simply 1. So it starts on one and decreases from that point. Next, we plot the experimental data. You should be familiar with this at this point. So I'm not going to spend a lot of time on that. I'll click run and there are our markers. Now, I did pause after plotting those markers so we could continue to talk about what's coming next. Now, note that this is a very, very bare bones graph. There are no grid lines. There are no labels. There are no rules. The numerical values on the axes are fairly small, so let's fix that up first. As we mentioned in a previous video, we're going to grab a handle to the axes, and I'll call that AH, and then we simply say AH dot font size equals 18. Here we go. I'll advance to the next pause and note... again as we saw in an earlier video that when I resized the font it also changed the spacing of the grid line so that it would not be too cluttered next let's put an X label on here this will be time and there is our time T and hours here I used the older method of specifying the font size by simply including the property name property value pair inside the parenthesis with x label and that's perfectly acceptable for y label however i'm going to do it by a new method now we have not yet seen this i alluded to it in an earlier video but in order to use the dot notation to change properties of text objects we first of all have to grab a text object you And in order to do that, when I say y label, instead of just saying y label, I say some variable equals y label and then the rest. Now, what that does is that puts what is called a text object into yl. I can use that text object with the dot notation to change various properties of that text. Now, I put a pause after y label so we could see what it looked like initially. Here's the Y label command, and there is our relative brightness. Now note, that's larger than the X label. But you're saying, but wait a minute, you said earlier that the default size is a lot smaller. Well, that's true. However, when I reset the axis font size, it not only changed the numerical values font size, it also changed all of the labels on the axes, including the X label and the Y label and the title to whatever I changed that to. So now the Y label is size 18 because I have not changed that. When I did X label, I specified 16. So it's a little bit smaller. So I need to match those up. So now I'll use YL dot font size equals 16. And there is our smaller font size. Now, it's important to remember that when you set the font size on the axes, it does more than just change the numerical values. So watch out on that. Next we're going to do the title. Now note I'm going to use the same sort of scheme here. I'm going to say t equals title, which puts a text object referring to the title into the variable capital T. And then I'm going to say t dot font size equals 16. So here we go. Now, wait a minute. A couple of things. First of all, that's a lot darker than the others, and that's because the default for titles is bold. Now, you can change that, but if you don't change it, the default for the title is going to be bold. That is not true for the X label and Y label. Also note that this came out on two lines. That can be a handy trick. to know sometimes in the way you do that is that instead of simply putting a text string containing the title you put a cell array containing two or more text strings separated by semicolons note that I've got open curly brace to start my cell array then I've got phosphorescent decay and then a semicolon and then I've got strontium illuminate compound 17 B And that's the second one. Now, when you do this, each item in that cell array will appear on a separate line. So that will allow you to do multiple lines in a title or, for that matter, in one of your other axis labels. By the way, you may say, well, strontium aluminate, isn't that just the same thing? How can you have different compounds? Well, strontium aluminate is a generic term for... a variety of compounds that contain strontium aluminum and oxygen and the relative amounts of those three elements within that compound may differ also to make the compound fluoresce you need to add what is called a dopant which is another element or compound such things as europium or dysprosium There are a variety of things that can be used, and so this compound 17b is a specific mixture of strontium, aluminum, and oxygen doped with some specific other elements. Now, the next thing, we've got to calculate the model parameters for an exponential model. Now, this should be familiar to you by now. We have some variable, which I called exparam. and you use polyfit you send it the independent variable T you send the natural log remember in MATLAB that's LOG of the dependent variable LR and then of course a 1 and then you have to extract M from the first element and you get B by raising e to the second element of that variable next you have to create a theoretical data set this also should be reviewed so I'm going to say T Theo for theoretical equals linear space now I'm going to start that on the time value for the very first element I'm going to end it at the last element in T so that it begins and ends at the same points that the experimental data does now note to get the last element I use the length of T now this does assume that the first element is the first one in the sequence of measurements and the last one is the last time a measurement was made. If they're not in the proper order, that would not work that way. You could use max and min instead if they weren't ordered properly. And then I simply send that sequence of theoretical t values, which are equally spaced. Now, they should be 100 because I did not override the default of 100 samples that Lenspace creates. So I send the t-theo to my model B. times e raised to the m times t and i put that in lr theo now i want to plot this on the same graph so i have to use hold on then i do the plot and there's my plot you always need to look at your trend line and make sure that it actually looks like it makes some sort of sense and sure this looks like it makes pretty good sense relative to those data points it is a fairly good fit you Next, we've got to add our trendline equation to the graph. Again, this should be review. It's just the details of the exponential model that are going to be a little bit different. We'll use sprintf in order to assemble that trendline equation. And we've got L divided by L underscore zero. Now, remember, the underscore says to subscript that zero. So the zero will come out subscripted. It equals. and then I've got an insertion point for my value of b with three decimal places, and then I've got e raised. Note here that after the carrot character, I've got an open curly brace, and then I've got an insertion point, and then a t, and then a closed curly brace. The purpose of the curly braces is to say everything inside the curly braces is superscripted. If I did not have the curly braces, it would only superscript the first numeral in the number inserted at that point, and then the rest of the number would not be superscripted. The T would not be superscripted. So I have to enclose all of that in curly braces to get the entire thing superscripted. And then, of course, I've got B to insert at the first insertion point, M to insert at the insertion point inside the curly braces. And And then I simply put that on the graph using the text function. That should be familiar to you at that point. And so let's get all of that done. Here we go. Oh my goodness, that's little tiny. We got lots of problems. It's too small to read. It probably really should be in red. It needs to have a background to obscure the grid lines. We might want to put a boundary around it. And so let's take care of all of those details. Again, note that when I did the. text function, I did create a text object by saying tx equals text, and that created a text object in tx for that trend line equation. And now I can change the various properties. This is one of the places where using the dot notation instead of the property name, property value pairs inside the parenthesis of the function can be really useful because I need to change four things, the font size, the color. the background color, and the edge color. If I tried to put the property name, property value pairs of all four inside the parenthesis, that line containing text would get fairly long, and that can be a little bit cumbersome. So this has a real advantage over that other notation. So we've got tx.fontsize is 16, and then the color is red, the background color is white. The edge color is red. I'm going to change all four of them at once. Here we go. And there we have it. L divided by L sub zero equals 0.946E to the minus 0.652T. And do note the minus 0.652T is all superscripted. All right. That winds up our three demonstrations of the three different trend line types. And next, we'll move on to something else.