Lecture 4

Warm up Exercise:

  • Evaluate the sum of -1^(n+1) / (2n-1) for n from 1 to N where N=100.
  • Find a value of N so that the sum is close to pi/4 (with difference < 10^-6)

Flow Control

Up to now, we saw how to make Matlab execute a list of commands without any decision making. Now we will learn to have Matlab do different things depending on the results of previous expressions.

if Statement

We might want have Matlab evaluate a bunch of expressions if some condition is satisfied, and another bunch of expressions otherwise. The syntax for this is (don't type this into Matlab..)

if condition








Note that you must replace the condition with an expression that Matlab will evaluate first.


>> if 3<4

    '3 is less than 4'


    '3 is not less than 4'



Since the condition is true, the expression which is evaluated is '3 is less than 4', and this is then displayed on the screen.

We can try something more interesting. Type the following into a file (say, if_example.m)

x = rand;

s = 'The number %g is %s than 0.5\n';

if x<0.5





Save the file and run it (either from the menu, or by typing the name of the file). You might be asked to change directory, do that.

As you know, rand returned a (pseudo-)random number (to initialize it, you need to call something like rand('twister', sum(100*clock)), but read more about rand to find out why.)


fprintf is similar to sprintf except that it  writes the result instead of returning it. If you want to understand this a little further..read the help files and look at the difference in the output from the following commands:


>> a=sprintf("Hello!") 

>> b=fprintf("Hello!") 

The '\n' at the end of the string s is to make sure that Matlab actually goes down a line before returning us the command prompt...try removing it and see what happens.

What is Truth?

I'm not trying to get all philosophical or anything, but we need to know what matlab considers to be "true" and what to be "false", if we want to understand how the execution of an if statement works. For this I want you to create another file (oracle.m) with the following 

if x 





Then, from the command line, assign various values to x and run your file. See if each x value is true or false. Things you can try out.





x=[0 0]


x=[0 1]

x=[1 2 3] 



x=' '



for Loops

Loops are good when we want to do stuff over and over again. For example, if we didn't know how to sum up the elements of a vector using sum or linear algebra commands, we could use a loop to do it (a bad idea...much slower than the alternative, about 100 times slower!!! check out loop_timer.m if you care.) However, slow they may be, but we might still need them....The syntax is as follows:

for variable = vector

    a bunch 



    here variable is an 

    ELEMENT of vector



The way it works is that for every element in vector, matlab runs through all the commands in the block. during each such execution, the value of variable is set to that element...Let see a simple example:

for v = 1:10





Notice that  for every number from 1 to 10 we have two printouts (since there were two commands in the block) one simply the number and the other is the number squared...just as we asked. 

One can have any legal variable name instead of the variable, but note that it gets overwritten. Also the vector, can be any vector...


for v = 'hello'



One can "break out" of a loop before it is done by issuing a break command:

for v = 'hello and goodbye'


    if v=='d'




OK, enough new material..lets exercise!


  • Find the sum of a vector x=[3 5 12 42 67] without using the old tricks...that is, use a for loop and don't use sum. 
  • Write a little program that checks if x=73 is prime. Do not use isprime. But you might find mod or rem useful.
  • Modify your program to find the first 20 primes 
  • Write a program that find the error in the calculation from the top of the page for N = 1, 10, 100,...10^6 and plots the results in a meaningful way. (you might want to consider using a log-plot, with loglog, or manually, taking the log from both axes before plotting)