SlideShare a Scribd company logo
MATLAB Summary
MATLAB (matrix algebra)
Matlab is a commercial "Matrix Laboratory" package which operates as an interactive
programming environment. It is a mainstay of the Mathematics Department software
lineup and is also available for PC's and Macintoshes and may be found on the CIRCA
VAXes. Matlab is well adapted to numerical experiments since the underlying algorithms
for Matlab's builtin functions and supplied m-files are based on the standard libraries
LINPACK and EISPACK.
Matlab program and script files always have filenames ending with ".m"; the
programming language is exceptionally straightforward since almost every data object is
assumed to be an array. Graphical output is available to supplement numerical results.
Online help is available from the Matlab prompt (a double arrow), both generally (listing
all available commands):
>> help
[a long list of help topics follows]

and for specific commands:
>> help fft
[a help message on the fft function follows].

Paper documentation is on the document shelf in compact black books and locally
generated tutorials are available and are used in courses.

How to quit Matlab
The answer to the most popular question concerning any program is this: leave a Matlab
session by typing
quit

or by typing
exit

to the Matlab prompt.

Batch jobs
Matlab is most often used interactively, but "batch" or "background" jobs can be
performed as well. Debug your commands interactively and store them in a file
(`script.m', for example). To start a background session from your input file and to put
the output and error messages into another file (`script.out', for example), enter this line at
the system prompt:
nice matlab < script.m >& script.out

&

You can then do other work at the machine or logout while Matlab grinds out your
program. Here's an explanation of the sequence of commands above.
1. The "nice" command lowers matlab's priority so that interactive users have first
crack at the CPU. You must do this for noninteractive Matlab sessions because of
the load that number--crunching puts on the CPU.
2. The "< script.m" means that input is to be read from the file script.m.
3. The ">& script.out" is an instruction to send program output and error output to
the file script.out. (It is important to include the first ampersand (&) so that error
messages are sent to your file rather than to the screen -- if you omit the
ampersand then your error messages may turn up on other people's screens and
your popularity will plummet.)
4. Finally, the concluding ampersand (&) puts the whole job into background.
(Of course, the file names used above are not important -- these are just examples to
illustrate the format of the command string.)
A quick tutorial on Matlab is available in the next Info node in this file. (Touch the "n"
key to go there now, or return to the menu in the Top node for this file.)

MATLAB Tutorial
MATLAB Tutorial (based on work of R. Smith,
November 1988 and later)
This is an interactive introduction to MATLAB. I have provided a sequence of
commands for you to type in. The designation RET means that you should type the
"return" key; this is implicit after a command.
To bring up MATLAB from from the operating system prompt
lab%

you should type matlab
lab% matlab RET

This will present the prompt
>>

You are now in MATLAB.
If you are using the X Window system on the Mathematics Department workstations then
you can also start MATLAB from the Main Menu by selecting "matlab" from the "Math
Applications" submenu. A window should pop up and start MATLAB. When you run
MATLAB under the window system, whether you start from the menu or a system
prompt, a small MATLAB logo window will pop up while the program is loading and
disappear when MATLAB is ready to use.
When you are ready to leave, type exit
>> exit RET
In the course of the tutorial if you get stuck on what a command means type
>> help <command name> RET

and then try the command again.
You should record the outcome of the commands and experiments in a notebook.
Remark: Depending on the Info reader you are using to navigate this tutorial, you might
be able to cut and paste many of the examples directly into Matlab.

Building Matrices
Matlab has many types of matrices which are built into the system. A 7 by 7 matrix with
random entries is produced by typing
rand(7)

You can generate random matrices of other sizes and get help on the rand command
within matlab:
rand(2,5)
help rand

Another special matrix, called a Hilbert matrix, is a standard example in numerical linear
algebra.
hilb(5)
help hilb

A 5 by 5 magic square is given by the next command:
magic(5)
help magic

A magic square is a square matrix which has equal sums along all its rows and columns.
We'll use matrix multiplication to check this property a bit later.
Some of the standard matrices from linear algebra are easily produced:
eye(6)
zeros(4,7)
ones(5)
You can also build matrices of your own with any entries that you may want.
[1

2

3

5

7

9]

[1, 2, 3; 4, 5, 6; 7, 8, 9]
[1

2 RET

3

4 RET 5

6]

[Note that if you are using cut-and-paste features of a window system or editor to copy
these examples into Matlab then you should not use cut-and-paste and the last line above.
Type it in by hand, touching the Return or Enter key where you see RET, and check to see
whether the carriage returns make any difference in Matlab's output.]
Matlab syntax is convenient for blocked matrices:
[eye(2);zeros(2)]
[eye(2);zeros(3)]
[eye(2),ones(2,3)]

Did any of the last three examples produce error messages? What is the problem?

Variables
Matlab has built-in variables like pi, eps, and ans. You can learn their values from the
Matlab interpreter.
pi
eps
help eps

At any time you want to know the active variables you can use who:
who
help

who

The variable ans will keep track of the last output which was not assigned to another
variable.
magic(6)
ans
x = ans
x = [x, eye(6)]
x

Since you have created a new variable, x, it should appear as an active variable.
who

To remove a variable, try this:
clear x
x
who

Functions
a = magic(4)

Take the transpose of a:
a'

Note that if the matrix A has complex numbers as entries then the Matlab function taking
A to A' will compute the transpose of the conjugate of A rather than the transpose of A.
Other arithmetic operations are easy to perform.
3*a
-a
a+(-a)
b = max(a)
max(b)
Some Matlab functions can return more than one value. In the case of max the interpreter
returns the maximum value and also the column index where the maximum value occurs.
[m, i] = max(b)
min(a)
b = 2*ones(a)
a*b
a

We can use matrix multiplication to check the "magic" property of magic squares.
A = magic(5)
b = ones(5,1)
A*b
v = ones(1,5)
v*A

Matlab has a convention in which a dot in front of an operation usually changes the
operation. In the case of multiplication, a.*b will perform entry-by-entry multiplic
Some Matlab functions can return more than one value. In the case of max the interpreter
returns the maximum value and also the column index where the maximum value occurs.
[m, i] = max(b)
min(a)
b = 2*ones(a)
a*b
a

We can use matrix multiplication to check the "magic" property of magic squares.
A = magic(5)
b = ones(5,1)
A*b
v = ones(1,5)
v*A

Matlab has a convention in which a dot in front of an operation usually changes the
operation. In the case of multiplication, a.*b will perform entry-by-entry multiplic

More Related Content

DOC
Matlab summary
PPT
How to work on Matlab.......
PPTX
Programming Environment in Matlab
PDF
Matlab introduction lecture 1
PPT
Brief Introduction to Matlab
PPTX
Matlab introduction
PPTX
All About MATLAB
PPSX
Matlab basic and image
Matlab summary
How to work on Matlab.......
Programming Environment in Matlab
Matlab introduction lecture 1
Brief Introduction to Matlab
Matlab introduction
All About MATLAB
Matlab basic and image

What's hot (20)

PDF
Matlab Tutorial for Beginners - I
PPTX
Matlab for diploma students(1)
PPTX
Introduction to MATLAB
PPT
Matlab Introduction
DOCX
MATLAB BASICS
PDF
MATLAB INTRODUCTION
PPTX
Importance of matlab
PPT
Introduction to matlab
PPT
Introduction to Matlab
PPTX
Introduction to matlab lecture 1 of 4
PPT
Introduction to matlab
PPT
Matlab Overviiew
PDF
Introduction to Matlab
PPT
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
PPTX
Matlab 1(operations on_matrix)
PDF
EE6711 Power System Simulation Lab manual
PDF
Introduction to MATLAB
PPTX
Matlab
PPTX
Basic matlab and matrix
PPTX
Matlab ppt
Matlab Tutorial for Beginners - I
Matlab for diploma students(1)
Introduction to MATLAB
Matlab Introduction
MATLAB BASICS
MATLAB INTRODUCTION
Importance of matlab
Introduction to matlab
Introduction to Matlab
Introduction to matlab lecture 1 of 4
Introduction to matlab
Matlab Overviiew
Introduction to Matlab
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
Matlab 1(operations on_matrix)
EE6711 Power System Simulation Lab manual
Introduction to MATLAB
Matlab
Basic matlab and matrix
Matlab ppt
Ad

Similar to Matlab tut2 (20)

PPTX
From zero to MATLAB hero: Mastering the basics and beyond
PDF
interfacing matlab with embedded systems
PDF
PDF
Matlab guide
DOCX
Introduction to matlab
PPT
Introduction to Matlab.ppt
PPT
Matlab practical and lab session
PDF
Introduction to Matlab for Engineering & Science Students.pdf
PPTX
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
PPT
Matlab anilkumar
PDF
MATLAB Basics-Part1
DOCX
Mmc manual
PDF
Basic matlab for beginners
DOCX
MATLAB guide
PPTX
Matlab Introduction
DOCX
KEVIN MERCHANT DOCUMENT
DOCX
Kevin merchantss
PPTX
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
PPTX
Chapter 1 _ Introduction to Matlab.pptx
PPTX
Matlab 1 level_1
From zero to MATLAB hero: Mastering the basics and beyond
interfacing matlab with embedded systems
Matlab guide
Introduction to matlab
Introduction to Matlab.ppt
Matlab practical and lab session
Introduction to Matlab for Engineering & Science Students.pdf
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
Matlab anilkumar
MATLAB Basics-Part1
Mmc manual
Basic matlab for beginners
MATLAB guide
Matlab Introduction
KEVIN MERCHANT DOCUMENT
Kevin merchantss
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Chapter 1 _ Introduction to Matlab.pptx
Matlab 1 level_1
Ad

More from Vinnu Vinay (10)

DOC
Matlab tut3
DOC
Lesson 8
DOC
Lesson 7
DOC
Lesson 6
DOC
Lesson 5
DOC
Lesson 4
DOC
Lesson 3
DOC
Lesson 2
DOC
matlab Lesson 1
DOC
Matlabtut1
Matlab tut3
Lesson 8
Lesson 7
Lesson 6
Lesson 5
Lesson 4
Lesson 3
Lesson 2
matlab Lesson 1
Matlabtut1

Recently uploaded (20)

PPTX
sap open course for s4hana steps from ECC to s4
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Cloud computing and distributed systems.
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
KodekX | Application Modernization Development
PDF
Machine learning based COVID-19 study performance prediction
PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
sap open course for s4hana steps from ECC to s4
The AUB Centre for AI in Media Proposal.docx
Cloud computing and distributed systems.
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Network Security Unit 5.pdf for BCA BBA.
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectroscopy.pptx food analysis technology
Review of recent advances in non-invasive hemoglobin estimation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
KodekX | Application Modernization Development
Machine learning based COVID-19 study performance prediction
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

Matlab tut2

  • 1. MATLAB Summary MATLAB (matrix algebra) Matlab is a commercial "Matrix Laboratory" package which operates as an interactive programming environment. It is a mainstay of the Mathematics Department software lineup and is also available for PC's and Macintoshes and may be found on the CIRCA VAXes. Matlab is well adapted to numerical experiments since the underlying algorithms for Matlab's builtin functions and supplied m-files are based on the standard libraries LINPACK and EISPACK. Matlab program and script files always have filenames ending with ".m"; the programming language is exceptionally straightforward since almost every data object is assumed to be an array. Graphical output is available to supplement numerical results. Online help is available from the Matlab prompt (a double arrow), both generally (listing all available commands): >> help [a long list of help topics follows] and for specific commands: >> help fft [a help message on the fft function follows]. Paper documentation is on the document shelf in compact black books and locally generated tutorials are available and are used in courses. How to quit Matlab The answer to the most popular question concerning any program is this: leave a Matlab session by typing quit or by typing exit to the Matlab prompt. Batch jobs Matlab is most often used interactively, but "batch" or "background" jobs can be performed as well. Debug your commands interactively and store them in a file (`script.m', for example). To start a background session from your input file and to put the output and error messages into another file (`script.out', for example), enter this line at the system prompt: nice matlab < script.m >& script.out & You can then do other work at the machine or logout while Matlab grinds out your program. Here's an explanation of the sequence of commands above.
  • 2. 1. The "nice" command lowers matlab's priority so that interactive users have first crack at the CPU. You must do this for noninteractive Matlab sessions because of the load that number--crunching puts on the CPU. 2. The "< script.m" means that input is to be read from the file script.m. 3. The ">& script.out" is an instruction to send program output and error output to the file script.out. (It is important to include the first ampersand (&) so that error messages are sent to your file rather than to the screen -- if you omit the ampersand then your error messages may turn up on other people's screens and your popularity will plummet.) 4. Finally, the concluding ampersand (&) puts the whole job into background. (Of course, the file names used above are not important -- these are just examples to illustrate the format of the command string.) A quick tutorial on Matlab is available in the next Info node in this file. (Touch the "n" key to go there now, or return to the menu in the Top node for this file.) MATLAB Tutorial MATLAB Tutorial (based on work of R. Smith, November 1988 and later) This is an interactive introduction to MATLAB. I have provided a sequence of commands for you to type in. The designation RET means that you should type the "return" key; this is implicit after a command. To bring up MATLAB from from the operating system prompt lab% you should type matlab lab% matlab RET This will present the prompt >> You are now in MATLAB. If you are using the X Window system on the Mathematics Department workstations then you can also start MATLAB from the Main Menu by selecting "matlab" from the "Math Applications" submenu. A window should pop up and start MATLAB. When you run MATLAB under the window system, whether you start from the menu or a system prompt, a small MATLAB logo window will pop up while the program is loading and disappear when MATLAB is ready to use. When you are ready to leave, type exit >> exit RET
  • 3. In the course of the tutorial if you get stuck on what a command means type >> help <command name> RET and then try the command again. You should record the outcome of the commands and experiments in a notebook. Remark: Depending on the Info reader you are using to navigate this tutorial, you might be able to cut and paste many of the examples directly into Matlab. Building Matrices Matlab has many types of matrices which are built into the system. A 7 by 7 matrix with random entries is produced by typing rand(7) You can generate random matrices of other sizes and get help on the rand command within matlab: rand(2,5) help rand Another special matrix, called a Hilbert matrix, is a standard example in numerical linear algebra. hilb(5) help hilb A 5 by 5 magic square is given by the next command: magic(5) help magic A magic square is a square matrix which has equal sums along all its rows and columns. We'll use matrix multiplication to check this property a bit later. Some of the standard matrices from linear algebra are easily produced: eye(6) zeros(4,7) ones(5)
  • 4. You can also build matrices of your own with any entries that you may want. [1 2 3 5 7 9] [1, 2, 3; 4, 5, 6; 7, 8, 9] [1 2 RET 3 4 RET 5 6] [Note that if you are using cut-and-paste features of a window system or editor to copy these examples into Matlab then you should not use cut-and-paste and the last line above. Type it in by hand, touching the Return or Enter key where you see RET, and check to see whether the carriage returns make any difference in Matlab's output.] Matlab syntax is convenient for blocked matrices: [eye(2);zeros(2)] [eye(2);zeros(3)] [eye(2),ones(2,3)] Did any of the last three examples produce error messages? What is the problem? Variables Matlab has built-in variables like pi, eps, and ans. You can learn their values from the Matlab interpreter. pi eps help eps At any time you want to know the active variables you can use who: who help who The variable ans will keep track of the last output which was not assigned to another variable. magic(6)
  • 5. ans x = ans x = [x, eye(6)] x Since you have created a new variable, x, it should appear as an active variable. who To remove a variable, try this: clear x x who Functions a = magic(4) Take the transpose of a: a' Note that if the matrix A has complex numbers as entries then the Matlab function taking A to A' will compute the transpose of the conjugate of A rather than the transpose of A. Other arithmetic operations are easy to perform. 3*a -a a+(-a) b = max(a) max(b)
  • 6. Some Matlab functions can return more than one value. In the case of max the interpreter returns the maximum value and also the column index where the maximum value occurs. [m, i] = max(b) min(a) b = 2*ones(a) a*b a We can use matrix multiplication to check the "magic" property of magic squares. A = magic(5) b = ones(5,1) A*b v = ones(1,5) v*A Matlab has a convention in which a dot in front of an operation usually changes the operation. In the case of multiplication, a.*b will perform entry-by-entry multiplic
  • 7. Some Matlab functions can return more than one value. In the case of max the interpreter returns the maximum value and also the column index where the maximum value occurs. [m, i] = max(b) min(a) b = 2*ones(a) a*b a We can use matrix multiplication to check the "magic" property of magic squares. A = magic(5) b = ones(5,1) A*b v = ones(1,5) v*A Matlab has a convention in which a dot in front of an operation usually changes the operation. In the case of multiplication, a.*b will perform entry-by-entry multiplic