Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
41 views

Introduction To MATLAB

MATLAB programs can be written as either functions or scripts. Functions have inputs and outputs while scripts do not. Functions cannot access variables outside the function while scripts can. Comments are important to document programs and explain their purpose, inputs, outputs, and sections.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Introduction To MATLAB

MATLAB programs can be written as either functions or scripts. Functions have inputs and outputs while scripts do not. Functions cannot access variables outside the function while scripts can. Comments are important to document programs and explain their purpose, inputs, outputs, and sections.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

INTRODUCTION TO PROGRAMMING

WITH MATLAB
MATLAB Programs: In MATLAB, programs may be written and
saved in files with a suffix .m called M-files. There are two types of
M-file programs: functions and scripts.

 Function Programs

 Begin by clicking on the new document icon in the top left of the
MATLAB window (it looks like an empty sheet of paper). In the
document window type the following:
 Save this file as: myfunc.m in your working directory. This file can
now be used in the command window just like any predefined
MATLAB function; in the command window enter:
 Look back at the program. All function programs are like this one, the
essential elements are:
• Begin with the word function.
• There is an input and an output.
• The output, name of the function and the input must appear in the
first line.
• The body of the program must assign a value to the output
variable(s).
• The program cannot access variables in the current workspace
unless they are input.

• Internal variables inside a function do not appear in the current

workspace.
 Functions can have multiple inputs, which are separated by commas.

For example:

 Functions can have multiple outputs, which are collected into a


vector. Open a new document and type:
 Save this file as mypowers.m. In the command window, we can use
the results of the program to make graphs:
 Script Programs

 MATLAB uses a second type of program that differs from a function


program in several ways, namely:
• There are no inputs and outputs.
• A script program may use, create and change variables in the
current workspace (the variables used by the command window).

 Below is a script program that accomplishes the same thing as the


function program plus the commands in the previous section:
• Type this program into a new document and save it as mygraphs.m.
In the command window enter:
 Note that the program used the variable 𝑥 in its calculations, even
though 𝑥 was defined in the command window, not in the program.

 Many people use script programs for routine calculations that would
require typing more than one command in the command window.
They do this because correcting mistakes is easier in a program than
in the command window.

 Program Comments

 For programs that have more than a couple of lines it is important to


include comments. Comments allow other people to know what your
program does and they also remind yourself what your program does
if you set it aside and come back to it later.
 It is best to include comments not only at the top of a program, but
also with each section. In MATLAB anything that comes in a line after
a % is a comment.

 For a function program, the comments should at least give the


purpose, inputs, and outputs. A properly commented version of the
function with which we started this section is:
 For a script program, there should be an initial comment starting the
purpose of the script. It is also helpful to include the name of the
program at the beginning. For example:

You might also like