MATLAB Examples of Iterative operations - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

MATLAB Examples of Iterative operations

Description:

MATLAB Examples of Iterative operations – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 21
Provided by: davidro88
Category:

less

Transcript and Presenter's Notes

Title: MATLAB Examples of Iterative operations


1
MATLAB Examples of Iterative operations
  • Ch E 111
  • David A. Rockstraw, Ph.D., P.E.
  • New Mexico State UniversityChemical Engineering
    Department

2
Nested Loop Statements
  • Nested loops
  • for i14
  • for j13
  • disp(i j ij)
  • end
  • end

3
Nested Loop Statements
  • clear A
  • for i1n
  • for j1n
  • if i lt j
  • A(i,j)-1
  • elseif i gt j
  • A(i,j)0
  • else
  • A(i,j)1
  • end
  • end
  • end
  • A

4
Nested Loop Statements
  • clear A
  • for i1n
  • for j1n
  • if i lt j
  • A(i,j)-1
  • elseif i gt j
  • A(i,j)0
  • else
  • A(i,j)1
  • end
  • end
  • end
  • A

compare with the built-in function statement
AAeye(n)-triu(ones(n),1)
5
Example 1
  • Write a for loop which calculates the sum the
    integers from 1 to 100 and the sum of the squares
    of the integers from 1 to 100.
  • Print out only the results.

6
Example 1 Solution
  • sum1 0
  • sum2 0
  • for i 1100
  • sum1 sum1 i
  • sum2 sum2 i2
  • end
  • sum1
  • sum2

7
Example 2
  • Determine the largest value of n such that

8
Example 2 Solution
  • sum 0
  • i 0
  • while ( sum lt 1000000 )
  • i i 1
  • sum sum i2
  • end
  • i

9
Example 3
  • Find the integer value of n between 0 and 100
    such that

10
Example 3 Solution
  • Solve assuming that minimum occurs when n 0.
    Then, for each n 1, 2, ..., 100, compare the
    absolute value of the running sum with that of
    the minimum absolute running sum currently found.
    If it is less, update the two variables.
  • minimum_n 0 the sum when n 0
  • minimum_abs_sum 1 initially, the absolute
    value of cos(0)
  • running_sum 1 cos(0) ... cos(n)
  • for n 1100
  • running_sum running_sum cos(n)
  • if ( abs( running_sum ) lt minimum_abs_sum )
  • minimum_n n
  • minimum_abs_sum abs( running_sum )
  • end
  • end
  • minimum_n
  • minimum_abs_sum

11
Example 4
  • Continue subtracting (to a max of 1000 times) e
    from p until a value less than -10 is obtained.
  • x pi
  • for i11000
  • x x - exp(1)
  • if x lt -10
  • break
  • end
  • end

12
Example 4
  • Calculate the sum of those entries of the matrix
    M 1 2 3 4 5 6 7 8 9 which lie in the
    upper-triangular portion (i.e., on or above
    diagonal).

13
Example 4 Solution
  • M 1 2 3 4 5 6 7 8 9
  • sum 0
  • for i 13
  • for j i3
  • sum sum M(i, j)
  • end
  • end
  • sum

14
average.m
  • clear
  • initialize - prepare to read 1st datum
  • i 1
  • read and count data values
  • data input('Enter datum ("Enter" to stop) ')
  • while isempty(data) data?
  • y(i) data - yes store
  • i i1 count
  • data input('Enter datum ("Enter" to stop)
    ')
  • end
  • no more data - compute average
  • sumY sum(y) compute sum
  • dummy, n size(y) determine values
    columns
  • averageY sumY/n
  • print result
  • disp('the average of the 'num2str(n) ' values is
    ' num2str(averageY))

15
Integrate the sine function
  • Write a program that approximates
  • as
  • N is the number of points used in the
    integration. You may need to use a for loop from
    1 to N-1. Calculate the approximation using N
    5, 10, 20, and 50.

16
function intsin(N)
  • function y intsin(N)
  • INTSIN - integrate sin(x) from 0 to pi
  • y (sin(0) sin(pi)) pi/(2N)
  • for n 1 N-1
  • y y sin(npi/N)pi/N
  • end

17
Infinite sum
  • Use a while loop to calculate the summation until
    the deviation from the exact answer is less than
    0.1. Determine up to what n-value is needed to
    carry out the summation so that this deviation is
    achieved? How about a deviation of 0.01 and 0.001?

18
function pi2over6
  • function y,n pi2over6(tol)
  • PI2OVER6 - Approximates (pi)2/6
  • y0
  • aimpipi/6
  • n0
  • while (abs(aim-y)gttol)
  • nn1
  • yy1/(nn)
  • disp(sprintf('gsgsg',n,' ',y,' ',aim-y))
  • end

19
grcodi script
  • Write a program that, given two positive integers
    N1 and N2, calculates their greatest common
    divisor. The greatest common divisor can be
    calculated easily by iteratively replacing the
    largest of the two numbers by the difference of
    the two numbers until the smallest of the two
    numbers reaches zero. When the smallest number
    becomes zero, the other gives the greatest common
    divisor.
  • You will need to use a while loop and an if-else
    statement. The largest of two numbers can be
    obtained using the built-in function MAX(A,B).
    The smallest of two numbers can be obtained using
    the built-in function MIN(A,B).

20
grcodi script
  • GRCODI - determine greatest common divisor
  • N1input('first number ')
  • N2input('second number ')
  • while (min(N1,N2)gt0)
  • if (N1 gt N2)
  • N1N1-N2
  • else
  • N2N2-N1
  • end
  • end
  • disp(sprintf('sg','GCD is ',max(N1,N2)))
Write a Comment
User Comments (0)
About PowerShow.com