Vous êtes sur la page 1sur 3

# Mathematica: Some Sample Code

Reminders: Press Shift-Enter to evaluate each command. Mathematica is case-sensitive; upper and lower case letters matter.
Also, you do not have to retype code if you want to make changes or add options. You can edit previous input and re-evaluate it.
To get the arrow character type a hypen - followed by the greater than sign >.
Do NOT type the input labels (for example, In[1]:= ). Mathematica automatically includes those and numbers them in the order
you evaluate your commands. Your numbers do not have to match the numbers listed below.
Graphing
Basic two-dimensional graphing including multiple curves and shading
The Plot function needs the function in the first part followed by the variable with its domain in a list { }.
In[1]:= Plotx^2, x, -4, 4
Same graph as above, but now with some popular options.
In[2]:= Plotx^2, x, -4, 4, PlotStyle - Red, PlotRange - 4, AspectRatio - Automatic,
GridLines - Range-4, 4, Range-4, 4, AxesLabel - "x", "y", AxesStyle - Thick
To graph two (or more) curves at the same time, include them in a list using { }.
In[3]:= Plotx^2, 2 x + 1, 5 - 2 x^2, x, -4, 4, AspectRatio - Automatic, PlotRange - -2, 7
You can adjust individual curve styles as shown below.
In[4]:= Plotx^2, 2 x + 1, 5 - 2 x^2, x, -4, 4, AspectRatio - Automatic, PlotRange - -2, 7,
PlotStyle - Red, DashingSmall, Blue, Black, Thickness0.01
Use Filling to shade graphs. You can also use the option FillingStyle to adjust the color.
In[5]:= Plotx^2, 2 x + 1, x, -4, 4, AspectRatio - Automatic,
PlotRange - -2, 7, Filling - 1 - 2, FillingStyle - LighterGreen, 0.8
Using alternating colors for the style, in this case, shades only the part between the intersection points.
In[6]:= Plotx^2, 2 x + 1, x, -4, 4, AspectRatio - Automatic,
PlotRange - -2, 7, Filling - 1 - 2, FillingStyle -> Orange, White
This example shows overlapping regions and dashing for the line.
In[7]:= Plotx^2, 2 x + 1, x, -4, 4, AspectRatio - Automatic, PlotRange - -2, 7,
Filling - 1 - Top, 2 - Bottom, PlotStyle - Thin, Black, DashingMedium
Polar graphing:
In[8]:= PolarPlotCos5 t, t, 0, Pi
Parametric graphing:
In[9]:= ParametricPlotCos3 t, Sin2 t, t, 0, 2 Pi
Basic three-dimensional graphing
Plot3D graphs functions of the form z = f (x, y). The first part is the function and that is followed by the domain for each variable.
You can rotate this graph by clicking it and dragging the mouse. Clicking-and-dragging while holding the Ctrl key will allow you
to zoom in and out.
Mat he m at ica: S om e S am ple Code - www. abby m at h. com
In[10]:= Plot3Dx + Cosy, x, 0, 1, y, 0, 2 Pi
Below is the same graph with some popular options for Plot3D.
In[11]:= Plot3Dx + Cosy, x, 0, 1, y, 0, 2 Pi, PlotStyle - Green,
Mesh - None, Boxed - False, AxesEdge - -1, -1, -1, -1, -1, -1
Functions and Solving Equations
Defining Functions
To define a function, use an underscore _ after the variable in the argument of the function when you first define it.
In[12]:= fx_ := x^2 + 1;
gx_ := x + 2;
Try each of the following inputs.
In[14]:= f3
In[15]:= g3
In[16]:= fg3
Solving Equations
To solve an equation, set up the equation with a double equal sign = = as below.
In[17]:= Solve4 x^2 + 2 x - 1 == 0
To solve for one variable in terms of others, indicate which variable to solve for. (Note that Log[ ] is the natural logarithm.
In[18]:= Solve5 x^2 - 2 Logy = 3 x, y
Put the solution together with its graph. (The Style command is used here to increase the font size.)
In[19]:= answer = y . FlattenSolve5 x^2 - 2 Logy = 3 x, y;
To solve a system of equations include each equation in a list and specify what you want to solve for.
In[21]:= Solvex + y = 3, 5 x - 2 y = 1, x, y
Below is a quick way to graph this system without solving for y in terms of x. A few options make it look nicer too.
In[22]:= ContourPlotx + y = 3, 5 x - 2 y = 1, x, -3, 3, y, -3, 3,
Axes - True, Frame - False, GridLines - Automatic, ContourStyle - Thick
Using Functions
Unless you have restarted Mathematica or cleared the memory, the functions you defined earlier are still available.
In[23]:= Plotfx, x, -2, 2, PlotLabel - fx
You can also find derivatives and integrals.
In[24]:= Dfx, x
In[25]:= DSinx + E^2 x, x
In[26]:= Integratefx, x
In[27]:= Integratefx, x, 0, 1
Manipulate
Mat he m at ica: S om e S am ple Code - www. abby m at h. com
Manipulate
Manipulate allows you to animate an output as you adjust one or more variables. There are many different types of controls and
options that let users create very interesting, dynamic modules. Some simple examples are below.
In[28]:= PlotSin2 x, x, 0, 2 Pi, PlotRange - 5
For example, starting with a basic plot as above you can adjust parameters using Manipulate. After evaluating the following,
move the slider to adjust the frequency of the graph.
In[29]:= ManipulatePlotSinb + x, x, 0, 2 Pi, PlotRange - 5, b, 1, 5
In[30]:= ManipulatePlota + Sinb + x, x, 0, 2 Pi, PlotRange - 5, b, 1, 5, a, 1, 5
Even the function can be a variable.
In[31]:= Manipulate
Plota + fcnb + x, x, 0, 2 Pi, PlotRange - 5,
b, 1, 5, a, 1, 5,
fcn, Sin, Cos, Tan, Cot, Sec, Csc
Manipulate works for non-graphical output as well.
In[32]:= Manipulatex^2, x, 0, 10
Column and Row help organize the output. Additional spacing is used in the coding to better understand it.
In[33]:= Manipulate
Column
Row"x = ", x,
Row"x^2 = ", x^2
,
x, 0, 10
In[34]:= Manipulate
Column
Row"x = ", x,
Row"x^2 = ", x^2,
Plotx^2, x, 0, 10, ImageSize - 200
,
x, 0, 10