1 / 9

MATLAB Lecture #5 EGR 110 – Engineering Graphics

MATLAB Lecture #5 EGR 110 – Engineering Graphics. Reading Assignment: MATLAB handout MATLAB Assignment: MATLAB Assignment #5. Control Structures In the last lecture three types of control structures were described: Straight line control Conditional control (or branching structures)

lexine
Download Presentation

MATLAB Lecture #5 EGR 110 – Engineering Graphics

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB Lecture #5 EGR 110 – Engineering Graphics Reading Assignment:MATLAB handout MATLAB Assignment:MATLAB Assignment #5 • Control Structures • In the last lecture three types of control structures were described: • Straight line control • Conditional control (or branching structures) • Iterative control (or looping structures) • Conditional structures were covered in the last lecture. The focus of this lecture will be iterative structures. • Iterative Structures • MATLAB offers two types of iterative structures: • for loops – primarily used to execute a block of statements a specified number of times • while loops – used to execute a block of instructions while a certain logical test is true

  2. MATLAB Lecture #5 EGR 110 – Engineering Graphics for loopVar = loopVector Command 1 Command 2 … Command n end The for loop The for loop is used to execute a block of statements a specified number of times. It has the form shown to the right. Example Evaluate the following summation: % filename: for1.m % Example: Use a for loop to find sum(i^3) for i = 1 to 10. Sum = 0; %Initialize variable to zero for i = 1:1:10 Sum = Sum + i^3; end fprintf('Sum = %0.0f\n',Sum); Results: >> for1 Sum = 3025 >> for2 Sum = 3025 >> % filename: for2.m % Example: Use a for loop to find sum(i^3) for i = 1 to 10. Sum = 0; %Initialize variable to zero for i = [1,2,3,4,5,6,7,8,9,10] Sum = Sum + i^3; end fprintf('Sum = %0.0f\n',Sum);

  3. MATLAB Lecture #5 EGR 110 – Engineering Graphics Example Write a program to determine the balance after N years on an account that contains an initial deposit D and earns a simple interest of I percent. Prompt the user to enter values for N, D, and I. % Sample program to determine the final balance in a savings account where % D = initial deposit % I = interest rate (percent) % N = number of years % Filename: Interest.m D = input('Enter the amount of the initial deposit: $'); I = input('Enter the percent interest rate: '); N = input('Enter the number of years: '); Balance = D; %Initial value in the account for years = 1:N Balance = Balance*(1+I/100); end fprintf('Final balance = $%0.2f\n',Balance); >> Interest Enter the amount of the initial deposit: $1000 Enter the percent interest rate: 6 Enter the number of years: 10 Final balance = $1790.85

  4. MATLAB Lecture #5 EGR 110 – Engineering Graphics Nested for loops For loops can occur within other for loops as shown in the examples below. Note that while indenting loops is not required, it helps to make them more readable. for loopVar1 = loopVector1 Command 1 for loopVar2 = loopVector2 Command A1 Command A2 … Command An end Command 2 … Command n end What value for Sum is printed in the program below? Sum = 0; for I = 1:10 for J = 1:15 Sum = Sum + 1; end End fprintf(‘\nSum = %0.0f’,Sum); • Example: • The example program on the following slide: • Prompts the user to enter each value of a 2D array • Calculates the maximum value of the array (and the row/column where the max occurs)

  5. MATLAB Lecture #5 EGR 110 – Engineering Graphics % Program to prompt the user to enter values in a matrix % by row and column number. % Also find the max value in the matrix. % Filename: Matrixmax.m format compact Rows = input('Enter the number of rows in the matrix: '); Columns = input('Enter the number of columns in the matrix: '); for i = 1:Rows for j = 1:Columns fprintf('Enter A(%0.0f,%0.0f):',i,j); A(i,j) = input(' '); end end A % find the max value in the matrix Max = A(1,1); %Set max to first value in array MaxRow = 1; MaxCol = 1; for i = 1:Rows for j = 1:Columns if A(i,j)> Max Max = A(i,j); MaxRow = i; MaxCol = j; end end end fprintf('Max value in array A is A(%0.0f,%0.0f) = %0.2f\n',MaxRow,MaxCol,Max);

  6. MATLAB Lecture #5 EGR 110 – Engineering Graphics >> Matrixmax Enter the number of rows in the matrix: 2 Enter the number of columns in the matrix: 3 Enter A(1,1): 12 Enter A(1,2): 14 Enter A(1,3): 17 Enter A(2,1): 23 Enter A(2,2): 11 Enter A(2,3): -8 A = 12 14 17 23 11 -8 Max value in array A is A(2,1) = 23.00

  7. MATLAB Lecture #5 EGR 110 – Engineering Graphics while LogicalTest Command 1 Command 2 … Command n end The while loop The while loop is used to execute a block of statements as long as a logical test is true. It has the form shown to the right. Example Modify the previous program that calculated the balance in an account after N years so that it will determine the number of years to reach a desired balance. See next slide.

  8. MATLAB Lecture #5 EGR 110 – Engineering Graphics % Sample program to determine the number of years required for an initial % balance to reach a final value % Deposit = initial deposit % Desired_Balance = final value to be reached % I = interest rate (percent) % N = number of years % Filename: Interest2.m Deposit = input('Enter the amount of the initial deposit: $'); Desired_Balance = input('Enter the desired final balance: $'); I = input('Enter the percent interest rate: '); N = 0; %Initialize the number of years Balance = Deposit; %Initial value in the account while Balance < Desired_Balance N = N+1; Balance = Balance*(1+I/100); end fprintf('\nResults:\nFinal balance = $%0.2f\n',Balance); fprintf('Number of years to reach final balance = %0.0f\n',N); >> Interest2 Enter the amount of the initial deposit: $1000 Enter the desired final balance: $3000 Enter the percent interest rate: 7 Results: Final balance = $3158.82 Number of years to reach final balance = 17

  9. MATLAB Lecture #5 EGR 110 – Engineering Graphics % Calculate sin(x) using the power series % sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ..... % where x is an angle in radians % Prompt the user to enter an angle A in degrees and display sin(A) % accurate to 4 digits after the decimal point (continue adding terms % to the infinite series until the absolute value of a terms < 0.0001). % Filename:sin.m Degrees = input('Enter an angle in degrees: '); Radians = Degrees*pi/180; Term = Radians; % First term sine = Term; % Initialize summation Power = 1; % Power used with each term Sign = 1; % First term is positive while abs(Term) >= 1e-4 Power = Power + 2; % Power increases by 2 each time Sign = -1*Sign; % Signs alternate with each term Term = Sign*Radians^Power/factorial(Power); sine = sine + Term; end fprintf('\nsin(%0.2f) = %0.4f\n',Degrees,sine); >> sin Enter an angle in degrees: 30 sin(30.00) = 0.5000 >> sin Enter an angle in degrees: 60 sin(60.00) = 0.8660

More Related