Troubleshooting:Common Mistakes

From MathLab

(Difference between revisions)
Jump to: navigation, search
Revision as of 16:43, 23 August 2012 (edit)
Malabdir (Talk | contribs)
(Common Mistakes)
← Previous diff
Current revision (16:45, 23 August 2012) (edit) (undo)
Malabdir (Talk | contribs)
(Avoiding Common Mistakes)
 
Line 31: Line 31:
examples of the error messages produced by these errors are below. examples of the error messages produced by these errors are below.
-===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.+
===Error Messages=== ===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. 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.

Current revision

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

Personal tools