
- •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
1.3 Example of Performance of Laboratory Work
Example
1.5.
Calculate the sum of the row
with
the given
x and
the
accuracy
and give the amount of the items.
As
you know from mathematic, this sum tendencies to value of function
.
To calculate every item of row we need to accumulate the power of x in numerator of fraction and accumulate the production in the denominator of fraction. The better way is to use recurrence formula for row items. It allows calculate next item form previous and number of item i. Let deduce the recurrence formula for this example. We have:
So, in program we recalculate item ST by formula ST = ST * x / i
We suppose that sum of row is calculated with accuracy ε when last item became less than ε, and we use this condition to finish the loop.
The text of the function for the example 1.5:
function [y,i]=exmpl1_5(x,eps)
i = 1 ;
ST = 1 ;
SUMMA = 1;
while abs(ST)>eps & i<100
ST = ST * x / i;
SUMMA = SUMMA + ST;
i = i + 1;
end
y=SUMMA;
Function calls give the following results:
>> [y,i]=exmpl1_5(1,0.001)
y = 2.7183
i = 8
>> [y,i]=exmpl1_5(-2,0.001)
y = 0.1354
i = 11
>> z=exp(-2) % Checking the program
z = 0.1353
1.4 The Test Questions
What purpose of for statement?
How does the for… statement work? Explain step by step.
How does the while… loop work?
What is an infinite loop? Give an example of an infinite loop.
List the ways by which an infinite loop can be broken.
Can we use the control variable during the execution of the loop? Explain a typical use of this value.
What are the rules of using for … loops?
What are nested structures? What are the rules of nesting loops?
2 Laboratory work № 2 The Work with Arrays in maTlab
2.1 Arrays
Arrays or vectors in Matlab allow you to refer to a series of variables by the same name and to use a number (an index) to tell them apart. This helps you create smaller and simpler code in many situations, because you can set up loops that deal efficiently with any number of cases by using the index number. Arrays have both upper and lower bounds, and the elements of the array are contiguous within those bounds. Lower bound of array index always equals 1. All the elements in an array have the same data type.
Matlab functions for arrays (vectors)
sum - sum of array elements. Syntax B = sum(A)
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
The magic square of order 3 is
M = magic(3)
M = 8 1 6
3 5 7
4 9 2
This is called a magic square because the sums of the elements in each column are the same.
sum(M) = 15 15 15
as are the sums of the elements in each row, obtained either by transposing
sum(M') = 15 15 15
2.2 Main algorithms for working with arrays
Students have know follows main algorithms:
calculations the sum of array elements, which ….. some conditions;
finding the minimum and maximum elements of array;
sorting the array;
transferring the arrays.
Example 2.1. The array of n elements is given. Calculate the sum of positive elements of array.
Function s=sp(x)