y = 3 x + 1
The lengthier way, but one that allows for greater flexibility, is:
f[x_] := x^3 + x^2 - 5x - 5
Note both the x _ symbol on the left, and the := defining symbol. Defining f this way allows you to easily evaluate, say, f[2] , f[5 t + 3], etc. In contrast, to evaluate the first definition of y at x = 2 you must type:
y /. x -> 2
Remember that Mathematica tends to do exact arithmetic; for example, carrying the fraction 2358463 / 78932749. This is time-consuming for the computer and one usually prefers the decimal approximation. One way to get the decimal form is to use the N command; for example.
N[2358463/78932749]
Or, you can simply put a decimal point after one of the integers. Try it. If you really need lots of decimal places of p you can type and execute, for example:
goodPi = N[Pi, 50]
?Plot
Or if you want more detail, type ??Plot. Try it.
However, if you simply call upon another notebook to refresh your memory on a command, and do no execution, there is no problem.
Abs---take the absolute value of its argument.
Abs[f[2]]
Clear---is used to "undefine" variables or functions. This is useful when you are changing the definition of f, or things have gotten confused. For example, Clear[f, y] clears above functions.
D---will take the derivative of specified function with respect to specified variable. For example,
D[f[x], x]
Do---allows you to set up a "loop" in order to perform the same (or similar) commands numerous times. The following does six Newton iterations on function t^2 - 5, to approximate Sqrt[5]:
t = 3.0
Do[t = t - (t^2 - 5.0) / (2.0 t), {k,
1, 6}]
t
The variable k is a "counter" that here takes on values 1,2,3,4,5,6.
Expand---expands out in powers of x (or some other expression). Consider these two examples:
Expand[y f[x]]
Expand[Cos[x] (3 - Cos[x]^2)]
Integrate---the first form of Integrate attempts to find an antiderivative of the first argument, with respect to the second.
Integrate[y, x]
The second form computes a definite integral.
Integrate[y, {x, 0, 3}]
(Should this fail, see NIntegrate below).
Factor---will attempt to factor the argument. In polynomials, it only looks for integer factors. For example,
Factor[f[x]]
Limit---will attempt to take the specified limit.
Limit[y, x -> 3]
The following takes the limit from the right (use +1 to get the limit from the left):
Limit[f[x], x -> 0, Direction -> -1]
N---converts the argument to decimal form and shows six figures. More figures can be shown if requested.
N[Pi]
N[Pi/2, 16]
NIntegrate---attempts to do a numerical integration of the first argument. Typically, the result is good to 16 figures. For example,
NIntegrate[y, {x, 0, 3}]
(Also see Integrate above).
Plot---plots one or more functions over the specified domain interval. There are many options (try ??Plot), the most common is perhaps PlotRange. Try this with, and without, the option:
Plot[{y, f[x]}, {x,-4,4}, PlotRange -> All]
To get a label on a plot we can do e.g.:
Plot[{y,
f[x]}, {x,-4,4}, PlotRange -> All,
PlotLabel -> "J. Jacobs Project No.
6"]
A carriage return (not "Enter") was used to go to a new line,
thereby keeping the command from running off of the screen to the right.
Sign---produces the sign (+1 or -1) of the argument.
Sign[-Pi/2]
This function is useful in producing "step functions"; for example,
g[x_] := Sign[x - 1] - Sign[x - 3]
Simplify---attempts to clean up the expression by looking for common factors, etc.
Simplify[(f[x]- y) / (x + 3)]
Solve---attempts to solve the specified equation (or equations) for the specified variables. Solve is effective primarily for polynomial equations. The following finds x-values at which f[x] and y are equal:
ourRules = Solve[y == f[x], x]
The result of this is a set of "rules", saved in a variable we have called "ourRules", which can be used as follows to evaluate y at these x values:
y /. ourRules
Next we solve a pair of equations for unknowns s and t:
Solve[{3 s + t == 7, 2 s - 3 t == 1}, {s,t}]
Sum---this command allows you to conveniently perform a sum, as a "counter" (k below) takes on a specified range of values. For example, to compute the sum of squares of the first six integers, execute:
sumsq = Sum[k^2, {k, 1, 6}]
Table and TableForm---Table is used, for example, to generate a list of order pairs; and TableForm puts them in a more readable form.
Table[{n, n^2}, {n, 1, 5}] // TableForm
Note that the ordered pairs {n, n^2} are grouped using braces and not the standard parentheses.
Together---puts terms in a sum over a common denominator, and does some simplifying. For example,
(1 + x) y + f[x] / (1 - x)
Together[%]