- •Contents
- •1 Laboratory work №1. The loop statements in matlab programs
- •2 Laboratory work №2. The work with arrays in matlab
- •3 Laboratory work №3. The work with matrixes in matlab
- •3.2 Tasks for laboratory work ……………………………….25
- •3.3 Examples of performance of laboratory work …………..29
- •1 Laboratory work № 1 Loop statements in mAtlab programs
- •1.1 Loop Statements
- •1.2 Tasks for Laboratory Work
- •1.3 Example of Performance of Laboratory Work
- •1.4 The Test Questions
- •2 Laboratory work № 2 The Work with Arrays in maTlab
- •2.1 Arrays
- •Matlab functions for arrays (vectors)
- •If a is a vector, sum(a) returns the sum of the elements. If a is a matrix, sum(a) treats the columns of a as vectors, returning a row vector of the sums of each column. Example
- •2.2 Main algorithms for working with arrays
- •2.3 Tasks for Laboratory Work
- •2.3 Examples of Performance of Laboratory Work
- •2.4 Test Questions
- •3 Laboratory work № 3 The Work with Matrixes in matlab
- •3.1 Multidimensional Arrays
- •Some elementary matrix functions and operations of transformation and decomposition
- •3.2 Tasks for Laboratory Work
- •Variant 1
- •Variant 2
- •Variant 3
- •Variant 4
- •Variant 5
- •Variant 6
- •Variant 7
- •Variant 8
- •Variant 9
- •Variant 10
- •Variant 11
- •Variant 12
- •Variant 13
- •3.3 Example of Performance of Laboratory Work
- •4 The list of books
2.3 Tasks for Laboratory Work
Problem 2.1. The array X(n) is given. Calculate the value .
Variant 1
Variant 2
Variant 3
Variant 4
Variant 5
Variant 6
Variant 7
Variant 8
Variant 9
Variant 10
Variant 11
Variant 12
Variant 13
Problem 2.2. Calculate new array Y(n) from array X(n) in accordance with the rule :
Variant 1
Variant 2 Arrange the array elements in inverse order.
Variant 3 Shift cyclically the array elements on one position to the left.
Variant 4
Variant 5 Order the array elements on a decrease.
Variant 6 Swap the maximum and minimum elements.
Variant 7
Variant 8 Transfer all negative array elements to the beginning of array, and all the rest the end of array, leaving initial mutual disposition between them.
Variant 9
Variant 10
Variant 11
Variant 12 Shift cyclically the array elements on two positions to the right.
Variant 13 Order the array elements on a growth.
2.3 Examples of Performance of Laboratory Work
Example 2.1. The value n and array a(n) are given. Calculate the value of maximal element and its number.
The text of the programs for the decision of the problem,
Private Sub Command1_Click()
Dim s As String
If IsNull(Text3.Text) Or Text3.Text = "" Then
MsgBox "Input amount of array elements"
Text3.SetFocus
Exit Sub
End If
n = Text3.Text
ReDim a(n)
For i = 1 To n
a(i) = InputBox("a(" & i & ")=")
s = s & i & vbTab & a(i) & vbCrLf
Next i
Text1.Text = s
End Sub
Private Sub Command2_Click()
Dim amax As Integer, kmax As Integer
amax = a(1): kmax = 1
For i = 2 To n
If a(i) > amax Then amax = a(i): kmax = i
Next i
Text2.Text = amax
Text4.Text = kmax
End Sub
Example 2.2. The value n and array a(n) are given. Calculate the new array y according the rule: transfer all negative elements of array a to the beginning of array y and all the rest elements of array a to the end of array y, leaving initial mutual disposition between elements of array.
Dim x() As Integer, y() As Integer, n As Integer
Private Sub CommandButton1_Click()
Dim i As Integer, s As String
n = TextBox1.Text
ReDim x(n)
s = ""
For i = 1 To n
x(i) = InputBox("x(" & i & ")=")
s = s & x(i) & vbCrLf
Next i
TextBox2.Text = s
End Sub
Private Sub CommandButton2_Click()
Dim i As Integer, k As Integer, s As String
ReDim y(n)
k = 0
s = ""
For i = 1 To n
If x(i) < 0 Then
k = k + 1
y(k) = x(i)
s = s & y(k) & vbCrLf
End If
Next i
For i = 1 To n
If x(i) >= 0 Then
k = k + 1
y(k) = x(i)
s = s & y(k) & vbCrLf
End If
Next i
TextBox3.Text = s
End Sub
2.4 Test Questions
1. What is an array variable? Give an example.
2. What are the advantages of using array variables?
3. Discuss the applications of array variables.
4. Is it true that the array elements be of any type?
5. Is it possible to change the size of array at run time of the program?