Difference between revisions of "OpenModelica/C2/Arrays-in-Modelica/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(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''' as a part of the series on OpenModelica
+
|| 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 '''arrays'''
+
* 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''' Linux version
+
* '''OpenModelica 1.9.2'''  
  
But, this process is identical in '''Windows''', '''Mac OS X''' or '''FOSSEE on ARM'''
+
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, you need
+
|| To understand and practice this tutorial,  
  
* Knowledge of arrays in any programming language
+
* you need knowledge of arrays in any programming language
* Knowledge of class definition in Modelica
+
* 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. The example shown declares an array variable a whose size is '''2'''.
+
Syntax for vector declaration is as shown.  
  
A vector can be constructed by including the elements in curly braces 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:
  
'''Vector Indexing'''
+
'''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.
  
The syntax for accessing an element is as shown.
+
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,c''' in '''polynomialEvaluator''' function with a vector
+
We shall replace parameters '''a,b''' and '''c''' of '''polynomialEvaluator''' with a vector '''a'''.
  
 
|-
 
|-
 
||  
 
||  
|| Please download and save all the code files available on our website.  
+
|| 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 */
|| Let me switch to '''OMEdit'''. It is now open in Welcome perspective.  
+
|| 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''', '''polynomialEvaluatorUsingVectors''', '''matrixAdder''', '''polynomialEvaluator'''
+
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 view these classes. Double click on each icon.  
+
|| Now, to view them let me double click on each icon.  
  
Switch to '''polynomialEvaluator''' tab.  
+
Let me shift the OMEdit window to the left for better visibility.
  
For more information on this function, watch the prerequisite tutorials.  
+
Go to '''polynomialEvaluator''' tab.
 +
 
 +
Open it in '''Text View'''.
 +
 
 +
For more information on this function, refer to the previous tutorials.  
  
 
|-
 
|-
 
||  
 
||  
|| Click on '''polynomialEvaluatorUsingVectors''' tab.
+
|| 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''' in polynomialEvaluator are replaced by a real vector parameter named '''a'''.
+
|| 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}''';
|| The size of this vector is '''3'''.
+
|| Size of this vector is '''3'''.
  
 
|-
 
|-
 
|| protected parameter '''Real a[3] = {1,2,1}''';
 
|| protected parameter '''Real a[3] = {1,2,1}''';
|| Vector elements are specified in curly braces. They are separated by comma.
+
|| 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>
|| a[1] is the first element in vector a.
+
|| In the assignment statement the elements of vector '''a''' are accessed using their indices.
  
It is the coefficient of '''x^2''' in the expression for '''fx'''.
+
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 elements of vector a are accessed.
+
|| Similarly, the second element and third element of vector '''a''' have been accessed as well.
  
 
|-
 
|-
 
||  
 
||  
|| Rest of this function is same as '''polynomialEvaluator'''.
+
|| Now, let me switch to '''functionTester''' tab.
  
|-
+
Open it in '''Text View'''.
||
+
|| Now, let us take a look at '''functionTester''' class
+
  
|-
+
This class is similar to the '''functionTester''' class discussed in previous tutorial.
|| Click on '''functionTester''' tab
+
|| Click on '''functionTester''' tab to switch.
+
  
 
|-
 
|-
Line 167: Line 179:
 
|-
 
|-
 
|| '''z = polynomialEvaluatorUsingVectors(10);'''
 
|| '''z = polynomialEvaluatorUsingVectors(10);'''
|| '''polynomialEvaluatorUsingVectors''' function is called. The input argument is '''10'''.
+
|| '''polynomialEvaluatorUsingVectors''' function is called with an input argument of '''10''' units.
  
 
|-
 
|-
 
|| '''z = polynomialEvaluatorUsingVectors(10);'''
 
|| '''z = polynomialEvaluatorUsingVectors(10);'''
|| '''z''' is equal to the output returned by function call.
+
|| 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 in the toolbar.
+
|| Click on '''Simulate''' button.
 +
 
 +
Close the pop up window.
  
 
|-
 
|-
 
|| /* Plotting perspective */
 
|| /* Plotting perspective */
  
Click on '''z''' in variables browser
+
Select '''z''' in the variables browser
 +
 
 
|| '''functionTester''' class is simulated.
 
|| '''functionTester''' class is simulated.
  
Click on '''z''' in the variables browser.
+
Select '''z''' in the variables browser.
  
'''z''' takes the value of '''f(x)''' at '''x = 10''', which is '''121'''
+
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.
  
 
|-
 
|-
|| Click on '''Modeling''' button
+
|| Go back to '''Modeling perspective'''
|| Click on '''Modeling''' button at bottom right.
+
|| Go back to '''Modeling perspective'''
  
 
|-
 
|-
 
||  
 
||  
|| Let me go back to the slides.
+
|| 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
  
'''for''' loop can be used in both algorithm and equation sections
+
It can be used in algorithm and equation sections.
  
The syntax of '''for''' loop is as shown with an example.  
+
Syntax for '''for''' loop is as shown with an example.  
  
 
|-
 
|-
 
||  
 
||  
|| Let me go back to '''OMEdit'''.
+
|| 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]; '''
|| To evaluate '''fx''', we are accessing the elements of vector '''a'''.  
+
|| 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''' in the algorithm section by inserting double slash.
+
|| 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.
+
|| 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 for loop using '''Ctrl + V'''. Press '''Ctrl + S''' to save the function.
+
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. Loop runs until '''i''' is '''3'''.  
+
|| 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, the function is complete. Let us use the class '''functionTester''' to test the function after modification.
+
|| Now, this function is complete.
  
|-
+
To test this function let us use the class '''functionTester'''.
|| Click on '''functionTester''' tab
+
 
|| Click on '''functionTester''' tab. We have not made any changes to this class.
+
I have made no changes to this function to this class.
  
 
|-
 
|-
 
|| Click on '''Simulate''' button
 
|| Click on '''Simulate''' button
|| Click on '''Simulate''' button in the toolbar.
+
|| let me simulate this class by pressing '''Simulate''' button in the toolbar.
  
 
|-
 
|-
 
|| /* Plotting perspective */
 
|| /* Plotting perspective */
|| The class is simulated.  
+
|| Select '''z''' in '''variables browser'''.  
  
Click on '''z''' in '''variables browser'''. Value of '''z''' remains the same after changes made to the function.
+
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.
  
 
|-
 
|-
|| Switch to '''Modeling perspective'''
+
|| Go back to '''Modeling perspective'''
|| Click on '''Modeling''' button at bottom right.
+
|| Go back to '''Modeling perspective'''
  
 
|-
 
|-
 
||  
 
||  
|| Let me go back to the slides.
+
|| Now let me go back to the slides once again.
  
 
|-
 
|-
 
|| Slide:
 
|| Slide:
  
'''while'''
+
'''while loop'''
  
|| '''while''' loop is used to iterate a set of statements until a given condition is satisfied.
+
|| '''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''' may have more than one dimension.
+
|| Let us discuss '''Arrays''' now.
  
They can constructed using vector notation.
+
'''Arrays''' are used to represent multi-dimensional data.
  
2 dimensional arrays may be constructed using matrix notation as well.
+
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'''.
||
+
|| This class performs matrix addition by accessing each element individually.
+
  
 
|-
 
|-
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}}''';  
|| This represents array size. It has two dimensions. The size of first dimension is '''3'''.  
+
|| 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.  
  
Size of the vector is equal to size of second dimension of the array. Hence, size of second dimension of myMatrix is '''2'''.
+
Hence, the size of second dimension of myMatrix is '''2'''.
  
Number of vectors is equal to the size of first dimension.
+
The number of vectors is equal to the size of first dimension.
  
Higher dimensional arrays may be constructed similarly.
+
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 similarly constructed.
+
|| '''adder''' matrix is constructed in a similar fashion.
  
 
|-
 
|-
 
||  
 
||  
|| We need to access elements from two dimensions here.  
+
|| To add this two arrays or matrices we need to access elements from two dimensions.  
  
A '''nested for''' loop is required.
+
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 of array.
+
|| This '''for''' loop runs through the first dimension.
  
 
|-
 
|-
 
|| '''for j in 1:2 loop'''
 
|| '''for j in 1:2 loop'''
|| This loop runs through the second dimension.
+
|| 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'''
 
+
Elements of '''mySum''' array are produced from this addition.
+
  
 
|-
 
|-
Line 389: Line 442:
  
 
'''end for; '''
 
'''end for; '''
|| They represent the end of each '''for''' loop.
+
|| This statements represent the end of each '''for''' loop.
 +
 
 +
The class is now complete.
  
 
|-
 
|-
 
|| Click on '''Simulate''' button
 
|| Click on '''Simulate''' button
|| Click on '''Simulate''' button in the toolbar.
+
|| Let me simulate it by clicking on '''Simulate''' button.
 +
 
 +
Close the pop up window if it appears.
  
 
|-
 
|-
 
|| /* Plotting perspective */
 
|| /* Plotting perspective */
||''' variables browser''' shows each individual element of the array.
+
||Let me expand variables column.
  
Click on '''myMatrix[1,1], adder[1,1]''' and '''mySum[1,1]''' in variables browser.  
+
Select '''adder[1,1]''', '''myMatrix[1,1]'''and '''mySum[1,1]'''.
  
Note that '''mySum[1,1]''' is the sum of '''myMatrix[1,1]''' and '''adder[1,1]'''.  
+
Note that '''adder[1,1]''' plus '''myMatrix[1,1]''' gives '''mySum[1,1]''' which means that the result is accurate.  
  
Hence, this result is correct. Let me delete the result.
+
Let me de-select them and delete the result.
  
 
|-
 
|-
 
||  
 
||  
|| Let us go back to the slides.
+
|| Let me go back to the slides.
  
 
|-
 
|-
Line 413: Line 470:
  
 
'''Assignment'''
 
'''Assignment'''
|| Try the following problems:  
+
|| As an assignment:  
  
Write a function named '''vectorReversal''' that takes a vector as input and returns a vector with the order of elements reversed.
+
Write a function named '''vectorReversal''' to reverse the order of elements in a vector.
  
Write a function named '''matrixReversal''' that takes a matrix as input and returns a matrix with the order of elements in each row reversed.
+
Similarly write a function '''matrixReversal''' to reverse the order of elements in each row of a matrix.
  
Write a '''functionTester''' class to test these two functions
+
Write '''functionTester''' class to test these two functions.
  
 
|-
 
|-
Line 429: Line 486:
  
 
About the '''Spoken Tutorial project'''
 
About the '''Spoken Tutorial project'''
|| Watch the video available at the link shown below
+
|| 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
 
If you do not have good bandwidth, you can download and watch it.
 
  
 
|-
 
|-
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 in this tutorial, please visit the webpage mentioned.
+
|| 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 from popular books. We give honorarium to contributors. Please visit our website.
+
|| 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 labs from commercial simulators to OpenModelica.  
+
|| 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 supported by '''NMEICT, MHRD''', Government of India.
+
|| Spoken Tutorial Project is funded by '''NMEICT, MHRD''', Government of India.
  
 
|-
 
|-
Line 475: Line 536:
  
 
'''Thanks'''
 
'''Thanks'''
|"| We thank '''OpenModelica''' development team for their support.
+
|"| We thank the development team of '''OpenModelica''' for their support.
  
 
|-
 
|-
 
||  
 
||  
|| Thank you for joining me in this tutorial. Goodbye!
+
|| 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:
  • How to declare array variables
  • How to construct arrays
  • How to use for and while loops and
  • How to use OMShell
Slide:

System Requirements

To record this tutorial, I am using:
  • OpenModelica 1.9.2

You may use any of the following operating systems to practice this tutorial.

Slide:

Prerequisites

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.

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.

Contributors and Content Editors

Kaushik Datta, Nancyvarkey