15.11.2014 Views

Chapter 4: Programming in Matlab - College of the Redwoods

Chapter 4: Programming in Matlab - College of the Redwoods

Chapter 4: Programming in Matlab - College of the Redwoods

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Section 4.2 Control Structures <strong>in</strong> <strong>Matlab</strong> 299<br />

4.2 Control Structures <strong>in</strong> <strong>Matlab</strong><br />

In this section we will discuss <strong>the</strong> control structures <strong>of</strong>fered by <strong>the</strong> <strong>Matlab</strong><br />

programm<strong>in</strong>g language that allow us to add more levels <strong>of</strong> complexity to <strong>the</strong><br />

simple programs we have written thus far. Without fur<strong>the</strong>r ado and fanfare, let’s<br />

beg<strong>in</strong>.<br />

If<br />

If evaluates a logical expression and executes a block <strong>of</strong> statements based on<br />

whe<strong>the</strong>r <strong>the</strong> logical expressions evaluates to true (logical 1) or false (logical 0).<br />

The basic structure is as follows.<br />

if logical_expression<br />

statements<br />

end<br />

If <strong>the</strong> logical_expression evaluates as true (logical 1), <strong>the</strong>n <strong>the</strong> block <strong>of</strong> statements<br />

that follow if logical_expression are executed, o<strong>the</strong>rwise <strong>the</strong> statements<br />

are skipped and program control is transferred to <strong>the</strong> first statement that follows<br />

end. Let’s look at an example <strong>of</strong> this control structure’s use.<br />

<strong>Matlab</strong>’s rem(a,b) returns <strong>the</strong> rema<strong>in</strong>der when a is divided by b. Thus, if a is<br />

an even <strong>in</strong>teger, <strong>the</strong>n rem(a,2) will equal zero. What follows is a short program<br />

to test if an <strong>in</strong>teger is even. Open <strong>the</strong> <strong>Matlab</strong> editor, enter <strong>the</strong> follow<strong>in</strong>g script,<br />

<strong>the</strong>n save <strong>the</strong> file as evenodd.m.<br />

n = <strong>in</strong>put(’Enter an <strong>in</strong>teger: ’);<br />

if (rem(n,2)==0)<br />

fpr<strong>in</strong>tf(’The <strong>in</strong>teger %d is even.\n’, n)<br />

end<br />

Return to <strong>the</strong> command w<strong>in</strong>dow and run <strong>the</strong> script by enter<strong>in</strong>g evenodd at<br />

<strong>the</strong> <strong>Matlab</strong> prompt. <strong>Matlab</strong> responds by ask<strong>in</strong>g you to enter an <strong>in</strong>teger. As a<br />

response, enter <strong>the</strong> <strong>in</strong>teger 12 and press Enter.<br />

4 Copyrighted material. See: http://msenux.redwoods.edu/Math4Textbook/

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!