Sei sulla pagina 1di 3

Mathematica Notes 2.

0
Simplication
There are often many ways to write a correct answer. The algorithms that Mathematica uses often lead it to print solutions in cumbersome form. If you add //Simplify to the end of the calculation, Mathematica will apply algebra and trigonometric identities to try to provide you with a more aesthetic solution. //FullSimplify tells Mathematica to try harder. In my experience, //Simplify usually does the trick.

Compound Expressions
If you type a = 1, b = 2, c = 3 and d = a b + c on separate lines, Mathematica will provide you with output lines for each of the trivial assignments as well as an output for the calculation at the end (its not up to the software to decide what is important and what is not). Chances are, youre really not so interested in seeing output for those intermediate steps. . . Type a = 1; b = 2; c = 3; d = a b + c (without the quotes) and execute. Semicolons allow you to group steps together in a compound calculation - Mathematica will recognize this as a single computation and will only print the nal result. Type a = 1; b = 2; c = 3; d = a b + c; (note the trailing semicolon!) and execute. Mathematica interprets the nal expression (implied, after that last semicolon) as a null computation and returns the result (Null, or nothing). This is is useful if you dont need any output from this stage in your calculation.

User-Dened Functions
Enter the following: f [x] := 2x2 +3 and then execute f [x]. Mathematica will dutifully output some equivalent to 2 x2 + 3. Now execute f [y ] and f [7]. What happened? Mathematica knows what f [x] is. but not f [y ] or f [7]. Execute x = 3; f [x]. Mathematica rst evaluates the assignment for x and then behaves as though it doesnt know what f [3] is. Mathematica doesnt know that f [x] is a function. Clear[f ,x] and type: f [x ] := 2 x2 + 3. Now try to execute x = 3; f [x], f [y ] and f [7]. An underscore that follows a variable in an argument tells Mathematica that that variable is to be understood as a placeholder in the expression that follows. 1

What happens if you goof and use an immediate assignment by mistake? Keep this in mind when youre troubleshooting. Like variables, be sure to clear out functions when theyre no longer needed: Clear[f ,x]

Recursive Functions
Lets make our own Fibonnaci generator. Type b[n ] := b[n 1] + b[n 2] and b[1] = b[2] = 1 and execute. Execute b[1], b[2], b[3], b[4] and so on. . . Execute b[30]. You might need to be a little patient. Why does it take so long? b[30] requires b[29] and b[28] - they have to be calculated rst. b[29] needs b[28] and b[27], and so on. The worst part is, each recursive calculation is done from scratch - when Mathematica calculates b[28] to obtain b[30], it cant use the b[28] it calculated to obtain b[29] - it doesnt keep that value around. . . (You can abort the evaluation using the Kernel menu, if youre still waiting) Clear[b] and type b[n ] := b[n] = b[n1] + b[n2] and b[1] = b[2] = 1. How is this dierent? The assignment we snuck in in the middle of the rst line is an immediate assignment that tells Mathematica to remember the intermediate values. Now try b[30]. Wise construction of recursive functions can save you a lot of time and grief.

Solving Algebra Equations


The command Solve[expr, var] will try to solve the expression expr for the variable var. The command Solve[{expr1,expr2,expr3}, {var1,var2,var3}] (note the use of lists!) will attempt to solve the system of equations (expr1,expr2,expr3) for the variables var1,var2, and var3. Make sure you use equivalence (==) rather than assignment (=) in those expressions. Dont be afraid to use //Simplify

Solving Dierential Equations


The command DSolve[expr, f unc, var] will try to solve the dierential equation expr for the function f unc having an independent variable var. DSolve is obscenely sensitive to syntax, and the syntax used is, perhaps, a little unique within Mathematica. Though the command looks simple enough, you can spend hours trying to get it to work (I have) - the problem is almost always syntactic. Be forewarned. 2

The dierential equation (expr) is written mostly as you would see it on paper. Primes () are used to denote derivatives. Functions (and their derivatives) must carry an argument. For instance, the equation d2 x 2 2 dt2 + x = 0 would be entered within DSolve as x [t] + x[t] == 0. Execute DSolve[x [t] + 2 x[t] == 0, x[t], t]. If your luck is like mine, it didnt work. Did you Clear[x,t]? More likely, you didnt put a space between 2 and x[t]. Im nding explicit multiplication (*) quite useful. Once you get it to work, youll notice C [1] and C [2] in your answer - these are the undetermined integration constants. Mathematica can evaluate the integration constants too. Replace the differential equation with a list that includes the dierential equation and the boundary conditions. Try: DSolve[{x [t] + 2 x[t] == 0, x[0]==v0, x[0]==x0}, x[t], t] (Didnt work? Check for cleared variables and multiplication. Did you use equivalence and not assignment? . . . remember to tack arguments on the functions?). TIP: Try to put DSolve and all the code that sets up the calculation in a single cell. Start that cell with Remove[Global*]. Youll thank me later. (This starts the computation with a clean-slate, eliminating many/most of the problems that cause DSolve[...] to complain. Coupled dierential equations are no more dicult than anything else. For expr, provide a list that includes the dierential equations and any initial conditions you wish to impose. For f unc, provide a list of the functions to be solved for.

Basic Plotting
The basic plot command, Plot[f unc, {var, varlo , varhi }], plots the function f unc as a function of var, over the range varlo to varhi . Plot[{f unc1, f unc2},{var, varlo , varhi }] (note the use of lists) plots f unc1 and f unc2 over the range varlo to varhi . The output of Plot can be modied by adding options to its argument, for example: Plot[f unc, {var, varlo , varhi },opt1->optval1,opt2->optval2] Options[Plot] will tell you some of the options available to you (and their default values). Try Plot[x2 4,{x,-8,8},AxesLabel->{x-axis,yaxis}] The option PlotStyle->{{style for curve 1}, {style for curve 2},. . . }} is quite useful... Try: Plot[{x2 4,x3 },{x,-8,8},PlotStyle->{RGBColor[1,0,0],RGBColor[0,1,0]}]. (Each index is a weight, from 0-1, for Red, Green and Blue, respectively)

Potrebbero piacerti anche