ECM6Lecture8Vietnam_2014
description
Transcript of ECM6Lecture8Vietnam_2014
-
Slide 1 of 14
ECM6 Computational Methods :
Lecture 8Overview of Graphical Functions
Brian G. HigginsDepartment of Chemical Engineering & Materials Science
University of California, Davis
April 2014, Hanoi, Vietnam
-
Slide 2 of 14Topics for Lecture 8
In this lecture we give a brief over view of the following topics
1. Plotting functions2. Plot Options3. Adding Text to a plot4. List and Contour Plots5. Extracting data from plot functions
In[1]:= Remove@myFunctions, myFun, myFun2, myF, myG, feqnD
2 ECM6Lecture8Vietnam_2014.nb
-
Slide 3 of 14Plotting Functions: The Basics
Mathematica has several plotting routines that allow you to plot a function of a single variable or multiple variables. In this lecture we will examine The Plot function and how to set its options. We will also consider several axillary plot functions: ListPlot, ParametricPlot and ImplicitPlot.
Mathematica's built-in function called Plot that can be used to plot an expression (exp) in the interval a to b. The plot function is specified as
Plot[exp, {x, xmin, xmax}, options]
Example 1
In the example given below we plot Sin[x] in the interval 0 to 3p. Since we have not specified any options, Mathematica has used the default values for the plot options.
In[2]:= Plot@Sin@xD, 8x, 0, 3 p
- In[3]:= Plot@8Sin@xD, Cos@xD
-
In[7]:= myFun@aDOut[7]=
2 I32 - Cos@3 aD + 2 a Sin@3 aDMI1 + 4 a2M 32Next, we plot the function over the interval 1 t 3 and use the command //Timing to determine how much CPU time it took for Mathematica to make the plot:
In[8]:= Column@Timing@Plot@myFun@tD, 8t, 1, 5
- In[11]:= Column@Timing@Plot@myFun2@tD, 8t, 1, 5
- In[13]:= Plot@Evaluate@xSin@xDD, 8x, 0, 3 p
-
Slide 4 of 14Exploring Plot Options For Axes, PlotLabel and Ticks
Let us consider more carefully the options available in Plot. Each option is a rule of the form
OptionName OptionValue
To see what options are available we can wrap the function Plot with the command Options
In[16]:= Options@PlotDOut[16]= :AlignmentPoint Center, AspectRatio 1
GoldenRatio, Axes True,
AxesLabel None, AxesOrigin Automatic, AxesStyle 8
- In[17]:= Plot@Sin@xD, 8x, 0, 3 p
-
In[19]:= Plot@Sin@xD, 8x, 0, 3 p{{StyleDirective for x-axis},{StyleDirective for y-axis}}
Here is an example where we change the appearance of the axes.
In[20]:= Plot@8Sin@xD, Cos@xD
-
In[22]:= ? Thin
Thin is a graphics directive that specifies that lines which follow should be drawn thin.
Another set of directives are Large, Medium, Small and Tiny. For example
In[23]:= ? Small
Small is a style or option setting that specifies that objects should be small.
Here we use Tiny to specify the dashing
In[24]:= Plot@8Sin@xD, Cos@xD
-
We can also specify what the label should be for each tick mark. The syntax in that case is
Ticks 888x1, label1
-
Slide 5 of 14Exploring Plot Options: PlotRange PlotStyle
PlotRange Option
The PlotRange option determines the range of the dependent and independent variables that will be displayed in the plot. If we want to restrict the range of the vertical axis the syntax is
PlotRange {ymin, ymax}
For example, suppose in the Sin plot shown earlier we want restrict the view for values of Sin@xD > 0. Then the range specification is shown below
In[27]:= Plot@Sin@xD, 8x, 0, 3 p
-
Alternative we may want to have a plot that has the vertical range specified automatically by Mathemat-ica, and restrict the horizontal range as in this example
PlotStyle Related Options
The syntax for the PlotStyle option is
PlotStyle Style Directive
The Style Directive is a graphic primitive that allows the user to specify how the plot should be drawn, e.g.. line thickness, color, broken line, etc. We will discuss the following style directives:
Thickness@valueD, Dashing@8d1, d2, {StyleDirective1, StyleDirective2, }
In the following example our plot has a specified thickness, and a specified GrayLevel:
In[29]:= Plot@Sin@xD, 8x, 0, 3 p
- In[30]:= Plot@Sin@xD, 8x, 0, 3 p
- In[32]:= Plot@8Sin@xD, Cos@xD
-
Slide 6 of 14Built-in Colors and Color Functions
The number of built-in color functions has expanded dramatically. Here is a list
Red Green Blue Black White Gray Cyan Magenta Yellow Brown Orange Pink Purple
LightRed LightGreen LightBlue LightGray LightCyan LightMagenta LightYellow LightBrown LightOrange LightPink LightPurple
Thus we can create a plot using these functions
In Version 7 the number of built-in color functions has expanded dramatically. Here is a list
Red Green Blue Black White Gray Cyan Magenta Yellow Brown Orange Pink Purple
LightRed LightGreen LightBlue LightGray LightCyan LightMagenta LightYellow LightBrown LightOrange LightPink LightPurple
Thus we can create a plot using these functions
In[33]:= Plot@8Sin@xD, Cos@xD
- In[34]:= Plot@8Sin@xD, Cos@xD
-
Slide 7 of 14ColorData
You can also generate a color using Mathematicas ColorData function
In[36]:= ? ColorData
ColorData@"scheme"D gives a function thatgenerates colors in the named color scheme when applied to parameter values.
ColorData@"scheme", "property"D gives the specified property of a color scheme.ColorData@"collection"D gives a list of color schemes in a named collection.ColorData@D gives a list of named collections of color schemes.
Color Schemes
There are 3 named collections or color schemes available
In[37]:= ColorData@DOut[37]= 8Gradients, Indexed, Named, Physical True and FrameStyle -> Hue[.8]
In[49]:= Plot@8Sin@xD, Cos@xD
- In[51]:= Plot@8Sin@xD, Cos@xD
- In[53]:= Plot@8Sin@xD, Cos@xD
-
Slide 9 of 14Using the Show Function
In some application it is desirable to suppress the display of a plot in the front end. In Mathematica 6 and later we simply use the ";" command. This is useful when we need to display several different types of plots on the same set of axes, but do not want to show individual plots. Consider the following example:
In[56]:= myPlot1 = Plot@Sin@xD^2, 8x, 0, 3 Pi
-
However, you can specify options in Show that override many but not all existing options in the graphic object. Consider the following
In[60]:= myPlot3 = Plot@Sin@xD, 8x, 0, 3 p
- In[62]:= myPlot3 = Plot@Sin@xD, 8x, 0, 3 p
-
Slide 10 of 14Adding Text to a Graphic
In this section we revisit the use of PlotLabel, AxesLabel, options and then consider the Epilog option. In the use of these options we will examine how to incorporate text into graphics. The first option we will consider is PlotLabel.
PlotLabel
The simplest syntax for this option is PlotLabel->This is a Trial Plot
In[64]:= Plot@Sin@xD, 8x, 0, 3 p
-
In[65]:= PlotASin@xD2, 8x, 0, 3 p"ylabel", or AxesLabel-> {"xlabel","ylabel"}. In the following example we illustrate how the StyleForm wrapper can be used to provide attributes to the individual labels
In[66]:= PlotASin@xD2, 8x, 0, 3 p 88x1, x2,
- In[67]:= Plot@Sin@xD, 8x, 0, 3 p
-
In the previous examples we manipulated the attributes of the text associated with labels of ticks using the wrapper Style.
BaseStyle
We can also specify the attributes for the entire graphic. This is done with the plot option BaseStyle. The syntax for BaseStyle is similar to Style.
In[70]:= ? BaseStyle
BaseStyle is an option for formatting and related constructs that specifies the base style to use for them.
In the following example we illustrate the use. Note that the syntax we are using is 8option1 -> value1, ..
- In[73]:= PlotASin@xD2, 8x, 0, 3 p
-
Slide 11 of 14Other Plot Functions: ListPlot
ListPlot function
The function that is used to plot discrete data points is called ListPlot. In its simplest form the argument of ListPlot is a list of y-values of the form 8y1, y2, y3, .. yn}. To illustrate the features of ListPlot, we first generate a data set by using the function Table (we have suppress the output by ending the input command with a semi-colon):
In[75]:= yvalues = Table@Sin@5 xD Exp@-x^2D, 8x, -3, 3, .02
-
In[77]:= Options@ListPlotDOut[77]= :AlignmentPoint Center, AspectRatio 1
GoldenRatio, Axes True,
AxesLabel None, AxesOrigin Automatic, AxesStyle 8
-
In[80]:= ListPlot@xyvalues, PlotRange AllD
Out[80]=-3 -2 -1 1 2 3
-0.5
0.5
We can embellish our plot using the various ListPlot options as illustrated with the Plot function. For example suppose we want to change the size of the data points and their color
In[81]:= ListPlot@xyvalues, PlotRange All, PlotStyle [email protected], [email protected]
-
In[83]:= ListPlot@xyvalues, PlotRange AllD
Out[83]=-3 -2 -1 1 2 3
-0.5
0.5
We can embellish our plot using the various ListPlot options as illustrated with the Plot function. For example suppose we want to change the size of the data points and their color
In[84]:= ListPlot@xyvalues, PlotRange All, PlotStyle [email protected], [email protected]
-
In[87]:= ListPlot@myNewXYValues, PlotRange AllD
Out[87]=-3 -2 -1 1 2 3
-0.5
0.5
If you want to join the data points use the option Joined
In[88]:= ListPlot@myNewXYValues, PlotRange All, Joined TrueD
Out[88]=-3 -2 -1 1 2 3
-0.5
0.5
38 ECM6Lecture8Vietnam_2014.nb
-
Slide 12 of 14Other Plot Functions: ParametricPlot
The ParametricPlot function allows one to plot parametric curves in two dimensions. The function is specified as
In[89]:= ? ParametricPlot
ParametricPlotA9 fx, fy=, 8u, umin, umax
- In[93]:= ParametricPlot@8myF@tD, myG@tD
-
Slide 13 of 14ContourPlot
Plotting Implicit Functions
In the previous examples the function that was plotted was an explicit function of the independent variable, viz., y=f(x). When y is only known implicitly as a function of x , g(x,y)=0, then it is necessary to use the routine ContourPlot. We illustrate the use of ContourPlot with the following equation
In[94]:= feqn@a_, b_D@x_, y_D := y2 Iy2 - b2M - x2 Ix2 - a2M == 0We plot this equation for selected values of the parameters : a=3, b=3.2. Note that we must wrap Evaluate around the implicit equation, feqn[a, b] [x, y], since we made a delayed assignment (see above). For selected values of x the plot shows that our equation has 4 solutions!
The following example illustrates the method. In this example we also invoke some plot options
In[95]:= ContourPlot@Evaluate@feqn@3, 3.2D@x, yDD, 8x, -4, 4
-
Slide 14 of 14Application: Exploring the Plot Options
Mathematica's plotting routines use an adaptive sampling routine. Thus the number of points used by a routine for plotting a function is not readily known. Equally important we do not know how Mathematica sampled the function. In this example we will show how one can deduce this information using some simple programing techniques. Consider the following plot
In[96]:= myBesselPlot = Plot@BesselJ@1, xD, 8x, 0, 6 p
-
Our next task is to take the list of data pairs and construct a new set of line primitives for each of the data pairs with the following form:
Line@88xi, yi
-
In[102]:= Options@PlotDOut[102]= :AlignmentPoint Center, AspectRatio 1
GoldenRatio, Axes True,
AxesLabel None, AxesOrigin Automatic, AxesStyle 8
-
In[105]:= Show@myBesselPlot2, mySamplePoints2D
Out[105]=
5 10 15
-0.2
0.2
0.4
ECM6Lecture8Vietnam_2014.nb 45