From the course: Accelerated MATLAB

Advanced 2D plotting: Parabolas - MATLAB Tutorial

From the course: Accelerated MATLAB

Advanced 2D plotting: Parabolas

- [Instructor] While 2D and 3D plotting has been covered in the previous MATLAB course. In this chapter, we will go over some more advanced features and properties we can use to modify our figures or plots of a given data set. Let's start with a new live script. And we are going to assume that we will plot a parabola, which is given by the equation y equals x squared minus 4x plus 3. So let's type first x, which will be determined by using the linspace function, and we will start from minus 10 to 10 with a sampling of 500. Next, we are going to write the equation. So y will be equal to x squared minus 4 multiplied by x plus 3. And then we are going to plot x,y. So let's run this code. And as we can see, nothing new so far. Now let us use the Section Break tool, which is located under Live Editor and Section panel. This will keep our code in sections, which will help us notice the changes that we are going to make to our plot. So in the new section, we are going to add a title, axis labels, grid, and remove the outer box. For the sake of time, I am going to copy and paste this ready part of the code here. And as you can see here, we have the title, which is Parabola, and gives us the equation y equals x squared minus 4x plus 3. We have the x and y labels. And then we are going to put the grid on. And also we are going to remove the box from the surrounding plot. So let's run this code again. And now we can see that we have two plots. The first one, which does not have all the information that we added in the second one, and the second one with all the information that contains the Parabola title, the x and y-axis, and also the grid on. And as you can see, the surrounding box has been removed in the second one. So now let's customize our plot by adjusting the axis properties. And we can do that by using the GCA function handle, which is get the current axis. So I am going to create another section break here. And in the new one, I'm going to paste a code that I have ready here with the comments as well, and this will be provided to you in the exercise files. So in here, and as you can see, you can use the slider at the bottom. We have axis equals to GCA, which is the get the current axis. Again, this is the function handle used in MATLAB to get the axis that are current in our drawing. And then we identify the limits for the axis. So the x limits will be from minus five to five, the y will be from minus 10 to 10, and then the axis for the x and y-axis location will be the origin. So if we run this code again, then we can see that now the origin is at 0,0, so that's our origin, compared to the first one that starts from minus 10 for the x-axis and 0 for the y-axis. And also we have the limits again for x and y-axis from minus 5 to 5 and from minus 10 to 10. Say we want to highlight a few points on the plot, we will reduce the number and use the stem function. So we are going to create another section break here and we are going to type x_reduced will be equal to minus 5 and every 1 to 5. And then also we can use the y_reduced will be equal to x_reduced. So we're going to rewrite the equation here, minus 4 multiplied now by the new x, which is x_reduced, plus 3. And we are going to type 'hold on' here. Now we will type the stem function with the reduced values of x and y. So x_reduced, y_reduced, and then we are going to use the red color here and then hold off. So let's run this code again. So we can see how with the x_reduced and y_reduced and the stem function, we can put these values of x and y on the parabola line. Now that we saw how to plot a simple parabola and use different functions to modify a plot, next let's see how we can apply this to plotting a current signal that contains harmonics.

Contents