This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Edward Neuman Department of Mathematics Southern Illinois University at Carbondale [email protected] This tutorial is intended for those who want to learn basics of MATLAB programming language. Even with a limited knowledge of this language a beginning programmer can write his/her own computer code for solving problems that are complex enough to be solved by other means. Numerous examples included in this text should help a reader to learn quickly basic programming tools of this language. Topics discussed include the m-files, inline functions, control flow, relational and logical operators, strings, cell arrays, rounding numbers to integers and MATLAB graphics. Files that contain a computer code are called the m-files . There are two kinds of m-files: the script files and the function files . Script files do not take the input arguments or return the output arguments. The function files may take input arguments or return output arguments. To make the m-file click on File next select New and click on M-File from the pull-down menu. You will be presented with the MATLAB Editor/Debugger screen. Here you will type your code, can make changes, etc. Once you are done with typing, click on File , in the MATLAB Editor/Debugger screen and select Save As… . Chose a name for your file, e.g., firstgraph.m and click on Save . Make sure that your file is saved in the directory that is in MATLAB's search path. If you have at least two files with duplicated names, then the one that occurs first in MATLAB's search path will be executed. To open the m-file from within the Command Window type edit firstgraph and then press Enter or Return key. Here is an example of a small script file % Script file firstgraph. x = pi/100:pi/100:10*pi; y = sin(x)./x; plot(x,y) grid 2 Let us analyze contents of this file. First line begins with the percentage sign % . This is a comment. All comments are ignored by MATLAB. They are added to improve readability of the code. In the next two lines arrays x and y are created. Note that the semicolon follows both commands. This suppresses display of the content of both vectors to the screen (see Tutorial 1, page 5 for more details). Array x holds 1000 evenly spaced numbers in the interval [ /100 10 ] while the array y holds the values of the sinc function y = sin(x)/x at these points. Note use of the dot operator . before the right division operator / . This tells MATLAB to perform the componentwise division of two arrays sin(x) and x . Special operators in MATLAB and operations on one- and two dimensional arrays are discussed in detail in Tutorial 3, Section 3.2. The command plot creates the graph of the sinc function using the points generated in two previous lines. For more details about command plot see Section 2.8.1 of this tutorial. Finally, the command grid is executed. This adds a grid to the graph. We invoke this file by typing its name in the Command Window and next pressing the Enter or Return key firstgraph 5 10 15 20 25 30 35...
View Full Document
This note was uploaded on 03/19/2008 for the course BME 311 taught by Professor Zhang during the Spring '08 term at University of Texas.
- Spring '08