Post on 14-Apr-2018
7/29/2019 Matlabadadfsfsf
http://slidepdf.com/reader/full/matlabadadfsfsf 1/2
Chapter 4
Loops and Logic
To solve many physics problems you have to know how to write loops and
how to use logic.
4.1 Loops
A loop is a way of repeatedly executing a section of code. It is so important to
know how to write them that several common examples of how they are used will
be given here. The two kinds of loops we will use are the f o r loop and the w h i l e
loop. We will look at f o r loops first, then study w h i l e l o o p s a bit later in thelogic section.
Thef o r
loop looks like this:
f o r n = 1 : N
% P u t c o d e h e r e
e n d
which tells Matlab to start n at 1, then increment it by 1 over and over until it
counts up to N , executing the code between f o r and e n d for each new value of n .
Here are a few examples of how the f o r loop can be used.
Summing a series with a f o r
loop
Let’s do the sumN
n =1
1
n 2(4.1)
with N chosen to be a large number.
Listing 4.1 (ch4ex1.m)
c l e a r ; c l o s e a l l ;
% s e t s t o z e r o s o t h a t 1 / n ^ 2 c a n b e r e p e a t e d l y a d d e d t o i t
s = 0 ;
N = 1 0 0 0 0 ; % s e t t h e u p p e r l i m i t o f t h e s u m
f o r n = 1 : N % s t a r t o f t h e l o o p
s = s + 1 / n ^ 2 ; % a d d 1 / n ^ 2 t o s e a c h i t e r a t i o n
e n d % e n d o f t h e l o o p
f p r i n t f (
'
S u m = % g \ n
'
, s ) % p r i n t t h e a n s w e r
Create a breakpoint at thex = 0
line in the code, run then code, and then step
through the first several iterations of thef o r
loop using F10. Look at the values of
19
7/29/2019 Matlabadadfsfsf
http://slidepdf.com/reader/full/matlabadadfsfsf 2/2
20 Chapter 4 Loops and Logic
n and s in the Workspace window and see how they change as the loop iterates.
Once you are confident you know how the loop works, press F5 to let the script
finish executing.
We can calculate the same sum using matrix operators like this
n = 1 : N ;
s u m ( 1 . / n . ^ 2 )
This matrix operator method is usually much faster. Try both the loop way and
this s u m command way and see which is faster. To slow things down enough that
you can see the difference change 10,000 to 100,000. When we tested this, the
colon ( : ) and s u m way was 21 times faster than the loop, so use array operators
whenever you can. You should use the colon command whenever possible
because it is a pre-compiled Matlab command. To do timing checks use thet i c
andt o c
commands. Look them up in online help.
To get some more practice with loops, let’s do the running sum
S m =
m
n =1
a n where a n =1
n 2(4.2)
for values of m from 1 to a large number N .
Listing 4.2 (ch4ex2.m)
c l e a r ; c l o s e a l l ;
N = 1 0 0 ;
a = z e r o s ( 1 , N ) ;
% F i l l t h e a a r r a y
f o r n = 1 : N
a ( n ) = 1 / n ^ 2 ;
e n d
S = z e r o s ( 1 , N ) ;
% D o t h e r u n n i n g s u m
f o r m = 1 : N
S ( m ) = s u m ( a ( 1 : m ) ) ;
e n d
% N o w l e t
'
s p l o t S v s . m
m = 1 : N
p l o t ( m , S )
Notice that in this example we pre-allocate the arraysa
andS
with thez e r o s
command before each loop. If you don’t do this, Matlab has to go grab an extra
little chunk of memory to expand the arrays each time the loop iterates and this
makes the loops run very slowly as N gets big.
We also could have done this cumulative sum using colon operators and the
c u m s u m command, like this: