lab3(1) - Chapter 3 Programming in Matlab 3.1 ME 218 Lab...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 3 Programming in Matlab 3.1 ME 218 Lab: Programming Structures in MATLAB The simpliest programs execute the first line, the second line, the third line and so on. But when we use programming structures (i.e. FOR loops, WHILE loops, and IF statements, we can cause the program to repeat certain steps (lines in a program), or skip steps. Stuctures control the flow of computer programs somewhat like traffic signs control the motion of cars. 3.1.1 Create, Name, and Save an m-file In MATLAB, open up a new m-file. At the top of your m-file, type your name, todays date, and the name of this exersize as follows: % Billy Bob % July 4, 1776 % ME 218 Lab:Programming Structures in MATLAB 35 CHAPTER 3. PROGRAMMING IN MATLAB 36 MATLAB ignores everything to the right of the % symbol. In this way, you can type comment to yourself or others within a MATLAB program without affecting the program. This is a good idea. Before we proceed, go ahead and save you m-file. Call it “structures.m” 3.1.2 FOR Loops You can give MATLAB a vector and a variable name and have it execute a list of commands with the variable set to each entry of the vector, one at a time. In words, here is how: for variable = vector instructions to be repeated end MATLAB assigns a variable (called the counting variable) to be the first entry of the vector and runs the “instructions to be repeated”. Then, MATLAB will move to the next entry in the vector, assign that value to the vector, and run the instructions again. Type this example into your m-file: clear for x = [ 1 3 5 ] x end Notice that we started out with the clear command which erases any previously stored variables in MATLAB. We also indented the line within the for loop–this is a good idea because it makes the program easier to read. If we have more than one line between the “for” and the “end” statements, we would have indented them all. Now, save and then run your MATLAB file (click the Run button or select Run from the Debug menu). MATLAB should print out the value of x three times in the Command Window. Each time, x has been changed to an entry of the vector in the for command. Your result should look like this: MATLAB response: x = 1 x = 3 x = 5 You can also use the range notation (“:”) to generate a vector for the for loop: for x = 1:2:5 x end CHAPTER 3. PROGRAMMING IN MATLAB 37 will produce the same results as the previous example. Problem 3.1 Enter the following commands in your m-file and run it if myValue equals 9. What size is y and what will be in each entry of y ? n = myValue; for x = 1:n y(x) = exp(x) ; end y Now, type whos in the Command Window to see what is left behind after running the for loop and assuming myValue equals 10: whos MATLAB response: Name Size Bytes Class ==== ==== ===== ===== x 1 × 1 8 double array y 1 × 10 80 double array Note that x is just a scalar! If you want x to hold the vector of all the values that the instructions were evaluated with, then you need to define it before hand:...
View Full Document

{[ snackBarMessage ]}

Page1 / 13

lab3(1) - Chapter 3 Programming in Matlab 3.1 ME 218 Lab...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online