Download presentation
Presentation is loading. Please wait.
1
MATLAB – Basic For Loops
Topics Covered: Loops in MATLAB - To repeat code - To access elements of an array Instructor notes: This set of slides covers loops, in particular the for-end loop. Also covered is the break command, which allows a program to jump out of a loop prior to reaching the terminal value of the loop counter. Instructional objectives: Acquire advanced programming concepts including programming using loops Use loops (for-end) and break statements in MATLAB for programming MATLAB - 9
2
Loops in MatLab What is a Loop ?
A loop allows a group of commands in a program to be repeated. Each repetition of the loop is called a pass. Either the number of passes can be fixed. Or the loop can be terminated after some condition is satisfied. Instructor notes: This slide is fairly straightforward. Looping causes a group of commands to be executed repeatedly before moving on to the commands that follow the loop. A loop has a beginning statement, a statement or group of statements that make up its body, and an ending statement. Each time program execution moves through the loop it is called a pass. The number of passes through a loop can be set to some fixed amount, or the looping can be set to continue until a specified condition is reached. It is very likely, and quite possible, that all of the variables used by a loop will reach new values for each pass. We’re talking about the first case today (simple use of a fixed number of passes). MATLAB - 9
3
Examples of basic for–end Loops
for k = 1:3 disp(k) end X=[5, 10, 15, 20]; for i = 1: length(X) disp(X(i)) end Output: 1 2 3 Output: 5 10 15 20 Instructor Notes: These 2 small examples give the essence of the for-end command. The students should type these as programs and then run them to have a feel of how the commands work. MATLAB - 9
4
for–end loops with vectors
Create the vector v = [ ]. Calculate and print the square of each element. Output: vs = 4 4 16 Program: v = [ ] ; for k = 1 : 4 vs(k) = v(k)^2 end Instructor notes: This example introduces the student of going through a vector element by element. Very important that the students understand the addressing of vectors using the for-end loop. Note how the output array is being created one element at a time by the computer. MATLAB - 9
5
Calculate the factorial of 5
What is a factorial? l If n=5 and x=1, you continue up to 5 x=x * i 1*1=1 1*2=2 2*3=6 6*4=24 24*5=120 Now, let’s try to have Matlab calculate the factorial of 5.. Presentation Short Title
6
Additional basic for-end loop example
clc clear %This script file calculates the factorial of a number %declare the number you wish to calculate the factorial for n=5; %calculate factorial using a for loop x=1; for i=1:n x=x*i; end fprintf('The factorial of %i is %i\n',n,x)
7
Matlab Tip of the Day – Dividing your code into Sections
Helps focus on one section of the script file Reduces overall computation time Easy to identify errors Helps create awesome published documents Simple to use All you need is %% (two comment characters or double percent)
8
Using %% - The Steps Start first code section with %%
Write the code for the first section Use %% again to start the next section and so on To run only a particular section Select particular section (by clicking anywhere in the section) Press Ctrl+Enter To run overall code or to publish – same as before
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.