Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lab_MATLAB_English_Part2.doc
Скачиваний:
2
Добавлен:
19.11.2019
Размер:
421.89 Кб
Скачать

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

  1. What purpose of for statement?

  2. How does the for… statement work? Explain step by step.

  3. How does the while… loop work?

  4. What is an infinite loop? Give an example of an infinite loop.

  5. List the ways by which an infinite loop can be broken.

  6. Can we use the control variable during the execution of the loop? Explain a typical use of this value.

  7. What are the rules of using for … loops?

  8. 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)

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]