Numerical Simulation and Symbolic Math only.

Numerical Simulation and Symbolic Math utilize a Program Model that enables simulations using an expressive programming language of Maxima which is based on Lisp.

This article will cover how to create variables in the Program Model.

Contents

Variable Basics

The format of variables in the Program Model takes the form variable_name: expression;

Let's take the following question:

James has 2 apples. Amy has twice as many as James. How many apples does Amy have?

There are two variables that need to be set.

  • The number of apples James has (2)
  • The number of apples Amy has (James' value multiplied by 2)

In the Program Model, this can be entered as:

james_apples: 2;
amy_apples: james_apples * 2;

In this example, the variable james_appleswill always equal 2, and the variable amy_apples will always equal 4 ( the james_apples value multiplied by 2).

Randomise Variables with Functions

The power of the Program Model comes from the ability to randomly generate the value of the variables. Rather than have fixed values for each variable as above, you can achieve a random value by using one of many different functions available in MAXIMA.

random(n)

Replace n with a positive integer and the value generated with be anywhere from 0 up to but not including that number.

eg. james_apples: random(5); will result in the value being 0, 1, 2, 3 or 4

rand(n.0)

Replace n with a positive integer and a floating point will be generated between 0.0 and n.0.

eg. james_apples: rand(2.0); will result in the value being between 0.0000 and 2.0000.

rand([n0, n1, n3, n7])

This is a list inside square brackets. A random value from the list will be selected

eg. james_apples: rand([2,7,12,18]); will result in the value being either 2, 7, 12, or 18.

rand_with_step(lower, upper, step)

A random value will be selected between the lower and upper limits with the values between increasing by the set step interval.

eg. james_apples: rand_with_step(1,13,3); will result in the value being either 1, 4, 7, 10, or 13.

rand_with_prohib(lower, upper, [n0,n1..])

A random value will be generated from a set a number range between the lower and upper limits but with the values in square brackets being excluded.

eg. james_apples: rand_with_prohib(1,10,[2,3,7,8]); will result in the value being either 1, 4, 5, 6, 9, or 10.

Rounding

Functions like rand(n.0), or calculated results may produce values with many decimal places. You can round values using the decimalplaces(a, b)function where:

  • a = the value or variable
  • b = the number of decimal places to which a should be rounded.

eg. the following variable might result in the value 4.69831604812.

fruit_weight: rand(5.0);

To make it more manageable you could round to 2 decimal places either in one or two lines. Both options below yield the same result depending on whether you want a specific rounded variable or not.

fruit_weight: rand(5.0);
fruit_weight_rounded: decimalplaces(fruit_weight, 2);

or

fruit_weight: decimalplaces(rand(5.0), 2);

Calculations

Variables aren't just for randomly generating values. They can equal full equations. Equations written in MAXIMA, and therefore the Program Model, use infix notation which is a standard mathematical notation where operators are placed between the operands.

Common Operators

+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponentiation
() Parenthesis

 

For example here are 5 equations with their infix notation equivalents.

\(3x^2 + 2x - 5\) = 3x^2 + 2x - 5

\((y + 3) / 2 - 4y\) = (y + 3) / 2 - 4y

\((z + 2) * (z - 3)\) = (z + 2) * (z - 3)

\(2t * (t + 1)\) = 2*t * (t + 1)

\((x^2 - 1) / (x + 2)\) = (x^2 - 1) / (x + 2)

Any variable previously defined in the program model can also be included in the equation of another variable. Take the following three lines:

trainA: rand_with_step(30,100,1);

trainB: trainA + rand_with_step(5,35,1);

combined_speed: trainA + trainB;

The above sets trainA as a random value between 30 and 100. Next, trainB will be a value between 5 to 35 greater than trainA. The final variable, combined_speed is the sum of trainA and trainB.

Simplification

Note: This only relates to Symbolic Math.

By default, the program model simplifies expressions to make computations efficient. For example, an expression like 1+2 is automatically simplified to 3. However, this might change the order or format of terms in the expressions.

Turning Off Simplification

If you require the candidate's answer to maintain a specific format, such as 3*x + y instead of a mathematically equivalent form like y + 3*x, you should turn off simplification. This is done by including the following assignment statement in the Program Model:

simp: false;

It's important to be aware that this will effect all expressions in the Program Model and require them to be matched in the order they are written, without any automatic simplification.

Artikler i denne seksjonen

Var denne artikkelen nyttig?
2 av 2 syntes dette var nyttig