Troubleshooting:Common Mistakes
From MathLab
Contents |
[edit] Mathematica Reference Page
[edit] Common Mistakes
Most Mathematica syntax problems are of a very few types: (Green denote proper Mathematica syntax, red expressions result in errors.)
- Parenthesis (), Braces {}, and Brackets [].
- Parenthesis are for breaking up multiplication and addition e.g. (x+2)^2 is the same as x^2 + 4 x + 4.
- Braces {} are for grouping things together e.g. {1, 2} is the point or vector with coordinates x=1, and y=2.
- Brackets [] are for functions e.g. Sin[x].
- Capitalization. All Mathematica commands start with a Capital letter.
- Sin[x] not sin[x].
- Plot[Cos[x],{x,0,4}] not plot[Cos[x],{x,0,4}]
- Pi or the pretty version on the Pallete not pi
- E or the pretty version on the Pallete not e
- etc.
- Missing commas.
- Plot[Cos[x],{x,0,4}] not Plot[Cos[x]{x,0,4}]
- Bad function definitions.
- f[x_] = Cos[x^2] defines a function. Common mistakes are:
- f[x] = Cos[x^2]
- missing underscore.
- f(x_) = Cos[x^2]
- It needs brackets [] not parens ().
- Printing without using Mathematica fonts.
- This will cause weird characters to print all over the page.
examples of the error messages priduced by these errors are below.
[edit] Avoiding Common Mistakes
Avoiding these mistakes is simple:
- Parenthesis (), Braces {}, and Brackets []. Remeber the rules:
- Parenthesis () are for breaking up multiplication and addition
- Braces {} are for grouping things together
- Brackets [] are for functions
- Capitalization. Remember ll Mathematica commands start with a Capital letter.
- Paste from the Palette.
- Always Capitalize
- Missing commas.
- Paste from the palette and fill in the template.
- Copy carefully from the Help.
- Bad function definitions.
- Remember the underscore.
- Remember to use brackets [].
- Printing without using Mathematica fonts.
- Put a checkmark in the box next to 'Print using Mathematica fonts'.
- This will prevent weird characters on your printed labs.
- Put a checkmark in the box next to 'Print using Mathematica fonts'.
[edit] Error Messages
These syntax errors produce (once you know how to read them) fairly clear (well compared to Matlab, Maple, Fortran, C++, Java, etc.) error messages. the trick is to only look at the first message.
- Parenthesis (), Braces {}, and Brackets [].
- Plot[Sin(x),{x,0,4}]
- produces the error message
- sin x is not a real number at x=1.66666 x 10^-7
- In other words it thinks sin and x are two seperate things.
- Plot(Sin[x],{x,0,4})
- produces the message "(" can not be followed by "Sin[x], {x,0,4})".
- Expand[ (x+2)^2] gives x^2 + 4 x + 4
- What do you think
- Plot[ {Sin[x], Cos[x]},{x, 0, Pi}]
- will give? Remember the braces group things together.
- Plot[Sin(x),{x,0,4}]
- Capitalization. All Mathematica commands start with a Capital letter.
- Plot[sin[x],{x,0,4}]
- produces the error message
- sin(x) is not a real number at x=1.66666 x 10^-7
- In other words it does not understand sin[x].
- plot[Cos[x],{x,0,4}]
- Just parrots back the command. This is what Mathematica does when it does not know what to do. You also get a warning the first time you do this that "plot" is similar to "Plot".
- Plot[ Cos[x],{x,0,pi}]
- gives the message that
- Limiting value pi in {x,0,pi} is not a real number.
- Plot[e^x, {x, 0, Pi}]
- gives the message that e^x is not a real number at x=1.309 x 10^(-7).
- Plot[sin[x],{x,0,4}]
- Missing commas.
- Plot[Cos[x]{x,0,4}]
- produces the message
- Plot called with one argument; 2 or more arguments are expected.
- Plot[Cos[x]{x,0,4}]
- Bad function definitions.
- After defining f[x_] = Cos[x^2]
- f'[x] gives the derivative -2 x Sin[x^2].
- After running f[x] = Cos[x^2]
- f'[x] simply parrots.
- f(x_) = Cos[x^2]
- produces the strange message
- Tag Times in ..... is protected.
- After defining f[x_] = Cos[x^2]
[edit] Printing Problems
If you get strange characters printed throughout your printed lab, make sure you have the "Print With Mathematica Fonts" option selected (button depressed).
