Note: You are currently viewing documentation for Moodle 3.3. Up-to-date documentation for the latest stable version of Moodle is probably available here: Formulas: Variables.

Formulas: Variables: Difference between revisions

From MoodleDocs
(Redirected page to Formulas question type)
 
(20 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Formulas question type}}
#REDIRECT [[Formulas_question_type]]
 
Variables can be used to substitute question texts, define the answers and specify grading criteria. One of the main purpose of the system of variables is to simplify the task to make variation of the questions. Since the variables are generated at the beginning of a quiz, the quiz will be halted if any errors occur. Hence, in order to minimize errors, the system is designed to only have deterministic variables type, constant length lists and no branching.
 
==Scope of variables==
There are four types of variables (random, global, local and grading) which you can define and manipulate in the following fields:
 
{| class="nicetable"
|-
! Field
! Scope of application
|-
| Random variables
| The place to define the variation of variables for the whole question.
|-
| Global variables
| The scope includes all (instantiated) random variables.
|-
| Local variables
| The scope includes all global variables. Note: Each subquestion has its own local variables scope.
|-
| Grading variables
| The scope includes all local variables, plus special variables (e.g. _0, _1 ) depending on students' response.
|}
 
Furthermore, you can use the four types of variables in the following fields:
 
{| class="nicetable"
|-
! Field
! Scope of application
|-
| Main text
| All global variables can be used in substitution.
|-
| Subquestion text
| All local variables for the part can be used in substitution.
|-
| Answer
| All local variables for the part can be used in the expression.
|-
| Grading criteria
| All grading variables can be used in the expression.
|}
 
==Name of variables==
 
A variable name is a string of alphanumeric characters (<span style="font-family:monospace;font-size:113%;">a-z</span>, <span style="font-family:monospace;font-size:113%;">A-Z</span>, <span style="font-family:monospace;font-size:113%;">0-9</span> and <span style="font-family:monospace;font-size:113%;">_</span>) that cannot start with a number or the underscore. Examples of valid variable names are:
 
<pre style="font-size:113%;">
x, y1, z_1, foo_bar, myFirstVariable
</pre>
 
Variable names are case-sensitive so that, for example, the following names denote four different variables:
 
<pre style="font-size:113%;">
foobar, FooBar, fooBar, FOOBAR
</pre>
 
==Types of variables==
 
Each variable will be implicitly assigned with a type. The type is either ''number'', ''string'' or ''algebraic variable'', or ''list of numbers'' or ''list of strings''. A list is defined as elements enclosed by [ and ]. An element in a list can be referred to using the syntax A[b], for example [4,5,6][0] gives the first number, that is, 4. These types are listed below:
 
{| class="nicetable"
|-
! Type
! Description
|-
| Number
| A number, for example 1.2e-3.
|-
| String
| Characters enclosed by two double quotes, for example "Hello".
|-
| List of numbers
| Numbers enclosed by [ and ], for example [4,5,6]. The equivalent short syntax is [4:7] or [4:7:1].
|-
| List of strings
| Strings enclosed by [ and ], for example ["A", "B", "C"]
|-
| Algebraic variable
| It is simply a set of numbers (see syntax) defined in the non-random variables scopes, for example: {1:100}.
|}
 
==Random variables==
 
During the quiz creation, each random variable will be assigned by ONE value defined by the expression. Hence, each student can have their own set of values for the quiz attempt.
 
A random variable has different syntax than other variables and it is defined only in the '''Random variables''' field. These variables can be defined by assigning a set of elements or a shuffling of a list. The probability of selecting each element is equal so that each element has the same chance of being drawn. There are three main types of expressions:
===Set of elements===
A set of elements consists of elements delimited by { and }. Elements can be numbers, strings, lists of numbers, or lists of strings. For example, the variable below is a set of list of numbers, and the probability for element [2,4] is 1/5:
 
F = {[0,0], [1,1], [2,4], [3,9], [4,16]};
 
===Set of numbers===
A set of numbers consists of numbers delimited by {and}. You can also specify a range in the format of {start:stop:interval}, where the numbers satisfying (start + n*interval) < stop, n = 0,1,2,3,..., will be generated. If the interval is not specified, value 1 will be used. Note that the stop value is not included by definition, but may be included for non-integers due to numerical errors. The sets of numbers defined below demonstrate different range syntaxes. The probabilities of the elements in each set are 1/8, 1/10 and 1/20 respectively.
 
A = {1,2,3,4,5,6,7,8};
 
B = {0:1:0.1};
 
C = {1:10, 10:100:10, 100, 200};
 
===Shuffled list===
A list can be passed to the shuffle function. In the example below, the instantiated variable <span style="font-family:monospace;font-size:113%;">S</span> will be one of the permutations of the input list, say [4,3,2,5] or [2,5,4,2]. The probability of each list is therefore 1/4! = 1/24.
 
S = shuffle([2,3,4,5]);
 
==Variable assignments==
 
Variable assignments allow you to define and manipulate variables. It can be defined in the field of global variables, local variables and grading variables.
 
{| class="nicetable"
|-
! Component
! Definition
|-
| Expression
| Any combination of numbers and variables together with operators and functions listed herein ('''[[Formulas:_Operators|operators]]''',  '''[[Formulas:_Numerical_functions|numerical functions]]''' and '''[[Formulas:_Non-numerical_functions|non-numerical_functions]]'''). Typically, it is just a simple mathematical formula evaluated to a number. The variables used in the expression must be defined before.
|-
| Assignment
| Used to assign the evaluated result of an expression to a variable, in the form of <span style="font-family:monospace;font-size:113%;">name = expression;</span>.
|-
| For loop
| Allows a simple iteration in the form of 'for(element:list)'. All elements in the list are iterated.
|}
 
Examples of assignments are shown below:
 
{| class="nicetable"
|-
! Example
! Comments
|-
| a = 1;
| # text after "#" and the end of line are treated as comment
|-
| b = exp(3.333);
|
|-
|d = round(b, 1);
| # the function round() can round the number to the desired decimal
|-
| e = 1 + sin(2) + pow(a,2);
|
|-
| A = [1,2,3];
|
|-
| x = A[0];
|
|-
| y = A[a];
|
|-
| z = A[2];
|
|-
| w = A[0] + A[1] + A[2];
|
|-
| m = max(x, y);
|
|-
| distance = sqrt(x*x + y*y + z*z);
|
|-
| theta = atan2(y, x);
|
|-
| smaller = x < y;
| # smaller is 1
|-
| con = (x < y) + (y < z);
| # con is 2
|-
| B = fill(3,0);
| # B is now [0,0,0]
|-
| C = map("sqrt",fill(3,16));
| # square root of [16,16,16], so C is now [4,4,4]
|-
| s = 0;
|
|-
| for (i:A)  s = s + i;
| # s is 6 after the loop
|-
| for (i:[0:3]) {
    B[i] = sum(map("+",A,fill(3,i)));
 
   }
| # B will be [6,9,12]
|-
| p = pick(a+9,"","A","B");
| # pick() always choose the first element if index out of range
|-
| u = {-3,-2,-1,1:100};
| # u and v define algebraic variables, the numbers are the points for evaluation
|-
| v = {-100:100:1};
|
|}
 
Notes:
<ul>
<li>Important! Index out of range cannot be checked by the validation during question save, so you have to check it yourself. Otherwise, the quiz initialization for some students may fail. Use pick() as a safe variant if applicable.</li>
<li>Logical true is treated as 1 and false is 0, so the variable con above gets the value 2.</li>
<li>There is no branching, however, you may use the ternary operator (condition) ? (true) : (false) for numbers or the pick() function for general case.</li>
<li>The available functions are listed in the Appendix. There are many additional functions for the numeric list operation, and a few for the list of strings.</li>
</ul>
 
 
[[#top|Back to top of page]]
 
[[Formulas_question_type|← FORMULAS QUESTION TYPE]]
 
===In other languages===
[https://docs.moodle.org/all/es/F%C3%B3rmulas:_Variables Español]<br>
[https://docs.moodle.org/3x/fr/Formules:_Variables Français]

Latest revision as of 00:58, 1 February 2018