MATLAB Examples of Iterative operations

1 / 20

# MATLAB Examples of Iterative operations - PowerPoint PPT Presentation

MATLAB Examples of Iterative operations. Ch E 111 David A. Rockstraw, Ph.D., P.E. New Mexico State University Chemical Engineering Department. % Nested loops for i=1:4 for j=1:3 disp([i j i*j]) end end. Nested Loop Statements. clear A for i=1:n for j=1:n if i < j

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## MATLAB Examples of Iterative operations

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### MATLABExamples of Iterative operations

Ch E 111

David A. Rockstraw, Ph.D., P.E.

New Mexico State UniversityChemical Engineering Department

% Nested loops

for i=1:4

for j=1:3

disp([i j i*j])

end

end

Nested Loop Statements
clear A

for i=1:n

for j=1:n

if i < j

A(i,j)=-1;

elseif i > j

A(i,j)=0;

else

A(i,j)=1;

end

end

end

A

Nested Loop Statements
clear A

for i=1:n

for j=1:n

if i < j

A(i,j)=-1;

elseif i > j

A(i,j)=0;

else

A(i,j)=1;

end

end

end

A

Nested Loop Statements

compare with the built-in function statement:

AA=eye(n)-triu(ones(n),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.

Example 1
sum1 = 0;

sum2 = 0;

for i = 1:100

sum1 = sum1 + i;

sum2 = sum2 + i^2;

end

sum1

sum2

Example 1 Solution
sum = 0;

i = 0;

while ( sum < 1000000 )

i = i + 1;

sum = sum + i^2;

end

i

Example 2 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 = 1:100

running_sum = running_sum + cos(n);

if ( abs( running_sum ) < minimum_abs_sum )

minimum_n = n;

minimum_abs_sum = abs( running_sum );

end

end

minimum_n

minimum_abs_sum

Example 3 Solution
Continue subtracting (to a max of 1000 times) e from π until a value less than -10 is obtained.

x = pi

for i=1:1000

x = x - exp(1)

if x < -10

break;

end

end

Example 4
Calculate the sum of those entries of the matrix M=[123;456;789] which lie in the upper-triangular portion (i.e., on or above diagonal).Example 4
M = [1 2 3; 4 5 6; 7 8 9];

sum = 0;

for i = 1:3

for j = i:3

sum = sum + M(i, j);

end

end

sum

Example 4 Solution
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 = i+1; % 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)])

average.m
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.

Integrate the sine function
function y = intsin(N)

% INTSIN - integrate sin(x) from 0 to pi

y = (sin(0) + sin(pi))* pi/(2*N);

for n = 1 : N-1

y = y + sin(n*pi/N)*pi/N;

end

function intsin(N)
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?Infinite sum
function [y,n] = pi2over6(tol)

% PI2OVER6 - Approximates (pi)^2/6

y=0;

aim=pi*pi/6;

n=0;

while (abs(aim-y)>tol)

n=n+1;

y=y+1/(n*n);

disp(sprintf('%g%s%g%s%g',n,' ',y,' ',aim-y))

end

function pi2over6
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).

grcodi script
% GRCODI - determine greatest common divisor

N1=input('first number = ');

N2=input('second number = ');

while (min(N1,N2)>0)

if (N1 > N2)

N1=N1-N2;

else

N2=N2-N1;

end

end

disp(sprintf('%s%g','GCD is ',max(N1,N2)))

grcodi script