Cell 1: Bill Knowlton Extra Credit Assignment #4 USE YOUR OWN EQUATION

advertisement
Cell 1: Bill Knowlton
Extra Credit Assignment #4
Your Assignment: (NOTE: USE YOUR OWN EQUATION)
1. Using Mathematica, use the Style Sheet called ArticleModern (under
Format −> Style Sheet) for this assignment.
Use various Styles (under Format −> Style) to describe your assignment
and program. This Cell is using the Subsection Style.
2. Plot an equation (USE YOUR OWN EQUATION) in 2 dimensions that
is a function of multiple variables. An example is shown below. You will
need to use several commands including:
a. Plot[ ]
b. YourEquation[var1_,var2_,...,var#_]:=equation
c. Print[ ] to output several values of interest
3. Plot your function over 2 variables but in 2 dimensions on many plots.
4. Plot your function over 2 variables but in 2 dimensions all on one plot.
5. Plot your function over 2 variables in 3 dimensions.
6. Plot your function over 2 variables in 2 dimensions suppressing
multiple plots and graphing the multiple plots in one plot.
à Examples:
Problem 2.
In the Input Cell below, I use the ideal gas law showing a direct relation with temperture, T, and an
EC4- Multiple Plots v6.nb
2
inverse relation with pressure and is given by:
v@p_, n_, T_D := n Rp T
Note that there are other variables, but they are not included in the bracket [ ]. The variables
included in the [ ] are call Arguments allows us to vary or give a value directly for each variable in the
[ ]. For more information, see Help −> Master Index −> [ ].
Lastly, to add a comment in the Input Cell that Mathematica will ignore, use (* comment *). I use this
to document what I'm up to in the program.
In[31]:=
Clear@v, R, T, pD
nRT
v@p_, n_, T_D :=
p
H∗Constants∗L
R = 0.08206;H∗atm L êHmol KL∗L
H∗Plots∗L
Plot@v@p, 1, 298D, 8p, 1, 10<, Frame → True, GridLines → Automatic,
PlotStyle → 8RGBColor@0, 0, 1D<, FrameLabel → 8"p HatmL", "v HLL"<,
PlotLabel −> "v vs p for T=25o C"D
Print@"v: ", v@1, 1, 298D, "L at T=25o C"D
EC4- Multiple Plots v6.nb
3
v vs p for T=25o C
16
14
Out[34]=
v HLL
12
10
8
6
4
2
4
p HatmL
6
8
10
v: 24.4539L at T=25o C
à Problem 3.
Plot your function over 2 variables. If I want to graphically examine a function in 2D but with 2
variables, one way I do so is shown below.
The easiest way to show all of the graphs together would be to use the Table[ ] command instead of
using a "For loop" or "Do loop" approach. The Table is called gasplots, and contains the same Print
and Plot commands as in the For loop. The second argument {T,300,750,50}, iterates T from 300K to
750K in steps of 50, which returns the same result as the For loop.
Note that this is essentially a program because it includes a For loop type functionality and a Print
command and a Plot command and a Table command. Note that there are multiple [ ]s.
In[36]:=
gasplots = Table@
Print@"T= ", T, " K"D;
Plot@v@p, 1, TD, 8p, 1, 10<, Frame → True, GridLines → Automatic,
PlotStyle → 8RGBColor@1, 0, 0D<, FrameLabel → 8"p HatmL", "v HLL"<,
PlotLabel −> "v for varies Temps"D, 8T, 300, 750, 50<D
EC4- Multiple Plots v6.nb
4
T= 300 K
T= 350 K
T= 400 K
T= 450 K
T= 500 K
T= 550 K
T= 600 K
T= 650 K
T= 700 K
T= 750 K
v for varies Temps
20
16
14
12
10
8
6
4
v HLL
v HLL
:
,
15
,
10
5
2
p HatmL
4
6
8
10
2
v for varies Temps
p HatmL
4
6
8
10
v for varies Temps
25
v HLL
20
v HLL
Out[36]=
v for varies Temps
15
,
10
5
20
15
,
10
5
2
p HatmL
4
6
8
10
2
p HatmL
4
6
8
10
EC4- Multiple Plots v6.nb
5
v for varies Temps
v for varies Temps
30
25
20
v HLL
v HLL
25
,
15
10
5
2
p HatmL
4
6
8
10
2
v for varies Temps
35
30
25
20
15
10
5
,
2
p HatmL
4
6
8
6
8
10
p HatmL
4
6
8
10
>
2
p HatmL
4
6
Plot the above plots in one graph.
One way to plot all the data in one graph is to use the Show[ ] command.
Show@gasplotsD
10
40
35
30
25
20
15
10
à Problem 4.
In[37]:=
8
v for varies Temps
v HLL
p HatmL
4
6
,
2
,
2
35
30
25
20
15
10
5
10
v for varies Temps
40
35
30
25
20
15
10
p HatmL
4
v for varies Temps
v HLL
v HLL
,
15
10
5
v HLL
20
8
10
EC4- Multiple Plots v6.nb
6
v for varies Temps
16
14
Out[37]=
v HLL
12
10
8
6
4
2
4
p HatmL
6
8
10
à Problem 5.
Suppressing multiple plots and graphing the multiple plots in one plot.
1st: Setting up the problem by defining a function. We will defing the function DGmix as a function of 3 variables.
In[38]:=
Clear@ai, aj, R, x1, x2, ∆Gmix, T, bD
∆Gmix@x1_, x2_, T_D :=
x1 x2 Hai + Haj − aiL x2L + R T Hx1 Log@x1D + x2 Log@x2DL;
2nd: Defining constants:
In[40]:=
H∗Constants∗L
ai = 12 500; aj = 5500; R = 8.314; H∗JêmolK∗L
3rd: Defining a table "Giplots" to hold an array of DGmix outputted by the Plot[ ] command.
Note:
In the Plot[ ]command at the end, I use the command: "DisplayFunctionØIdentity"
This tells the Plot[ ] not to graph any plots.
EC4- Multiple Plots v6.nb
In[41]:=
7
Giplots = TableB
PlotB∆Gmix@1 − x2, x2, TD, 8x2, 0, 1<, Frame → True, GridLines → Automatic,
PlotStyle → 8RGBColor@0, 0, 1D<, FrameLabel → :"X2 ", "∆Gmix H
J
mol
L">,
PlotLabel −> "ai=12500; aj=5500;", DisplayFunction → IdentityF,
8T, 200, 1200, 100<F;
Print@"Temp. Range: 200K to 1200K @ 100K Increments"D
Print@"Curve at top: 200K; Curve at bottom: 1200K;"D
Temp. Range: 200K to 1200K @ 100K Increments
Curve at top: 200K; Curve at bottom: 1200K;
4th: I use the Show[ ] command to graph all the plots together. Since I told the Plot[ ] command not to plot, then I NEED
to tell the Show[ ] command to tell the Plot [ ] command to graph the data. This is done by adding into the Show[ ] the
following: "DisplayFunction->$DisplayFunction" .
So I use Show[Giplots, DisplayFunction -> $DisplayFunction] to plot all the plots
In[44]:=
Show@Giplots, DisplayFunction → $DisplayFunctionD
ai=12500; aj=5500;
1200
Out[44]=
mol
∆Gmix H
J
L
1000
800
600
400
200
0
0.0
0.2
0.4
0.6
0.8
1.0
X2
à Problem 6.
Plot the above plots in one graph WITHOUT using the Table[ ] command. Rather, use the Range[ ]
EC4- Multiple Plots v6.nb
8
and Evaluate[ ] commands to plot over two variables.
See p. 30-31 of Hollis' A Mathematica Companion for Differential Equations
In[45]:=
Clear@v, R, T, pD
nRT
v@p_, n_, T_D :=
p
R = 0.08206`;
imin = 200; imax = 500; step = 50;
T = Range@imin, imax, stepD;
PlotAEvaluate@v@p, 1, TDD, 8p, 1, 10<, Frame → True, GridLines → Automatic,
PlotStyle → 8RGBColor@0, 0, 1D<, FrameLabel → 8"p HatmL", "v HLL"<,
PlotLabel → "v vs p for T=\!\H\∗SuperscriptBox@\H25\L, \Ho\LD\LC"E
PrintA"v: ", v@1, 1, 298D,
"L at T=\!\H\∗SuperscriptBox@\H25\L, \Ho\LD\LC"E
v vs p for T=25o C
20
Out[50]=
v HLL
15
10
5
2
v: 24.4539L at T=25o C
4
p HatmL
6
8
10
EC4- Multiple Plots v6.nb
9
v vs p for T=25o C
40
v HLL
30
20
10
0
0
2
v: 24.4539L at T=25o C
4
6
p HatmL
8
10
EC4- Multiple Plots v6.nb
10
à Problem 7.
Plot the function in 3 dimensions.
Use the Plot3D[ ] command.
In[65]:=
Plot3D@v@p, 1, TD, 8p, 1, 10<, 8T, 300, 750<, Mesh → True, FaceGrids → All,
AxesLabel → 8"P HatmL", "T HKL", "v HLL"<D
H∗Used 3D Viewpoint Selecter in the Input pulldown menu for
viewing plot from a different viewpoint∗L
Print@"You can click on the 3D plot and move it with your mouse."D
Plot3D@v@p, 1, TD, 8p, 1, 10<, 8T, 300, 750<, Mesh → True, FaceGrids → All,
AxesLabel → 8"P HatmL", "T HKL", "v HLL"<,
ViewPoint −> 81.402, −3.025, 0.580<D
Out[65]=
You can click on the 3D plot and move it with your mouse.
EC4- Multiple Plots v6.nb
11
Out[67]=
à Problem 8.
Using Manipulate[ ]
The Manipulate[ ] command provides interactive plots. Variables other than the independent and dependent variables are
displayed as sliders and are the interactive part of the plot. The sliders can be moved with the mouse and the plot will
change as the slider is moved.
Here is an example plot with 2 sliders.
EC4- Multiple Plots v6.nb
12
In[55]:=
H∗Constants∗L
Clear@R, x1, x2, ∆Gmix, T, a1D
R = 8.314; H∗JêmolK∗L
∆Gmix@x2_, T_D := a1 H1 − x2L x2 + R T HH1 − x2L Log@1 − x2D + x2 Log@x2DL;
ManipulateBPlotBa1 H1 − x2L x2 + R T HH1 − x2L Log@1 − x2D + x2 Log@x2DL,
8x2, 0, 1<, PlotRange → 8−8000, 6000<, Frame → True,
GridLines → Automatic, PlotStyle → 8RGBColor@0, 0, 1D<,
J
FrameLabel → :"X2 ", "∆Gmix H
L">F,
mol
J
::a1, 20 000, "Multiplier a1 H
L">, 3000, 25 000>,
mol
88T, 750, "Factor T HKL"<, 100, 1500<F
Multiplier a1 H
J
mol
L
Factor T HKL
6000
4000
mol
∆Gmix H
Out[58]=
J
L
2000
0
−2000
−4000
−6000
−8000
0.0
0.2
0.4
0.6
X2
Here is an example plot with 3 sliders.
0.8
1.0
EC4- Multiple Plots v6.nb
In[59]:=
13
H∗Constants∗L
H∗ai=12 500; aj=5500;∗L R = 8.314; H∗JêmolK∗L
ManipulateB
PlotBH1 − x2L x2 Hai + Haj − aiL x2L + R T HH1 − x2L Log@H1 − x2LD + x2 Log@x2DL,
8x2, 0, 1<, PlotRange → 8−8000, 6000<, Frame → True,
GridLines → Automatic, PlotStyle → 8RGBColor@0, 0, 1D<,
J
FrameLabel → :"X2 ", "∆Gmix H
L">F,
mol
J
::ai, 12 500, "Multiplier ai H
L">, 3000, 20 000>,
mol
J
L">, 3000, 10 000>,
::aj, 5500, "Multiplier aj H
mol
88T, 300, "Factor T HKL"<, 100, 1500<F
Multiplier ai H
Multiplier aj H
J
mol
J
mol
L
L
Factor T HKL
6000
4000
mol
∆Gmix H
J
L
2000
Out[60]=
0
−2000
−4000
−6000
−8000
0.0
0.2
0.4
0.6
X2
0.8
1.0
Download