Presentation is loading. Please wait.

Presentation is loading. Please wait.

MatLab – 2D Plots 2 MATLAB has many built-in functions and commands to create various types of plots. Instructor notes: We start with an example of some.

Similar presentations


Presentation on theme: "MatLab – 2D Plots 2 MATLAB has many built-in functions and commands to create various types of plots. Instructor notes: We start with an example of some."— Presentation transcript:

1 MatLab – 2D Plots 2 MATLAB has many built-in functions and commands to create various types of plots. Instructor notes: We start with an example of some of the plots Matlab is capable of. 2D plot on the left, polar coordinate plot in the middle and 3D plot on the right with colored shading and mesh lines to better display the surface. 2D plot polar plot 3D plot MATLAB - 5

2 Two Dimensional Plots in MATLAB
Topics Covered: Cautions when using different plot functions fplot polar plot histograms subplot Instructor notes: This presentation introduces 2D plotting in Matlab. It focuses on the plot() command for creating simple plots. Plotting is important because often times we can learn a lot more from a visual representation of data than we can from just looking at raw numbers. It also covers formatting of plots such as adding a title or axis label or legend, etc. Instructional Objectives: Use plot() command in MATLAB to create 2-D plots Use formatting commands in MATLAB to create complete well-formatted 2-D plots Use loglog(), semilogx() and semilogy() commands in MATLAB to create log plots MATLAB - 5

3 fplot(‘function’,limits)
THE fplot COMMAND The fplot command can be used to plot a function with the form: y = f(x) fplot(‘function’,limits) The ‘function’ line can only contain one variable name, e.g. a=2;b=-1;c=3; fplot(‘ax^2+bx+c’, [-2,2]) will give an error message as a, b, and c will be considered variables The function fplot produces a smooth plot of a function for a linear plot. Using fplot and then converting one or both axes to log axes will in general not produce a smooth plot, even when the plotted function should be smooth. Instructor notes: For plotting mathematical functions, in addition to the plot () command, we also have the fplot () command. This command is used specifically for plotting functions, hence the “f” at the beginning of the name. The function to be plotted is supplied to fplot () as a string in single quotes. The domain of the function is supplied as a vector that includes mininum and maximum x values. Additionally, limits can be placed on the minimum and maximum y values of the plot, too. It’s important to note that the function being passed to fplot () can contain only one variable because Matlab expects the variable that it sees in the function to be the independent variable. You cannot define a variable to have a specific value prior to the fplot () command and use that variable in the function. Matlab will not be able to distinguish which one represents the domain variable and which one has the constant value since Matlab will have to parse the string and form of the function and at the point of parsing the function is just a string.

4 polar(theta,r) THE polar COMMAND
The polar command can be used to plot a function with coordinates r and Ɵ. polar(theta,r) Theta and r should be equal length arrays, just as with the plot function. Theta must be in radians. The order of the variables is the opposite to the normal math order. Hold on, hold off must be used to place multiple curves on the same graph. The size of the graph (r coordinate scale) is set by the first curve plotted – so plot the largest curve first

5 Histograms The hist command can be used to create a histogram, but produces a graph with connected columns which can look bad when printed in b/w. The graph can be improved by using hist to organize data and then feeding the results to the bar function which produces nicer results, e.g. Step 1: Given an array y, [n, xout] = hist(y,nbins); will organize the values y into nbins equally spaced bins. The variable n is an array containing the number of occurrences in each bin and xout is an array containing the center location for each bin. Step 2: bar(xout,n) will create a vertical bar chart of the data. The columns will be separated and look much better in b/w. Instructor notes: For plotting mathematical functions, in addition to the plot () command, we also have the fplot () command. This command is used specifically for plotting functions, hence the “f” at the beginning of the name. The function to be plotted is supplied to fplot () as a string in single quotes. The domain of the function is supplied as a vector that includes mininum and maximum x values. Additionally, limits can be placed on the minimum and maximum y values of the plot, too. It’s important to note that the function being passed to fplot () can contain only one variable because Matlab expects the variable that it sees in the function to be the independent variable. You cannot define a variable to have a specific value prior to the fplot () command and use that variable in the function. Matlab will not be able to distinguish which one represents the domain variable and which one has the constant value since Matlab will have to parse the string and form of the function and at the point of parsing the function is just a string.

6 subplot(n,m,locn) THE subplot COMMAND
The subplot command can be used to organize a number of graphs in the same figure window subplot(n,m,locn) Subplot only indicates a location within the figure window to place a graph. The graph should be created following the subplot command. Any valid MATLAB plotting command may be used, e.g. plot, polar, semilogx, fplot, bar, stem, etc. Not all locations in the array of graphs need to be used. Within a figure window, the same values of n and m must be used for all subplot commands. Unfortunately subplot cannot be used as a sophisticated page layout tool. For example, you cannot place two figures at the top of a figure window with subplot(2,2,1) and subplot(2,2,2) and then place one figure at the bottom with subplot(2,1,2). Maybe someday this will work … Instructor notes: For plotting mathematical functions, in addition to the plot () command, we also have the fplot () command. This command is used specifically for plotting functions, hence the “f” at the beginning of the name. The function to be plotted is supplied to fplot () as a string in single quotes. The domain of the function is supplied as a vector that includes mininum and maximum x values. Additionally, limits can be placed on the minimum and maximum y values of the plot, too. It’s important to note that the function being passed to fplot () can contain only one variable because Matlab expects the variable that it sees in the function to be the independent variable. You cannot define a variable to have a specific value prior to the fplot () command and use that variable in the function. Matlab will not be able to distinguish which one represents the domain variable and which one has the constant value since Matlab will have to parse the string and form of the function and at the point of parsing the function is just a string.

7 PLOTTING MULTIPLE PLOTS ON ONE PAGE
subplot(3,2,1) subplot(3,2,2) subplot(3,2,3) subplot(3,2,5) subplot(3,2,4) subplot(3,2,6) For example, the command: subplot(3,2,p) Creates 6 plots arranged in 3 rows and 2 columns. Instructor notes: Here we see how the subplot () command divides the page into different plotting sections. It’s important to note that the first two parameters supplied to the subplot () command must be the same for all of the plots on a given page.

8 EXAMPLE OF MULTIPLE PLOTS ON ONE PAGE
Instructor notes: Here’s an example of multiple plots appearing on one page. The subplot () commands for this page would be of the form subplot (2, 3, p). Each time the subplot () command is used for this page, it would be followed by a, in this example, plot () command that would complete the plot in the section of the page that subplot () had dictated. The script file of the figure above is shown in the next slide.

9 % Example of using the subplot command. x1=linspace(1,20,100);
y1=sin(x1); subplot(2,3,1) plot(x1,y1) axis([ ]) text(2,1.5,'A plot of sin(x)') y2=sin(x1).^2; subplot(2,3,2) plot(x1,y2) text(2,1.5,'A plot of sin^2(x)') y3=sin(x1).^3; (The file continues on the next slide) Creating a vector x1 Calculating y1 Creating the first plot Plotting the first plot Formatting the first plot Calculating y2 Creating the second plot Plotting the second plot Calculating y3 Formatting the second plot Instructor notes: The next two slides contain a script file that lists the commands needed to create the plot on the previous slide. The most important thing to point out is that each subplot () command is followed by a plotting command...in this case either plot () or fplot ()...that is responsible for actually drawing the plot in the section of the page that its corresponding subplot () command has made active. All ancillary plotting commands, such as axis () and text () correspond to the plot that subplot () has made active prior to the plotting commands.

10 text(2,1.5,'A plot of sin^3(x)') subplot(2,3,4)
plot(x1,y3) axis([ ]) text(2,1.5,'A plot of sin^3(x)') subplot(2,3,4) fplot('abs(sin(x))',[ ]) text(1,1.5,'A plot of abs(sin(x))') subplot(2,3,5) fplot('sin(x/2)',[ ]) text(1,1.5,'A plot of sin(x/2)') subplot(2,3,6) fplot('sin(x.^1.4)',[ ]) text(1,1.5,'A plot of sin(x^1^.^2)') Creating the third plot Plotting the third plot Formatting the third plot Creating the fourth plot Plotting the fourth plot Formatting the fourth plot Creating the fifth plot Plotting the fifth plot Formatting the fifth plot Plotting the sixth plot Formatting the sixth plot Creating the sixth plot Instructor notes:


Download ppt "MatLab – 2D Plots 2 MATLAB has many built-in functions and commands to create various types of plots. Instructor notes: We start with an example of some."

Similar presentations


Ads by Google