Troubleshooting:Common Mistakes

From MathLab

Jump to: navigation, search

Contents

[edit] Mathematica Reference Page

[edit] Try =entry first!

If you have a syntax problem, first try =entry: copy your code, click down on a new line, press "=", paste, and press enter. Mathematica will fix many syntax errors automatically, and will show you the corrected code. If you see the corrected code, click on it. The corrected code will replace your =entry miscode.

[edit] Common Mistakes

Most Mathematica syntax problems are of a very few types: (Green denotes proper Mathematica syntax, red expressions result in errors.)

  1. 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].
  2. 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 palette not pi
    • E or the pretty version on the palette not e
    • etc.
  3. Missing commas.
    • Plot[Cos[x],{x,0,4}] not Plot[Cos[x]{x,0,4}]
  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 parenthesis ().
  5. Printing without using Mathematica fonts.
    • This will cause weird characters to print all over the page.

examples of the error messages produced by these errors are below.


[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.

  1. 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.
  2. 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).
  3. Missing commas.
    • Plot[Cos[x]{x,0,4}]
      produces the message
      Plot called with one argument; 2 or more arguments are expected.
  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.

[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).