1 / 84

Maxima program control

Maxima program control. Topics. Branching Iteration. Branching. Branching controls the execution of a program. Statements in a program are executed only if a condition holds. An informal example, not quite Maxima: if (gender is “male”) then use men’s room else use women’s room.

Download Presentation

Maxima program control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Maxima program control

  2. Topics • Branching • Iteration

  3. Branching

  4. Branching controls the execution of a program • Statements in a program are executed only if a condition holds. • An informal example, not quite Maxima: if (gender is “male”) then use men’s room else use women’s room

  5. Branching controls the execution of a program • Statements in a program are executed only if a condition holds. • Informal example, not quite Maxima: if ( gender is male ) then use men’s room else use women’s room Condition

  6. A formal Maxima example if (gender = male) then print ("use men’s room") else print ("use women’s room"); • Statement also uses the Maxima print function

  7. Question – how does Maxima know the gender of the programmer?

  8. Question – how does Maxima know the gender of the programmer? • The value had to have been initialized first. gender: male; • We now think of sequences of Maxima instructions forming Maxima programs.

  9. formal example has 3 words reserved in Maxima if (gender is male) then print ("use men’s room") else print ("use women’s room");

  10. Another example if (flag = 1) then plot2d(f(x), [x, 1, 100]); else plot2d(g(x), [x, 1, 1000]);

  11. Functions and if statements • They can be combined: f(x):= if (x < 0) then 0 else if (0 <= x) and (x < 1) then x else 1

  12. Functions and if statements • They can be combined: f(x):= if (x < 0) then 0 else if (0 <= x) and (x < 1) then x else 1 Argument less than 0 Argument between 0 and 1 Any other value of the argument

  13. What do we have to know about branches? • What condition determines the choice of branch we want our program to execute? • What happens in each branch? • What is the value of the logical condition that determines the branch we take • Must know this before we start coding • The syntax of an if-else statement

  14. Formal syntax • if cond_1 then expr_1 else expr_0 • The value of this expression: • evaluates to expr_1 if cond_1 evaluates to true • otherwise the expression evaluates to expr_0.

  15. The condition • Must be able to be evaluated to either true or false. • Uses operators for comparing things • In nearly all situations, operators are one or more of • Relational operators • Logical operators

  16. These are tricky – discuss later

  17. Some uses are obvious if (1 < 2) then print (1) else print("I love this course"); Output is 1 = 1

  18. Some uses are obvious if (1 >= 2) then print (1) else print("I love this course"); Output is I love this course

  19. if ((1 < 2) and (2 < 3)) then print(1) else print("I love this course"); Output is

  20. Less obvious if ((1 < 2) and (2 < 3)) then print(1) else print("I love this course"); Output is 1 = 1 Note the use of parentheses

  21. if ((1 < 2) and (2 > 3)) then print(1) else print("I love this course"); Output is

  22. if ((1 < 2) and (2 > 3)) then print(1) else print("I love this course"); Output is I love this course

  23. Let’s go back to the equality relational operators • =, # • equal, notequal • = and # are by far the most common • Even they do not quite behave the way you think • We will avoid equal and notequal as much as we can

  24. Examples of the use of = if (1 = 1) then print("use men’s room") else print("use women’s room"); Output: use men’s room

  25. if (1 = 2-1) then print ("use men’s room") else ("use women’s room"); Output: use men’s room

  26. if (1 = (2 + 4 + 6)/(6 + 4 + 2)*(2-1)) then ("use men’s room") else ("use women’s room"); Output: use men’s room

  27. Unfortunately, Maxima doesn’t really understand = for everything if (1 = sin(x)^2 + cos(x)^2) then print("use men’s room") else print("use women’s room"); Output: use women’s room

  28. More complex if-else statements if cond_1 then expr_1 elseif cond_2 then expr_2 elseif ... else expr_0 evaluates to expr_k if cond_k is true and all preceding conditions are false. • If none of the conditions are true, the expression evaluates to expr_0.

  29. if (avg < 60) then print("F") elseif ((60 <= avg) and (avg < 70)) then print("D") elseif ((70 <= avg) and (avg < 80)) then print("C") elseif ((80 <= avg) and (avg < 90)) then print("B") else print(A);

  30. Thing to note • All cases are covered • Parentheses were used to delimit every input to a relational operation such as and • Code was indented consistently to make it more readable

  31. There are shortcuts – you can omit the ending else if cond_1 then expr_1 is equivalent to if cond_1 then expr_1 else false

  32. The same holds for elseif statements • Try this one yourself

  33. What happens if we omit the else? if (1 = (2 + 4 + 6)/(6 + 4 + 2)*(2-1)) then print("use men’s room"); Output is use men’s room Note that the condition in the parentheses evaluates totrue.

  34. if (1 = (2 + 4 + 6)/(6 + 4 + 2) * 2) then print("use men’s room"); Output is false Note that the condition in the parentheses evaluates tofalse. The phrase “use men’s room” never gets printed.

  35. Iteration

  36. We have already used one form of iteration: for i: 0 step 1 thru 9 do B[i] : 3 * i ;

  37. Iteration in Maxima The idea: • Identify statements you want to repeat • Start the iteration, using an iterator • Choose when to start • Choose where to end • Choose how large a step you make It’s just like walking!

  38. This can be simplified for i: 0 thru 9 do B[i] : 3 * i ; • The step can be omitted – the step value defaults to 1 if omitted. • Don’t omit the step if any other increment is intended.

  39. So far, all iterations just used loop control variables (usually indicies) • We can do more – accumulate information • Example: Find the sum of the integers from 1 to 100.

  40. Approach (by hand) • Add the second to the first, • then the third to that sum • Then the fourth to that sum, • …

  41. Approach on a calculator • Clear the memory • Add the first • Add the second • Add the third • …

  42. The Maxima approach follows the calculator model sum:0; for i: 1 thru 100 do sum : sum + i ; print(sum);

  43. Comments show how the program works sum:0; /* initialize */ for i: 1 thru 100 do sum : sum + i ; /* accumulate */ print(sum); /* output */ 5051

  44. Another example sum_of_squares:0; /* initialize */ for i: 1 thru 100 do sum_of_squares : sum_of_squares + i * i; /* accumulate */ print(sum_of_squares); /* output */ 338350

  45. Probably better to use shorter name sum:0; /* initialize */ for i: 1 thru 100 do sum : sum + i *i ; /* accumulate */ print(sum); /* output */

  46. Can find averages of arrays array(B, 20); for i: 0 thru 99 do B[i] : sin(i * %pi/4); /* init */ B[17]; 1 ------- sqrt(2) avg: 0; sum :0; for i :0 step 1 thru 99 do sum : sum + B[i]; print(sum, avg);

  47. There are 3 forms of iteration • A do-loop which follows the format we have seen so far • A do-loop where the ending condition depends on some condition continues to hold. • A do-loop where the loop continues to execute unless a condition is true.

  48. There are 3 forms of iteration • for variable: initial_value step increment thru limit do body (we have done this one many times already)

More Related