Difference between revisions of "OpenModelica/C2/Arrays-in-Modelica/English"
(Created page with "{| border=1 ||'''Visual Cue''' ||'''Narration''' |- || Slide: '''Title Slide''' || Welcome to the spoken tutorial on '''Arrays''' as a part of the series on OpenModelica |-...") |
|||
Line 7: | Line 7: | ||
'''Title Slide''' | '''Title Slide''' | ||
− | || Welcome to the spoken tutorial on '''Arrays''' | + | || Welcome to the spoken tutorial on '''Arrays'''. |
|- | |- | ||
Line 15: | Line 15: | ||
|| In this tutorial, we are going to learn: | || In this tutorial, we are going to learn: | ||
− | * How to declare ''' | + | * How to declare '''array''' variables |
* How to construct '''arrays''' | * How to construct '''arrays''' | ||
− | * How to use '''for''' and '''while''' loops | + | * How to use '''for''' and '''while''' loops and |
* How to use '''OMShell''' | * How to use '''OMShell''' | ||
Line 26: | Line 26: | ||
|| To record this tutorial, I am using: | || To record this tutorial, I am using: | ||
− | * '''OpenModelica 1.9.2''' | + | * '''OpenModelica 1.9.2''' |
− | + | You may use any of the following operating systems to practice this tutorial. | |
|- | |- | ||
Line 34: | Line 34: | ||
Prerequisites | Prerequisites | ||
− | || To understand and practice this tutorial, | + | || To understand and practice this tutorial, |
− | * | + | * you need knowledge of arrays in any programming language |
− | * | + | * You need to know how to define a class in Modelica |
+ | |||
+ | Prerequisite tutorials are mentioned on our website. | ||
+ | |||
+ | Please go through them. | ||
|- | |- | ||
Line 47: | Line 51: | ||
It has single index | It has single index | ||
− | Syntax for vector declaration is as shown | + | Syntax for vector declaration is as shown. |
− | + | The example shown declares a vector variable '''a''' whose size is '''2'''. | |
− | This example defines vector '''a''' with '''2''' and '''3''' as its elements. | + | A vector can be constructed by including the elements in curly braces. |
+ | |||
+ | This example defines a vector parameter '''a''' with '''2''' and '''3''' as its elements. | ||
|- | |- | ||
|| Slide: | || Slide: | ||
− | ''' | + | '''Indexing vector elements''' |
|| To access the elements of a vector, it is necessary to understand indexing. | || To access the elements of a vector, it is necessary to understand indexing. | ||
− | + | Syntax for vector indexing is as shown. | |
− | Vector indexing starts from '''1''' | + | Vector indexing starts from '''1''' and |
Indices must be integers. | Indices must be integers. | ||
Line 69: | Line 75: | ||
|| Let us develop a function named '''polynomialEvaluatorUsingVectors'''. | || Let us develop a function named '''polynomialEvaluatorUsingVectors'''. | ||
− | This function is an extension of '''polynomialEvaluator''' function discussed in previous tutorials. | + | This function is an extension of '''polynomialEvaluator''' function which was discussed in previous tutorials. |
− | We shall replace '''a,b | + | We shall replace parameters '''a,b''' and '''c''' of '''polynomialEvaluator''' with a vector '''a'''. |
|- | |- | ||
|| | || | ||
− | || Please download and save all the | + | || Please download and save all the files available on our Code Files link. |
For your convenience, '''polynomialEvaluator''' function is also made available. | For your convenience, '''polynomialEvaluator''' function is also made available. | ||
Line 81: | Line 87: | ||
|- | |- | ||
|| /* Switch to OMEdit */ | || /* Switch to OMEdit */ | ||
− | || | + | || Now let me switch to '''OMEdit''' to demonstrate this function. |
+ | |||
+ | OMEdit is now open in Welcome perspective. | ||
− | I have opened the necessary files. | + | I have opened all the necessary files. |
− | Note that the following classes are now open in OMEdit: '''functionTester''', ''' | + | Note that the following classes or functions are now open in OMEdit: '''functionTester''', '''matrixAdder''', '''polynomialEvaluator''' and '''polynomialEvaluatorUsingVectors'''. |
|- | |- | ||
Line 96: | Line 104: | ||
'''protected parameter Real c = 1;''' | '''protected parameter Real c = 1;''' | ||
− | || Now, let me | + | || Now, to view them let me double click on each icon. |
− | + | Let me shift the OMEdit window to the left for better visibility. | |
− | For more information on this function, | + | Go to '''polynomialEvaluator''' tab. |
+ | |||
+ | Open it in '''Text View'''. | ||
+ | |||
+ | For more information on this function, refer to the previous tutorials. | ||
|- | |- | ||
|| | || | ||
− | || | + | || Let me go to '''polynomialEvaluatorUsingVectors'''. |
+ | |||
+ | Open it in '''Text View'''. | ||
|- | |- | ||
Line 113: | Line 127: | ||
'''output Real fx;''' | '''output Real fx;''' | ||
− | || Input and output variables are the same as '''polynomialEvaluator''' function. | + | || Input and output variables are the same as in '''polynomialEvaluator''' function. |
|- | |- | ||
|| '''protected parameter Real a[3]''' = {1,2,1}; | || '''protected parameter Real a[3]''' = {1,2,1}; | ||
− | || Parameters '''a,b''' and '''c''' | + | || Parameters '''a,b''' and '''c''' of polynomialEvaluator are replaced with a vector '''a'''. |
|- | |- | ||
|| protected parameter '''Real a[3] = {1,2,1}'''; | || protected parameter '''Real a[3] = {1,2,1}'''; | ||
− | || | + | || Size of this vector is '''3'''. |
|- | |- | ||
|| protected parameter '''Real a[3] = {1,2,1}'''; | || protected parameter '''Real a[3] = {1,2,1}'''; | ||
− | || | + | || The elements of this vectors are included in curly braces as shown. |
+ | |||
+ | The elements are separated by a comma. | ||
|- | |- | ||
Line 139: | Line 155: | ||
fx := '''a[1]'''<nowiki>*x^2 + a[2]*x + a[3];</nowiki> | fx := '''a[1]'''<nowiki>*x^2 + a[2]*x + a[3];</nowiki> | ||
− | || | + | || In the assignment statement the elements of vector '''a''' are accessed using their indices. |
− | + | a[1] is the first element of vector a. | |
|- | |- | ||
|| fx := a[1]*x^2 + '''a[2]*x + a[3];''' | || fx := a[1]*x^2 + '''a[2]*x + a[3];''' | ||
− | || Similarly, second and third | + | || Similarly, the second element and third element of vector '''a''' have been accessed as well. |
|- | |- | ||
|| | || | ||
− | || | + | || Now, let me switch to '''functionTester''' tab. |
− | + | Open it in '''Text View'''. | |
− | + | ||
− | + | ||
− | + | This class is similar to the '''functionTester''' class discussed in previous tutorial. | |
− | + | ||
− | + | ||
|- | |- | ||
Line 167: | Line 179: | ||
|- | |- | ||
|| '''z = polynomialEvaluatorUsingVectors(10);''' | || '''z = polynomialEvaluatorUsingVectors(10);''' | ||
− | || '''polynomialEvaluatorUsingVectors''' function is called | + | || '''polynomialEvaluatorUsingVectors''' function is called with an input argument of '''10''' units. |
|- | |- | ||
|| '''z = polynomialEvaluatorUsingVectors(10);''' | || '''z = polynomialEvaluatorUsingVectors(10);''' | ||
− | || | + | || The value returned by this function is equated to z. |
+ | |||
+ | Now let me Simulate this class. | ||
|- | |- | ||
|| Click on '''Simulate''' button | || Click on '''Simulate''' button | ||
− | || Click on '''Simulate''' button | + | || Click on '''Simulate''' button. |
+ | |||
+ | Close the pop up window. | ||
|- | |- | ||
|| /* Plotting perspective */ | || /* Plotting perspective */ | ||
− | + | Select '''z''' in the variables browser | |
+ | |||
|| '''functionTester''' class is simulated. | || '''functionTester''' class is simulated. | ||
− | + | Select '''z''' in the variables browser. | |
− | + | Note that the value of z is equal to '''f(x)''' at '''x = 10'''. | |
+ | |||
+ | This plot is the same as observed in case of polynomialEvaluator function. | ||
+ | |||
+ | Now let me de-select z and delete the result. | ||
|- | |- | ||
− | || | + | || Go back to '''Modeling perspective''' |
− | || | + | || Go back to '''Modeling perspective''' |
|- | |- | ||
|| | || | ||
− | || | + | || Now let me switch to the slides. |
|- | |- | ||
|| Slide: | || Slide: | ||
− | '''for''' (Add another slide for syntax) | + | '''for loop''' (Add another slide for syntax) |
|| '''for''' loop is used to iterate statements a given number of times | || '''for''' loop is used to iterate statements a given number of times | ||
− | + | It can be used in algorithm and equation sections. | |
− | + | Syntax for '''for''' loop is as shown with an example. | |
|- | |- | ||
|| | || | ||
− | || | + | || To demonstrate how to use for loop let me go back to '''OMEdit'''. |
|- | |- | ||
Line 218: | Line 239: | ||
'''fx := a[1]*x^2 + a[2]*x + a[3]; ''' | '''fx := a[1]*x^2 + a[2]*x + a[3]; ''' | ||
− | || | + | || In the assignment statement for '''fx''', we are accessing the elements of vector '''a'''. |
− | This can also be done using a for loop. Now, let us include a for loop in the algorithm section. | + | This can also be done using a for loop. |
+ | |||
+ | Now, let us see how to include a for loop in the algorithm section. | ||
|- | |- | ||
|| Comment // '''fx := a[1]*x^2 + a[2]*x + a[3]; ''' | || Comment // '''fx := a[1]*x^2 + a[2]*x + a[3]; ''' | ||
− | || Comment the assignment statement for '''fx''' | + | || Firstly, Comment the assignment statement for '''fx''' by inserting double slash at the beginning and end. |
+ | |||
+ | Save this function by pressing Ctrl+S. | ||
|- | |- | ||
|| | || | ||
− | || The for loop to be inserted has been provided in a text file named '''for-loop.txt''' | + | || The for loop to be inserted has been provided in a text file named '''for-loop.txt'''. |
− | I have opened this file using '''gedit'''. Windows users may use notepad to open it. | + | It is available on our website. |
+ | |||
+ | I have opened this file using '''gedit'''. | ||
+ | |||
+ | Windows users may use notepad or any other text editor to open it. | ||
|- | |- | ||
Line 238: | Line 267: | ||
|- | |- | ||
|| | || | ||
− | || Copy all the statements. Go back to '''OMEdit''' | + | || Copy all the statements by pressing Ctrl+C. |
+ | |||
+ | Go back to '''OMEdit''' | ||
|- | |- | ||
Line 244: | Line 275: | ||
|| Press '''Enter'''. | || Press '''Enter'''. | ||
− | Paste the | + | Paste all the statements by pressing '''Ctrl + V'''. |
+ | |||
+ | Save this function by pressing '''Ctrl + S'''. | ||
|- | |- | ||
|| // '''fx := 0;''' | || // '''fx := 0;''' | ||
− | || Now, let me explain each statement. | + | || Now, let me explain each statement of this loop. |
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 254: | Line 287: | ||
|- | |- | ||
|| // '''for i in 1:3 loop''' | || // '''for i in 1:3 loop''' | ||
− | || Here, '''i''' serves as a loop counter. | + | || Here, '''i''' serves as a loop counter. |
+ | |||
+ | The loop runs until value of '''i''' is '''3'''. | ||
It is not necessary to declare '''i''' before it is used. | It is not necessary to declare '''i''' before it is used. | ||
+ | |||
+ | Let me scroll down a bit. | ||
|- | |- | ||
|| //''' fx := fx + a[i]*x^(3-i); ''' | || //''' fx := fx + a[i]*x^(3-i); ''' | ||
|| This statement iteratively adds terms of the polynomial '''f(x)'''. | || This statement iteratively adds terms of the polynomial '''f(x)'''. | ||
+ | |||
+ | Polynomial '''f(x)''' has been discussed while discussing polynomialEvaluator function. | ||
|- | |- | ||
Line 268: | Line 307: | ||
|- | |- | ||
|| | || | ||
− | || Now, | + | || Now, this function is complete. |
− | + | To test this function let us use the class '''functionTester'''. | |
− | + | ||
− | + | I have made no changes to this function to this class. | |
|- | |- | ||
|| Click on '''Simulate''' button | || Click on '''Simulate''' button | ||
− | || | + | || let me simulate this class by pressing '''Simulate''' button in the toolbar. |
|- | |- | ||
|| /* Plotting perspective */ | || /* Plotting perspective */ | ||
− | || | + | || Select '''z''' in '''variables browser'''. |
− | + | Note that the value of '''z''' remains the same after changes are made to the function. | |
+ | |||
+ | Let me de-select z and delete the result. | ||
|- | |- | ||
− | || | + | || Go back to '''Modeling perspective''' |
− | || | + | || Go back to '''Modeling perspective''' |
|- | |- | ||
|| | || | ||
− | || | + | || Now let me go back to the slides once again. |
|- | |- | ||
|| Slide: | || Slide: | ||
− | '''while''' | + | '''while loop''' |
− | || '''while''' loop is used to iterate | + | || '''while''' loop is used to iterate statements until a given condition is satisfied. |
− | '''while''' loop cannot be used in equation section. | + | '''while''' loop cannot be used in '''equation''' section. |
− | '''for''' is frequently used as compared to '''while'''. | + | '''for ''' loop is more frequently used in Modelica as compared to '''while'''. |
|- | |- | ||
Line 307: | Line 348: | ||
'''Arrays''' | '''Arrays''' | ||
− | || '''Arrays''' | + | || Let us discuss '''Arrays''' now. |
− | + | '''Arrays''' are used to represent multi-dimensional data. | |
− | + | They can be constructed using vector notation. | |
|- | |- | ||
Line 323: | Line 364: | ||
'''Problem Statement''' | '''Problem Statement''' | ||
− | || Let us write a class named '''matrixAdder''' which | + | || To understated more about array construction and indexing |
+ | |||
+ | Let us write a class named '''matrixAdder''' which | ||
adds '''myMatrix''' and adder matrices to give '''mySum'''. | adds '''myMatrix''' and adder matrices to give '''mySum'''. | ||
Line 331: | Line 374: | ||
|- | |- | ||
|| | || | ||
− | || Now, let me switch to '''OMEdit''' to demonstrate '''matrixAdder''' class. It is already open in '''OMEdit'''. | + | || Now, let me switch to '''OMEdit''' to demonstrate '''matrixAdder''' class. |
+ | |||
+ | It is already open in '''OMEdit'''. | ||
|- | |- | ||
Line 337: | Line 382: | ||
|| Click on '''matrixAdder''' tab. | || Click on '''matrixAdder''' tab. | ||
− | + | Open it in '''Text view'''. | |
− | + | ||
− | + | ||
|- | |- | ||
Line 347: | Line 390: | ||
|- | |- | ||
|| '''parameter Real[3, 2] myMatrix = {{1,2},{3,4},{5,6}}'''; | || '''parameter Real[3, 2] myMatrix = {{1,2},{3,4},{5,6}}'''; | ||
− | || | + | || Numbers in square bracket represent size of this array. |
+ | |||
+ | The size of first dimension is '''3'''. | ||
Similarly, the size of second dimension is '''2''' | Similarly, the size of second dimension is '''2''' | ||
Line 353: | Line 398: | ||
|- | |- | ||
|| parameter Real[3, 2] myMatrix = // '''{{1,2},{3,4},{5,6}}; '''//''' Highlight piecewise''' | || parameter Real[3, 2] myMatrix = // '''{{1,2},{3,4},{5,6}}; '''//''' Highlight piecewise''' | ||
− | || '''myMatrix''' array is constructed using three vectors of two elements each. '''{1,2}''' represents the first vector. | + | || '''myMatrix''' array is constructed using three vectors of two elements each. |
+ | |||
+ | '''{1,2}''' represents the first vector. | ||
+ | |||
+ | '''{3,4}''' is the second one and | ||
+ | |||
+ | '''{5,6}}''' represents the third vector. | ||
+ | |||
+ | Size of each of this vectors is equal to size of second dimension of this array. | ||
− | + | Hence, the size of second dimension of myMatrix is '''2'''. | |
− | + | The number of vectors is equal to the size of first dimension. | |
− | + | Hence, the size of first dimension is equal to '''3'''. | |
|- | |- | ||
|| '''parameter Real[3, 2] adder''' '''<nowiki>= {{1,0},{0,1},{1,0}};</nowiki>''' | || '''parameter Real[3, 2] adder''' '''<nowiki>= {{1,0},{0,1},{1,0}};</nowiki>''' | ||
− | || '''adder''' matrix is | + | || '''adder''' matrix is constructed in a similar fashion. |
|- | |- | ||
|| | || | ||
− | || | + | || To add this two arrays or matrices we need to access elements from two dimensions. |
− | + | Hence a '''nested for''' loop is required. | |
|- | |- | ||
|| '''for i in 1:3 loop''' | || '''for i in 1:3 loop''' | ||
− | || This '''for''' loop runs through the first dimension | + | || This '''for''' loop runs through the first dimension. |
|- | |- | ||
|| '''for j in 1:2 loop''' | || '''for j in 1:2 loop''' | ||
− | || | + | || Similarly this for loop runs through the second dimension. |
+ | |||
+ | Let me scroll down a bit. | ||
|- | |- | ||
|| '''mySum[i, j] := myMatrix[i, j] + adder[i, j]; ''' | || '''mySum[i, j] := myMatrix[i, j] + adder[i, j]; ''' | ||
− | || Corresponding elements of '''myMatrix''' and adder are added | + | || Corresponding elements of '''myMatrix''' and adder matrices are added to yield '''mySum''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 389: | Line 442: | ||
'''end for; ''' | '''end for; ''' | ||
− | || | + | || This statements represent the end of each '''for''' loop. |
+ | |||
+ | The class is now complete. | ||
|- | |- | ||
|| Click on '''Simulate''' button | || Click on '''Simulate''' button | ||
− | || | + | || Let me simulate it by clicking on '''Simulate''' button. |
+ | |||
+ | Close the pop up window if it appears. | ||
|- | |- | ||
|| /* Plotting perspective */ | || /* Plotting perspective */ | ||
− | || | + | ||Let me expand variables column. |
− | + | Select '''adder[1,1]''', '''myMatrix[1,1]''', and '''mySum[1,1]'''. | |
− | Note that ''' | + | Note that '''adder[1,1]''' plus '''myMatrix[1,1]''' gives '''mySum[1,1]''' which means that the result is accurate. |
− | + | Let me de-select them and delete the result. | |
|- | |- | ||
|| | || | ||
− | || Let | + | || Let me go back to the slides. |
|- | |- | ||
Line 413: | Line 470: | ||
'''Assignment''' | '''Assignment''' | ||
− | || | + | || As an assignment: |
− | Write a function named '''vectorReversal''' | + | Write a function named '''vectorReversal''' to reverse the order of elements in a vector. |
− | + | Similarly write a function '''matrixReversal''' to reverse the order of elements in each row of a matrix. | |
− | Write | + | Write '''functionTester''' class to test these two functions. |
|- | |- | ||
Line 429: | Line 486: | ||
About the '''Spoken Tutorial project''' | About the '''Spoken Tutorial project''' | ||
− | || | + | || Please watch the video available at following link: |
['''http://spoken-tutorial.org/ http][http://spoken-tutorial.org/ ://][http://spoken-tutorial.org/ spoken][http://spoken-tutorial.org/ -][http://spoken-tutorial.org/ tutorial][http://spoken-tutorial.org/ .][http://spoken-tutorial.org/ org] /What\_is\_a\_Spoken\_Tutorial''' | ['''http://spoken-tutorial.org/ http][http://spoken-tutorial.org/ ://][http://spoken-tutorial.org/ spoken][http://spoken-tutorial.org/ -][http://spoken-tutorial.org/ tutorial][http://spoken-tutorial.org/ .][http://spoken-tutorial.org/ org] /What\_is\_a\_Spoken\_Tutorial''' | ||
It summarises the '''Spoken Tutorial''' project | It summarises the '''Spoken Tutorial''' project | ||
− | |||
− | |||
|- | |- | ||
Line 443: | Line 498: | ||
|| We conducts workshops using spoken tutorials | || We conducts workshops using spoken tutorials | ||
− | Give certificates | + | Give certificates. |
− | Please contact us | + | Please contact us. |
|- | |- | ||
Line 451: | Line 506: | ||
Forum | Forum | ||
− | || If you have questions | + | || If you have questions related to this spoken tutorial, please visit the following website. |
|- | |- | ||
Line 457: | Line 512: | ||
'''Textbook Companion Project''' | '''Textbook Companion Project''' | ||
− | || We coordinate coding of solved examples | + | || We coordinate coding of solved examples of popular books. |
+ | |||
+ | We give honorarium and certificates to those who do this. | ||
+ | |||
+ | Please visit the following website. | ||
|- | |- | ||
Line 463: | Line 522: | ||
'''Lab Migration Project''' | '''Lab Migration Project''' | ||
− | || We help migrate | + | || We help migrate commercial simulator labs to OpenModelica. |
+ | |||
+ | Please visit the following website for more information. | ||
|- | |- | ||
Line 469: | Line 530: | ||
'''Acknowledgements''' | '''Acknowledgements''' | ||
− | || Spoken Tutorial Project is | + | || Spoken Tutorial Project is funded by '''NMEICT, MHRD''', Government of India. |
|- | |- | ||
Line 475: | Line 536: | ||
'''Thanks''' | '''Thanks''' | ||
− | |"| We thank '''OpenModelica''' | + | |"| We thank the development team of '''OpenModelica''' for their support. |
|- | |- | ||
|| | || | ||
− | || Thank you for joining me in this tutorial. | + | || Thank you for joining me in this tutorial. |
− | + | ||
− | + |
Revision as of 14:03, 16 March 2016
Visual Cue | Narration |
Slide:
Title Slide |
Welcome to the spoken tutorial on Arrays. |
Slide:
Learning Objectives |
In this tutorial, we are going to learn:
|
Slide:
System Requirements |
To record this tutorial, I am using:
You may use any of the following operating systems to practice this tutorial. |
Slide:
Prerequisites |
To understand and practice this tutorial,
Prerequisite tutorials are mentioned on our website. Please go through them. |
Slide:
Vector declaration (Add an example in the slide for declaration as well) |
Vector is a one dimensional array
It has single index Syntax for vector declaration is as shown. The example shown declares a vector variable a whose size is 2. A vector can be constructed by including the elements in curly braces. This example defines a vector parameter a with 2 and 3 as its elements. |
Slide:
Indexing vector elements |
To access the elements of a vector, it is necessary to understand indexing.
Syntax for vector indexing is as shown. Vector indexing starts from 1 and Indices must be integers. |
Problem Statement | Let us develop a function named polynomialEvaluatorUsingVectors.
This function is an extension of polynomialEvaluator function which was discussed in previous tutorials. We shall replace parameters a,b and c of polynomialEvaluator with a vector a. |
Please download and save all the files available on our Code Files link.
For your convenience, polynomialEvaluator function is also made available. | |
/* Switch to OMEdit */ | Now let me switch to OMEdit to demonstrate this function.
OMEdit is now open in Welcome perspective. I have opened all the necessary files. Note that the following classes or functions are now open in OMEdit: functionTester, matrixAdder, polynomialEvaluator and polynomialEvaluatorUsingVectors. |
/* polynomialEvaluator */
protected parameter Real a = 1; protected parameter Real b = 2; protected parameter Real c = 1; |
Now, to view them let me double click on each icon.
Let me shift the OMEdit window to the left for better visibility. Go to polynomialEvaluator tab. Open it in Text View. For more information on this function, refer to the previous tutorials. |
Let me go to polynomialEvaluatorUsingVectors.
Open it in Text View. | |
/* polynomialEvaluatorUsingVectors */
input Real x; output Real fx; |
Input and output variables are the same as in polynomialEvaluator function. |
protected parameter Real a[3] = {1,2,1}; | Parameters a,b and c of polynomialEvaluator are replaced with a vector a. |
protected parameter Real a[3] = {1,2,1}; | Size of this vector is 3. |
protected parameter Real a[3] = {1,2,1}; | The elements of this vectors are included in curly braces as shown.
The elements are separated by a comma. |
algorithm
fx := a[1]*x^2 + a[2]*x + a[3]; |
This indicates the beginning of algorithm section.
The elements of vector a are accessed using their indices. |
algorithm
fx := a[1]*x^2 + a[2]*x + a[3]; |
In the assignment statement the elements of vector a are accessed using their indices.
a[1] is the first element of vector a. |
fx := a[1]*x^2 + a[2]*x + a[3]; | Similarly, the second element and third element of vector a have been accessed as well. |
Now, let me switch to functionTester tab.
Open it in Text View. This class is similar to the functionTester class discussed in previous tutorial. | |
/* functionTester */
Real z; |
z is a Real variable. |
z = polynomialEvaluatorUsingVectors(10); | polynomialEvaluatorUsingVectors function is called with an input argument of 10 units. |
z = polynomialEvaluatorUsingVectors(10); | The value returned by this function is equated to z.
Now let me Simulate this class. |
Click on Simulate button | Click on Simulate button.
Close the pop up window. |
/* Plotting perspective */
Select z in the variables browser |
functionTester class is simulated.
Select z in the variables browser. Note that the value of z is equal to f(x) at x = 10. This plot is the same as observed in case of polynomialEvaluator function. Now let me de-select z and delete the result. |
Go back to Modeling perspective | Go back to Modeling perspective |
Now let me switch to the slides. | |
Slide:
for loop (Add another slide for syntax) |
for loop is used to iterate statements a given number of times
It can be used in algorithm and equation sections. Syntax for for loop is as shown with an example. |
To demonstrate how to use for loop let me go back to OMEdit. | |
Click on polynomialEvaluatorUsingVectors tab. | |
/* polynomialEvaluatorUsingVectors */
fx := a[1]*x^2 + a[2]*x + a[3]; |
In the assignment statement for fx, we are accessing the elements of vector a.
This can also be done using a for loop. Now, let us see how to include a for loop in the algorithm section. |
Comment // fx := a[1]*x^2 + a[2]*x + a[3]; | Firstly, Comment the assignment statement for fx by inserting double slash at the beginning and end.
Save this function by pressing Ctrl+S. |
The for loop to be inserted has been provided in a text file named for-loop.txt.
It is available on our website. I have opened this file using gedit. Windows users may use notepad or any other text editor to open it. | |
Let me go to gedit. | |
Copy all the statements by pressing Ctrl+C.
Go back to OMEdit | |
Press Enter.
Paste all the statements by pressing Ctrl + V. Save this function by pressing Ctrl + S. | |
// fx := 0; | Now, let me explain each statement of this loop.
This statement assigns fx an initial value of zero before the loop starts. |
// for i in 1:3 loop | Here, i serves as a loop counter.
The loop runs until value of i is 3. It is not necessary to declare i before it is used. Let me scroll down a bit. |
// fx := fx + a[i]*x^(3-i); | This statement iteratively adds terms of the polynomial f(x).
Polynomial f(x) has been discussed while discussing polynomialEvaluator function. |
// end for; | This statement indicates the end of for loop |
Now, this function is complete.
To test this function let us use the class functionTester. I have made no changes to this function to this class. | |
Click on Simulate button | let me simulate this class by pressing Simulate button in the toolbar. |
/* Plotting perspective */ | Select z in variables browser.
Note that the value of z remains the same after changes are made to the function. Let me de-select z and delete the result. |
Go back to Modeling perspective | Go back to Modeling perspective |
Now let me go back to the slides once again. | |
Slide:
while loop |
while loop is used to iterate statements until a given condition is satisfied.
while loop cannot be used in equation section. for loop is more frequently used in Modelica as compared to while. |
Slide:
Arrays |
Let us discuss Arrays now.
Arrays are used to represent multi-dimensional data. They can be constructed using vector notation. |
Slide:
Syntax |
Syntax for array declaration and indexing is as shown. |
Slide:
Problem Statement |
To understated more about array construction and indexing
Let us write a class named matrixAdder which adds myMatrix and adder matrices to give mySum. myMatrix and adder matrices are as shown. |
Now, let me switch to OMEdit to demonstrate matrixAdder class.
It is already open in OMEdit. | |
/* Switch to OMEdit */ | Click on matrixAdder tab.
Open it in Text view. |
parameter Real[3, 2] myMatrix = {{1,2},{3,4},{5,6}}; | myMatrix is a Real parameter array. |
parameter Real[3, 2] myMatrix = {{1,2},{3,4},{5,6}}; | Numbers in square bracket represent size of this array.
The size of first dimension is 3. Similarly, the size of second dimension is 2 |
parameter Real[3, 2] myMatrix = // {{1,2},{3,4},{5,6}}; // Highlight piecewise | myMatrix array is constructed using three vectors of two elements each.
{1,2} represents the first vector. {3,4} is the second one and {5,6}} represents the third vector. Size of each of this vectors is equal to size of second dimension of this array. Hence, the size of second dimension of myMatrix is 2. The number of vectors is equal to the size of first dimension. Hence, the size of first dimension is equal to 3. |
parameter Real[3, 2] adder = {{1,0},{0,1},{1,0}}; | adder matrix is constructed in a similar fashion. |
To add this two arrays or matrices we need to access elements from two dimensions.
Hence a nested for loop is required. | |
for i in 1:3 loop | This for loop runs through the first dimension. |
for j in 1:2 loop | Similarly this for loop runs through the second dimension.
Let me scroll down a bit. |
mySum[i, j] := myMatrix[i, j] + adder[i, j]; | Corresponding elements of myMatrix and adder matrices are added to yield mySum |
end for;
end for; |
This statements represent the end of each for loop.
The class is now complete. |
Click on Simulate button | Let me simulate it by clicking on Simulate button.
Close the pop up window if it appears. |
/* Plotting perspective */ | Let me expand variables column.
Select adder[1,1], myMatrix[1,1], and mySum[1,1]. Note that adder[1,1] plus myMatrix[1,1] gives mySum[1,1] which means that the result is accurate. Let me de-select them and delete the result. |
Let me go back to the slides. | |
Slide:
Assignment |
As an assignment:
Write a function named vectorReversal to reverse the order of elements in a vector. Similarly write a function matrixReversal to reverse the order of elements in each row of a matrix. Write functionTester class to test these two functions. |
This brings us to the end of this tutorial | |
Slide:
About the Spoken Tutorial project |
Please watch the video available at following link:
[http://spoken-tutorial.org/ http]://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial It summarises the Spoken Tutorial project |
Slide:
Spoken Tutorial Workshops |
We conducts workshops using spoken tutorials
Give certificates. Please contact us. |
Slide:
Forum |
If you have questions related to this spoken tutorial, please visit the following website. |
Slide:
Textbook Companion Project |
We coordinate coding of solved examples of popular books.
We give honorarium and certificates to those who do this. Please visit the following website. |
Slide:
Lab Migration Project |
We help migrate commercial simulator labs to OpenModelica.
Please visit the following website for more information. |
Slide:
Acknowledgements |
Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India. |
Slide:
Thanks |
We thank the development team of OpenModelica for their support. |
Thank you for joining me in this tutorial. |