Maple and Matlab Tips



    Here are some tips for common problems you might run into when working in Maple and Matlab. If you are having a problem with something, check this list for help.

[ Maple ][ Matlab ]



Maple

  • Use "Pi" and not "pi"
    If you use "pi", Maple will display the Greek letter, but it will not be interpreted as the number 3.14159...
    • More generally, remember that Maple is case senstive.
  • Use the exp() function for the number e
    You can't just use the letter "e" if you want the number 2.71828... Note that the number e will show up in a bold font in Maple, while the letter will just appear as normal text.
  • % refers to what you last evaluated, not necessarily the line above.
    If you use %, evaluate some more statments, and then go back and try to evaluate the statement with the % again, your result will be different because something new is in %. This is one reason that it is good to assign names to your results.
  • Put in multiplication signs explicitly.
    e.g. a*(1-cos(x)) not a(1-cos(x))
    The latter will be interpreted as a function call with an argument of 1-cos(x). Maple won't complain, and it looks ok to our eyes, but you will get unexpected results.
  • Square brackets are used for lists.
    This is another case where the output will look ok to our eyes, because in mathematics we sometimes use square brackets in place of parens. In Maple, however, you always have to use parens for quantities.
    e.g. a*(1-cos(x)) not a*[1-cos(x)]
  • Sets are unordered.
    Maples sometimes returns sets as answers. Be careful because the next time you open this worksheet and evaluate it, Maple may not return the elements in the same order. This can cause problems if the following statements expect the elements of the answer to be in a certain order.
  • More about sets and lists...
    See section of 5.4 of the first week's notes for starters. To recap, curly braces {} make a set, i.e. an ordered group of objects. Square brackets [] make a list, i.e. an ordered set.
    Use lists in a standard "plot" command to plot two expressions and specify a color for each. e.g.
    plot([exp1, exp2], x=0..10, color=[coral, sienna])
    If you do "plot({exp1, exp2}, x=0..10, color=[coral, sienna])" then you aren't guaranteed that the colors will match up with the expressions that you're expecting. (See note above for why.)
    Use sets pretty much everywhere else. Most commands in Maple want a set, if you have to specify multiple items as one argument, rather than a list.
    You don't have to put braces around single items. It's ok to have naked results. e.g.
    solve(x-3, x)
    NOT any of the following: "solve(x-3, {x})" OR "solve({x-3}, x)" OR "solve({x-3}, {x})". Putting in extra braces just clutters your results and makes them harder to get at.
  • Referencing individual items in a set or list
    If you have either a set or a list, you can pull out the individual items using square brackets. e.g. If sol is a set/list containing two items, sol[1] is the first item, and sol[2] is the second item.
  • Break big equations into pieces.
    Spotting an error in a three line long tangled mass of variables and parens can be difficult. It is easier to debug smaller equations, and then string them together after you are confident in each smaller part.
  • Plan before you execute.
    Maple is incredibly powerful and can make doing involved mathematics very fast. However, it is important to think a problem through and have a plan of attack before you start plugging away. It is easy to get bogged down in convoluted equations and lose sight of your goal, making your work much more complex than it needs to be.
  • Follow directions! (especially on quizzes)
    Reading the directions carefully and putting the correct answer in the correct box are parts of the problem. It's easy to lose points over simple mistakes.
  • Place "Restart;" at the top of your work.
    When everything seems to be going wrong, place your cursor at that "Restart;" and press Enter through each line.
  • How to add new lines to your worksheet:
    ctrl-k will insert a new line above the cursor
    ctrl-j will insert a line below the cursor
    You can also access these commands through the "Insert->Execution Group" menu or the "[>" icon in the toolbar.
  • If you do an integral and get something with a limit in it...
    This generally means that Maple will need some more information in order to solve the integral symbolically and give you a nice simple answer. Try doing an assume() statement. Hint: Usually the sign of variables in the integrand is important when integrting symbolically.



Matlab

  • When NOT to use a for loop
    If you are trying to assign a value to a particular element in a matrix, do not use a for loop. Suppose you have a 5x5 matrix called A. If you want to set the 3rd row, 2nd column to be 1, simply do "A(3,2) = 1;". Do NOT do:
    		       % Note: this is an example of what NOT to do.
                           for i=3
    		           A(i,2) = 1;
    		       end
    		       
    Also, to access or look at the value of an element of an array called A... If you want to look at row r, column c, then use "A(r,c)". For example, to test if the element at row r, column c is a 1, you can do:
                           if (A(r,c) == 1)
    		          % then do something
    		       end
    		       
  • Careful about function and file names

    Do not name your functions with names that you think MatLab might already be using for something. For example, do not call your function any of the following: factor, network, square, poly, ... If you can think of a reason MatLab would have a function with a certain name already, then it probably does. You can find out if there is already a function defined with a certain name by trying to look it up in the help. If you do accidentally name your function after something that MatLab has already defined, some weird things can happen.

    Also, make sure to save your .m file as the same name of your function. If you don't, then it won't work.

  • Make sure you understand the basic programming concepts of "if" and "for" statements.
    Check out the "Additional Exercises" column for the second week of Matlab on the syllabus for extra examples. Make sure you can explain why the examples behave as they do.
  • Use "pi" in Matlab, not "Pi"
    Now that you've gotten used to "Pi" in Maple, be careful when switching to Matlab.
  • Scripts vs. Functions
    Scripts are just a series of commands (which you could have entered one by one in the command window) saved in a file, so that you can run them all with just a single command in the command window. Putting commands in scripts makes debugging and changing code easier. Scripts can see any variables currently in the workspace, and may modify them inside the script.
    Functions are also a series of commands, but they take one or more input arguments, and return one or more values. To call a function in the command window, unlike a script you must pass it values for the arguments it expects. If you want a function to know about a variable in your workspace, you must pass it to the function explicity. Functions cannot modify the variables in your workspace.
  • Function arguments and parameters
    The name of the parameters which you pass a function do not need to be the same as the name you give the arguments in the function. e.g. If the function f(x) is declared as "function y = f(x)" in an m-file, then in the command window I can call it as: "f(a)" or "f(b)" etc. (assuming that the variables "a" and "b" have been declared in my workspace).
  • Comment your code
    Use comments to explain what your code is doing. Don't just repeat the code line for line in English, but outline the general purpose of sections or particularily tricky lines. A bit of code explaining each for loop and/or if statement can be a good general rule. Make sure that if you come back to the code in a few weeks (e.g. before a test), you'll be able to understand what it is doing.
    A good strategy is to first lay out in comments what you want to do (this is called "pseudocode"), and then go back and fill in the actual code for each step once you've outlined the whole approach.
  • Assignment is different than testing equality
    To assign a value to a variable, use a single equals sign. e.g. "x = [1:4];" To test if a variable has a certain value, or two variables have the same value, use two equals sign. e.g. "if x == 4"
  • fzero, quad and ode45
    These matlab commands take a function name in single quotes as their first argument. Notice, it's the name of the function, not the name of the file that the function is in, so there's no ".m". Also, it's just the name - you don't supply any input arguments, or put any parentheses. For instance, if you have a function "ToBeZero", you would write: fzero('ToBeZero', 2.718). You can NOT write: fzero('ToBeZero-20', 2.718) or fzero('ToBeZero(x)', 2.718).
    This is the only time you'll use function names in single quotes, the rest of the time you'll want to call the function like normal, i.e. ToBeZero(x).