Difference between revisions of "OpenModelica/C2/Arrays-in-Modelica/English-timed"
From Script | Spoken-Tutorial
Jyotisolanki (Talk | contribs) (Created page with "{| border=1 ||''' Time ''' ||'''Narration''' |- || 00:01 || Welcome to the spoken tutorial on '''Arrays'''. |- || 00:05 || In this tutorial, we are going to learn: How to d...") |
Sandhya.np14 (Talk | contribs) |
||
Line 5: | Line 5: | ||
|- | |- | ||
|| 00:01 | || 00:01 | ||
− | || Welcome to the spoken tutorial on '''Arrays'''. | + | || Welcome to the '''spoken tutorial''' on '''Arrays'''. |
|- | |- | ||
|| 00:05 | || 00:05 | ||
− | || In this tutorial, we are going to learn: | + | || 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'''. | ||
|- | |- | ||
Line 17: | Line 21: | ||
|- | |- | ||
|| 00:26 | || 00:26 | ||
− | ||You may use any of the following operating | + | ||You may use any of the following '''operating system'''s to practice this tutorial. |
|- | |- | ||
||00:32 | ||00:32 | ||
− | || To understand and practice this tutorial | + | || To understand and practice this tutorial, you need knowledge of '''arrays''' in any programming language. |
|- | |- | ||
Line 33: | Line 37: | ||
|- | |- | ||
|| 00:53 | || 00:53 | ||
− | ||It has single index. | + | ||It has single '''index'''. |
|- | |- | ||
Line 45: | Line 49: | ||
|- | |- | ||
|| 01:05 | || 01:05 | ||
− | ||A '''vector''' can be constructed by including the | + | ||A '''vector''' can be constructed by including the '''element'''s in curly braces. |
|- | |- | ||
Line 53: | Line 57: | ||
|- | |- | ||
|| 01:19 | || 01:19 | ||
− | || To access the | + | || To access the '''element'''s of a '''vector''', it is necessary to understand '''indexing'''. |
|- | |- | ||
Line 61: | Line 65: | ||
|- | |- | ||
|| 01:29 | || 01:29 | ||
− | ||'''Vector indexing''' starts from '''1''' and | + | ||'''Vector indexing''' starts from '''1''' and indices must be integers. |
|- | |- | ||
Line 73: | Line 77: | ||
|- | |- | ||
|| 01:49 | || 01:49 | ||
− | ||We shall replace parameters '''a,b''' and '''c''' of '''polynomialEvaluator''' with a vector '''a'''. | + | ||We shall replace parameters '''a, b''' and '''c''' of '''polynomialEvaluator''' with a vector '''a'''. |
|- | |- | ||
|| 01:58 | || 01:58 | ||
− | || Please download and save all the files available on our '''Code Files''' link. | + | || Please download and '''save''' all the files available on our '''Code Files''' link. |
|- | |- | ||
Line 102: | Line 106: | ||
|- | |- | ||
||02:42 | ||02:42 | ||
− | || Now, to view them let me double click on each icon. | + | || Now, to view them, let me double click on each '''icon'''. |
|- | |- | ||
Line 130: | Line 134: | ||
|- | |- | ||
|| 03:23 | || 03:23 | ||
− | || Parameters '''a,b''' and '''c''' of '''polynomialEvaluator''' are replaced with a vector '''a'''. | + | || Parameters '''a, b''' and '''c''' of '''polynomialEvaluator''' are replaced with a vector '''a'''. |
|- | |- | ||
Line 142: | Line 146: | ||
|- | |- | ||
|| 03:42 | || 03:42 | ||
− | ||The elements are separated by a | + | ||The elements are separated by a comma. |
|- | |- | ||
|| 03:46 | || 03:46 | ||
− | || In the '''assignment''' statement the elements of vector '''a''' are accessed using their indices. | + | || In the '''assignment''' statement, the elements of vector '''a''' are accessed using their indices. |
|- | |- | ||
Line 166: | Line 170: | ||
|- | |- | ||
|| 04:16 | || 04:16 | ||
− | ||This class is similar to the '''functionTester''' class discussed in previous tutorial. | + | ||This '''class''' is similar to the '''functionTester''' class discussed in previous tutorial. |
|- | |- | ||
|| 04:24 | || 04:24 | ||
− | || '''z''' is a Real variable. | + | || '''z''' is a '''Real''' variable. |
|- | |- | ||
Line 182: | Line 186: | ||
|- | |- | ||
|| 04:40 | || 04:40 | ||
− | ||Now let me Simulate this class. | + | ||Now, let me '''Simulate''' this class. |
|- | |- | ||
Line 194: | Line 198: | ||
|- | |- | ||
|04:49 | |04:49 | ||
− | || Select '''z''' in the '''variables''' | + | || Select '''z''' in the '''variables browser'''. |
|- | |- | ||
Line 202: | Line 206: | ||
|- | |- | ||
|| 05:00 | || 05:00 | ||
− | ||This plot is the same as observed in the case of '''polynomialEvaluator''' function. | + | ||This '''plot''' is the same as observed in the case of '''polynomialEvaluator''' function. |
|- | |- | ||
|| 05:07 | || 05:07 | ||
− | ||Now let me de-select '''z''' and delete the result. | + | ||Now, let me de-select '''z''' and delete the result. |
|- | |- | ||
Line 230: | Line 234: | ||
|- | |- | ||
|| 05:34 | || 05:34 | ||
− | || To demonstrate how to use '''for''' loop let me go back to '''OMEdit'''. | + | || To demonstrate how to use '''for''' loop, let me go back to '''OMEdit'''. |
|- | |- | ||
Line 250: | Line 254: | ||
|- | |- | ||
|| 06:01 | || 06:01 | ||
− | || Firstly, ''' | + | || Firstly, '''comment''' the assignment statement for '''fx''' by inserting double slash at the beginning and end. |
|- | |- | ||
Line 266: | Line 270: | ||
|- | |- | ||
|| 06:29 | || 06:29 | ||
− | ||Windows users may use '''notepad''' or any other text editor to open it. | + | ||'''Windows''' users may use '''notepad''' or any other text editor to open it. |
|- | |- | ||
Line 286: | Line 290: | ||
|- | |- | ||
|| 06:53 | || 06:53 | ||
− | ||Save this function by pressing '''Ctrl + S'''. | + | ||'''Save''' this function by pressing '''Ctrl + S'''. |
|- | |- | ||
Line 294: | Line 298: | ||
|- | |- | ||
|| 07:02 | || 07:02 | ||
− | ||This statement assigns '''fx '''an initial value of zero before the loop starts. | + | ||This statement assigns '''fx '''an initial value of zero, before the loop starts. |
|- | |- | ||
Line 302: | Line 306: | ||
|- | |- | ||
|| 07:12 | || 07:12 | ||
− | ||The loop | + | ||The loop '''run'''s until value of '''i''' is '''3'''. |
|- | |- | ||
Line 330: | Line 334: | ||
|- | |- | ||
|| 07:44 | || 07:44 | ||
− | ||To test this function let us use the class '''functionTester'''. | + | ||To test this function, let us use the class '''functionTester'''. |
|- | |- | ||
|| 07:49 | || 07:49 | ||
− | ||I have made no changes to this function to this '''class'''. | + | ||I have made no changes to this function, to this '''class'''. |
|- | |- | ||
|| 07:54 | || 07:54 | ||
− | || | + | || Let me simulate this class by pressing '''Simulate''' button in the toolbar. |
|- | |- | ||
Line 346: | Line 350: | ||
|- | |- | ||
|| 08:03 | || 08:03 | ||
− | ||Note that the value of '''z''' remains the same after changes are made to the function. | + | ||Note that the value of '''z''' remains the same after changes are made to the '''function'''. |
|- | |- | ||
Line 358: | Line 362: | ||
|- | |- | ||
|| 08:17 | || 08:17 | ||
− | || Now let me go back to the | + | || Now let me go back to the '''slide'''s once again. |
|- | |- | ||
Line 390: | Line 394: | ||
|- | |- | ||
|| 08:55 | || 08:55 | ||
− | || To | + | || To understand more about '''array''' construction and '''indexing'''- |
|- | |- | ||
Line 450: | Line 454: | ||
|- | |- | ||
|| 10:11 | || 10:11 | ||
− | ||Hence, the size of second dimension of myMatrix is '''2'''. | + | ||Hence, the size of second dimension of '''myMatrix''' is '''2'''. |
|- | |- | ||
Line 462: | Line 466: | ||
|- | |- | ||
|| 10:29 | || 10:29 | ||
− | || To add | + | || To add these two arrays or matrices, we need to access elements from two dimensions. |
|- | |- | ||
|| 10:35 | || 10:35 | ||
− | ||Hence | + | ||Hence a '''nested for''' loop is required. |
|- | |- | ||
Line 474: | Line 478: | ||
|- | |- | ||
|| 10:44 | || 10:44 | ||
− | || Similarly this '''for''' loop runs through the second dimension. | + | || Similarly, this '''for''' loop runs through the second dimension. |
|- | |- | ||
Line 486: | Line 490: | ||
|- | |- | ||
|| 11:00 | || 11:00 | ||
− | || | + | || These statements represent the end of each '''for''' loop. The '''class''' is now complete. |
|- | |- | ||
Line 502: | Line 506: | ||
|- | |- | ||
|| 11:18 | || 11:18 | ||
− | ||Select '''adder[1,1]''', '''myMatrix[1,1]''' | + | ||Select '''adder[1,1]''', '''myMatrix[1,1]''' and '''mySum[1,1]'''. |
|- | |- | ||
Line 522: | Line 526: | ||
|- | |- | ||
|| 11:51 | || 11:51 | ||
− | ||Similarly write a function '''matrixReversal''' to reverse the order of elements in each row of a matrix. | + | ||Similarly, write a function '''matrixReversal''' to reverse the order of elements in each row of a matrix. |
|- | |- | ||
Line 534: | Line 538: | ||
|- | |- | ||
|| 12:09 | || 12:09 | ||
− | || Please watch the video available at following link: | + | || 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 | || 12:15 | ||
− | || We | + | || We conduct workshops using spoken tutorials, give certificates. Please contact us. |
|- | |- | ||
Line 558: | Line 563: | ||
|- | |- | ||
|| 12:48 | || 12:48 | ||
− | || Spoken Tutorial | + | || Spoken Tutorial project is funded by '''NMEICT, MHRD''', Government of India. |
|- | |- |
Revision as of 14:31, 20 August 2018
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. |