MA/CS375 Fall MA/CS 375 Fall 2002 Lecture 4
MA/CS375 Fall Plotting in Matlab
MA/CS375 Fall Example 5 (building and plotting a function of a vector) 1)create a vector x, with 50 equally spaced entries from 0 to 1.0 2)create a vector f, with entries of cosine evaluated at entries of 2*pi*x 3)plot f as a function of x, using + symbols
MA/CS375 Fall Example 5 (figure created by Matlab)
MA/CS375 Fall Example 5 (building and plotting a function of a vector) 1)create a vector x, with 50 equally spaced entries from 0 to 1.0 2)create a vector f, with entries of cos evaluated at entries of x 3)plot f as a function of x, using a blue line
MA/CS375 Fall Example 5 (figure created by Matlab)
MA/CS375 Fall Example 6 (building and plotting two functions of a vector) 1)create a vector x, with 50 entries from 0.0 to 1.0 2)create a vector f, with cos(2*pi*x). 3)create a vector g with entries sin(2*pi*x) 4)plot f as a function of x, using a blue line 5)on same graph plot g as a function of x, using red + symbols.
MA/CS375 Fall Example 6
MA/CS375 Fall Close Up of Example 6 code Instruct Matlab to keep the current graph window and its contents for the next plot.
MA/CS375 Fall Close Up of Example 6 code Instruct Matlab that you have finished plotting in the current graph window.
MA/CS375 Fall Quick Reference For Plot (Taken from Matlab: > help plot) Various line types, plot symbols and colors may be obtained with PLOT(X,Y,S) where S is a character string made from one element from any or all the following 3 colunms: y yellow. point - solid m magenta o circle: dotted c cyan x x-mark-. dashdot r red + plus -- dashed g green* star b blues square w white d diamond k black v triangle (down) ^ triangle (up) < triangle (left) > triangle (right) p pentagram h hexagram
MA/CS375 Fall Adding a Title, Labeling Axes and Legends Ok – so we have made a cool graph But it isn’t much use to anybody else unless you label everything in sight! The following functions will modify the current figure: title, xlabel, ylabel, legend
MA/CS375 Fall Example 7 The label commands
MA/CS375 Fall Details: Set up the legend box with descriptions of each curve: >> legend('f=cos(2*pi*x)', 'g=sin(2*pi*x)'); Label the axis >> xlabel('x'); Label the y-axis >> ylabel('f,g'); Give the graph a title >> title('The very boring graphs');
MA/CS375 Fall
MA/CS375 Fall Modifying the Figure Properties So I am not so pleased by the default fonts used by Matlab. It is easy to change the font: In the figure window click on the text object you wish to change
MA/CS375 Fall
MA/CS375 Fall Click on “Edit/Current Object Properties”
MA/CS375 Fall I prefer a larger Arial font
MA/CS375 Fall
MA/CS375 Fall The Final Product
MA/CS375 Fall Alternative Ways To Change Fonts..
MA/CS375 Fall
MA/CS375 Fall Individual Exercise 1)Create two curves on the same graph: F = 1/(1+x^2) G = exp(x^3) 2)Plot the points at 33 points equally spaced between 0 and 1 3)Use black *’s for F and green o’s for G 4)Label the horizontal and vertical axes 5)Create a title (including your name) 6)Create a legend 7)Make sure the font’s are legible for my old eyes (also figure out how to change the axis fonts) 8)Print out your graph and hand it in.
MA/CS375 Fall Plotting Two-Dimensional Data
MA/CS375 Fall Example 7 (Plotting Two-dimensional Data) 1)create two vectors x1d and y1d, with 20 entries from 0.0 to 1.0 2)create matrices of values using the x1d and y1d to distribute points in the unit square with 20 points in the x and y directions. 3)plot results in a graph.
MA/CS375 Fall Results From Example 7
MA/CS375 Fall Where are we going with this? We can evaluate a function at the two dimensional points (x2d,y2d) We can plot contours of this function where: a contour is a curve in the domain of the function where the value of the function is constant.
MA/CS375 Fall Example 8 (plotting contours of a two-dimensional function)
MA/CS375 Fall Example 9 (plotting two-dimensional function as a surface)
MA/CS375 Fall Example 9 cont. (plotting two-dimensional function as a smooth surface)
MA/CS375 Fall Summary of Lecture 4 We have learnt how to: – plot a graph – plot multiple graphs in the same window – improve the fonts – label everything in sight…. – plot contours of a two-dimensional function – plot a two-dimensional function as a surface