This is not a course in programming. On the other hand, the whole point of using mathematical software is to save labor. Computers love to perform repetitive and tedious tasks, and are good at doing simple operations without mistakes. Those are the kinds of things that people are not good at doing. For that reason, we should know how to do some simple programming. Here are some naive principles of programming in Matlab and Maple.
It is best to make programs modular. Tasks that will be performed many times can be put in "functions"; code objects that are almost self contained. They take some input arguments, operate on those for a while, and return some outputs. A simple sketch of a function follows.
function(arg1,arg2) Now use arg1 and arg2 to compute a variable called output return output end
Most programs take arguments - variables and values that you type in for them to use. Typically arguments appear in parentheses after the function name. For example, in Matlab you pass the argument x to the function norm as
Most programs return variable values. In other words, certain things it computes are available after the program finishes. We can assign variables to the things returned so we can use them later. For example, the following line takes the result of the norm function and saves it in a variable called mynorm.
mynorm = norm(x)
You are comfortable working with a Matlab command line or a Maple worksheet. Programming these packages is almost the same. The chief difference is that instead of writing things line by line, you write many lines into a file. Running that command file executes all your commands sequentially. Still, the Matlab or Maple commands you know are the ones you use.
The most powerful thing about computers is their high tolerance for boredom. Make the computer do the boring things, and save your brain for the tricky things by using "for" loops. They are called loops because the execution goes around and around. These always have the form
for i from start_value to end_value, tasks to be repeated go here they frequently depend on the variable i when the "end" is reached, the program loops back to the "for" statement until end_value is passed end
Sometimes you don't know beforehand how many times you want to do a task. Instead, you want to do it as long as some condition is true. In this case, you use a "while" loop, instead of a "for". This is key: the "for" loop does some task a fixed number of times; the "while" loop does some task for just as long as some condition is true.
while (some condition is true), tasks to be repeated go here they may or may not depend on a counter i when the "end" is reached, the program loops back to the "while" end
Your program can choose to do different things based on e.g. whether some expression yields a positive or negative result. To do this use an "if" statement. If constructions always look something like the following.
if expression do the tasks in here if expression was true only one of these branches is executed else do these tasks if the expression was false when the end is reached, we are done - no loop end
The expression can take many forms - examples in Matlab notation follow.
if x+5<10 if sin(i*x)==0 if n/5 <= 100