OpenModelica/C2/Arrays-in-Modelica/English-timed
From Script | Spoken-Tutorial
| Time | Narration |
| 00:01 | Welcome to the spoken tutorial on Arrays. |
| 00:05 | In this tutorial, we are going to learn:
how to declare array variables, how to construct arrays, how to use for and while loops and how to use OMShell. |
| 00:20 | To record this tutorial, I am using: OpenModelica 1.9.2 |
| 00:26 | You may use any of the following operating systems to practice this tutorial. |
| 00:32 | To understand and practice this tutorial, you need knowledge of arrays in any programming language. |
| 00:40 | You need to know how to define a class in Modelica. Prerequisite tutorials are mentioned on our website. Please go through them. |
| 00:50 | Vector is a one dimensional array. |
| 00:53 | It has single index. |
| 00:55 | Syntax for vector declaration is as shown. |
| 00:50 | The example shown declares a vector variable a whose size is 2. |
| 01:05 | A vector can be constructed by including the elements in curly braces. |
| 01:11 | This example defines a vector parameter a with 2 and 3 as its elements. |
| 01:19 | To access the elements of a vector, it is necessary to understand indexing. |
| 01:25 | Syntax for vector indexing is as shown. |
| 01:29 | Vector indexing starts from 1 and indices must be integers. |
| 01:35 | Let us develop a function named polynomialEvaluatorUsingVectors. |
| 01:41 | This function is an extension of polynomialEvaluator function which was discussed in previous tutorials. |
| 01:49 | We shall replace parameters a, b and c of polynomialEvaluator with a vector a. |
| 01:58 | Please download and save all the files available on our Code Files link. |
| 02:05 | For your convenience, polynomialEvaluator function is also made available. |
| 02:12 | Now let me switch to OMEdit to demonstrate this function. |
| 02:17 | OMEdit is now open in Welcome perspective. |
| 02:21 | I have opened all the necessary files. |
| 02:25 | Note that the following classes or functions are now open in OMEdit: functionTester,
matrixAdder, polynomialEvaluator and polynomialEvaluatorUsingVectors. |
| 02:42 | Now, to view them, let me double click on each icon. |
| 02:49 | Let me shift the OMEdit window to the left for better visibility. |
| 02:56 | Go to polynomialEvaluator tab. |
| 03:00 | Open it in Text View. |
| 03:03 | For more information on this function, refer to the previous tutorials. |
| 03:09 | Let me go to polynomialEvaluatorUsingVectors. Open it in Text View. |
| 03:16 | Input and output variables are the same as in polynomialEvaluator function. |
| 03:23 | Parameters a, b and c of polynomialEvaluator are replaced with a vector a. |
| 03:32 | Size of this vector is 3. |
| 03:36 | The elements of this vector are included in curly braces as shown. |
| 03:42 | The elements are separated by a comma. |
| 03:46 | In the assignment statement, the elements of vector a are accessed using their indices. |
| 03:54 | a[1] is the first element of vector a. |
| 03:59 | Similarly, the second element and third element of vector a have been accessed as well. |
| 04:08 | Now, let me switch to functionTester tab. |
| 04:13 | Open it in Text View. |
| 04:16 | This class is similar to the functionTester class discussed in previous tutorial. |
| 04:24 | z is a Real variable. |
| 04:27 | polynomialEvaluatorUsingVectors function is called with an input argument of 10 units. |
| 04:35 | The value returned by this function is equated to z. |
| 04:40 | Now, let me Simulate this class. |
| 04:43 | Click on Simulate button. |
| 04:46 | Close the pop up window. |
| 04:49 | Select z in the variables browser. |
| 04:53 | Note that the value of z is equal to f(x) at x = 10. |
| 05:00 | This plot is the same as observed in the case of polynomialEvaluator function. |
| 05:07 | Now, let me de-select z and delete the result. |
| 05:13 | Go back to Modeling perspective |
| 05:16 | Now let me switch to the slides. |
| 05:19 | for loop is used to iterate statements a given number of times. |
| 05:24 | It can be used in algorithm and equation sections. |
| 05:29 | Syntax for for loop is as shown with an example. |
| 05:34 | To demonstrate how to use for loop, let me go back to OMEdit. |
| 05:40 | Click on polynomialEvaluatorUsingVectors tab. |
| 05:45 | In the assignment statement for fx, we are accessing the elements of vector a. |
| 05:52 | This can also be done using a for loop. |
| 05:55 | Now, let us see how to include a for loop in the algorithm section. |
| 06:01 | Firstly, comment the assignment statement for fx by inserting double slash at the beginning and end. |
| 06:10 | Save this function by pressing Ctrl+S. |
| 06:15 | The for loop to be inserted has been provided in a text file named for-loop.txt. |
| 06:23 | It is available on our website. I have opened this file using gedit. |
| 06:29 | Windows users may use notepad or any other text editor to open it. |
| 06:35 | Let me go to gedit. |
| 06:38 | Copy all the statements by pressing Ctrl+C. |
| 06:44 | Go back to OMEdit |
| 06:46 | Press Enter. Paste all the statements by pressing Ctrl + V. |
| 06:53 | Save this function by pressing Ctrl + S. |
| 06:57 | Now, let me explain each statement of this loop. |
| 07:02 | This statement assigns fx an initial value of zero, before the loop starts. |
| 07:09 | Here, i serves as a loop counter. |
| 07:12 | The loop runs until value of i is 3. |
| 07:16 | It is not necessary to declare i before it is used. |
| 07:21 | Let me scroll down a bit. |
| 07:24 | This statement iteratively adds terms of the polynomial f(x). |
| 07:30 | Polynomial f(x) has been discussed while discussing polynomialEvaluator function. |
| 07:37 | This statement indicates the end of for loop. |
| 07:41 | Now, this function is complete. |
| 07:44 | To test this function, let us use the class functionTester. |
| 07:49 | I have made no changes to this function, to this class. |
| 07:54 | Let me simulate this class by pressing Simulate button in the toolbar. |
| 07:49 | Select z in variables browser. |
| 08:03 | Note that the value of z remains the same after changes are made to the function. |
| 08:10 | Let me de-select z and delete the result. |
| 08:14 | Go back to Modeling perspective |
| 08:17 | Now let me go back to the slides once again. |
| 08:21 | while loop is used to iterate statements until a given condition is satisfied. |
| 08:27 | while loop cannot be used in equation section. |
| 08:31 | for loop is more frequently used in Modelica as compared to while. |
| 08:37 | Let us discuss Arrays now. |
| 08:40 | Arrays are used to represent multi-dimensional data. |
| 08:44 | They can be constructed using vector notation. |
| 08:48 | Syntax for array declaration and indexing is as shown. |
| 08:55 | To understand more about array construction and indexing- |
| 09:00 | Let us write a class named matrixAdder which adds myMatrix and adder matrices to give mySum. myMatrix and adder matrices are as shown. |
| 09:14 | Now, let me switch to OMEdit to demonstrate matrixAdder class. |
| 09:19 | It is already open in OMEdit. |
| 09:23 | Click on matrixAdder tab. |
| 09:26 | Open it in Text view. |
| 09:29 | myMatrix is a Real parameter array. |
| 09:33 | Numbers in square bracket represent size of this array. |
| 09:39 | The size of first dimension is 3. |
| 09:42 | Similarly, the size of second dimension is 2. |
| 09:46 | myMatrix array is constructed using three vectors of two elements each. |
| 09:53 | {1,2} represents the first vector. |
| 09:57 | {3,4} is the second one and |
| 10:00 | {5,6}} represents the third vector. |
| 10:04 | Size of each of this vectors is equal to size of second dimension of this array. |
| 10:11 | Hence, the size of second dimension of myMatrix is 2. |
| 10:16 | The number of vectors is equal to the size of first dimension. Hence, the size of first dimension is equal to 3. |
| 10:25 | adder matrix is constructed in a similar fashion. |
| 10:29 | To add these two arrays or matrices, we need to access elements from two dimensions. |
| 10:35 | Hence a nested for loop is required. |
| 10:40 | This for loop runs through the first dimension. |
| 10:44 | Similarly, this for loop runs through the second dimension. |
| 10:49 | Let me scroll down a bit. |
| 10:52 | Corresponding elements of myMatrix and adder matrices are added to yield mySum |
| 11:00 | These statements represent the end of each for loop. The class is now complete. |
| 11:07 | Let me simulate it by clicking on Simulate button. |
| 11:11 | Close the pop up window if it appears. |
| 11:15 | Let me expand variables column. |
| 11:18 | Select adder[1,1], myMatrix[1,1] and mySum[1,1]. |
| 11:25 | Note that adder[1,1] plus myMatrix[1,1] gives mySum[1,1] which means that the result is accurate. |
| 11:35 | Let me de-select them and delete the result. |
| 11:40 | Let me go back to the slides. |
| 11:43 | As an assignment: Write a function named vectorReversal to reverse the order of elements in a vector. |
| 11:51 | Similarly, write a function matrixReversal to reverse the order of elements in each row of a matrix. |
| 12:00 | Write functionTester class to test these two functions. |
| 12:05 | This brings us to the end of this tutorial. |
| 12:09 | Please watch the video available at following link:
http://spoken-tutorial.org/What_is_a_Spoken_Tutorial It summarizes the Spoken Tutorial project. |
| 12:15 | We conduct workshops using spoken tutorials, give certificates. Please contact us. |
| 12:21 | If you have questions related to this spoken tutorial, please visit the following website. |
| 12:28 | We coordinate coding of solved examples of popular books. |
| 12:33 | We give honorarium and certificates to those who do this. Please visit the following website. |
| 12:39 | We help migrate commercial simulator labs to OpenModelica. Please visit the following website for more information. |
| 12:48 | Spoken Tutorial project is funded by NMEICT, MHRD, Government of India. |
| 12:55 | We thank the development team of OpenModelica for their support. |
| 13:00 | Thank you for joining me in this tutorial. |