BIL108E In. To Sci. Comp. MATLAB

advertisement
BIL108E In. To Sci. Comp.
MATLAB
by Dr. M. Yılmaz (itumekanik@gmail.com)
MATLAB (Matrix Laboratory)
• MATLAB is a programming language developed by MathWorks
(www.mathworks.com). It started out as a matrix programming
language where linear algebra programming was simple. It can be run
both under interactive sessions and as a batch job.
• Every variable in matlab is a MATRIX.
• Interactive session: Jobs are set up sequentially with human
interaction. (Like term by term scripting)
• Batch job: Jobs are set up so they can be run to completion without
human interaction. (Like ordinary computer programming)
The course Objectives
Learn computer-programming concepts and
their use in Matlab
Learn mathematical concepts and their
representation in Matlab
Code in Matlab to solve some Engineering/Scientific problems
Open Source alternatives of MATLAB
• https://www.gnu.org/software/octave/
• http://octave-online.net/
• http://www.scilab.org/
• http://sourceforge.net/projects/freemat/?source=typ_redirect
Commonly used mathematical calculations
•
•
•
•
•
•
•
•
•
•
•
•
•
Dealing with Matrices and Arrays
2-D and 3-D Plotting and graphics
Linear Algebra
Algebraic Equations
Non-linear Functions
Statistics
Data Analysis
Calculus and Differential Equations
Numerical Calculations
Integration
Transforms
Curve Fitting
Various other special functions
Running MATLAB
• Click Desktop icon
or
• C:\Program Files\MATLAB\R2015b\bin\matlab.exe
Integrated Developmet Environment (IDE)
2
Batch Editor
3
Ctrl+Shift+0
0
1
IDE Pannels
• Command Window − This is the main area where commands can be entered at the
command line. It is indicated by the command prompt (>>). (Ctrl + 0)
• Editor − This is the tabbed window where batch command files and function files are
defined. (Ctrl + Shift+0). To run any batch command select the file and press
(Ctrl+Enter) or press (F5)
• Command History − This panel shows or rerun commands that are entered at the
command line. (Ctrl + 1)
• Current Folder − This panel allows you to access the project folders and files. (Ctrl + 2)
• Workspace − The workspace shows all the variables created and/or imported from
files. (Ctrl + 3)
• Ctrl + Tab -> Swich to next pannel.
• Ctrl + Shift +Tab -> Swich to previous pannel.
HELLO WORLD
>> disp('Hello World')
Hello World
>> clc
(Clear command window.)
>>help clc
>>home
(moves the cursor to the upper left corner of the window.)
Try the following input in Command Window
•
•
•
•
•
•
•
•
•
•
•
•
>> x = 1
>> x
>> X %upper case (Matlab is case sensitive)
>> y = 2;
>> z = x + y;
>> clear x
>> disp(x)
>> str=sprintf(‘z=%d',z)
>> disp(str)
>> pi
>> whos
>>clear all
Commands for Managing a Session
;
Suppresses screen printing.
help
Searches for a help topic.
clc
 Clears Command window.
home Returns a clean command window without erasing last commands.
clear Removes variables from memory.
edit
Open a file in batch editor panel
open  Open a file in batch editor panel
exist
Checks for existence of file or variable.
global Declares variables to be global.
lookfor Searches help entries for a keyword. (Ctrl +C if too much flow)
who
Lists current variables.
whos Lists current variables (long display)
quit
Stops MATLAB.
Special Variables and Constants
• ans
Most recent answer.
eps
Accuracy of floating-point precision.
i,j
The imaginary unit -1.
Inf
Infinity.
nan,NaN Undefined numerical result (not a number).
pi
The number p .
Scalars/Vectors/Matrices
>> a = 1
>> b = [1,2,5,7]
>> c = [1,2 ; 5,7]
>> b’
>> c’
>> d = [1;2;3;4]
>> reshape(b,[2,2])
>> reshape(a,[2,2])’
System and File Commands
cd
Changes current directory.
date
Displays current date.
delete Deletes a file.
diary
Switches on/off diary file recording.
dir
Lists all files in current directory.
load
Loads workspace variables from a file.
path
Displays search path.
pwd
Displays current directory.
save
Saves workspace variables in a file.
type
Displays contents of a file.
what
Lists all MATLAB files in the current directory.
wklread Reads .wk1 spreadsheet file.
Input/Output Commands
disp
Displays contents of an array or string.
fscanf Read formatted data from a file.
format Controls screen-display format.
fprintf Performs formatted writes to screen or file.
input
Displays prompts and waits for input.
%s
%d
%f
%e
%g
\n
\t
Format as a string.
Format as an integer.
Format as a floating point value.
Format as a floating point value in scientific notation.
Format in the most compact form: %f or %e.
Insert a new line in the output string.
Insert a tab in the output string.
Numeric Display Formats
format short Four decimal digits (default).
format long
16 decimal digits.
format short e Five digits plus exponent.
format long e 16 digits plus exponents.
format bank Two decimal digits.
format + Positive, negative, or zero.
format rat
Rational approximation.
format compact Suppresses some line feeds.
format loose
Resets to less compact display mode.
Vector, Matrix and Array Commands
cat
Concatenates arrays.
find Finds indices of nonzero elements.
length Computers number of elements.
linspace
Creates regularly spaced vector.
logspace
Creates logarithmically spaced vector.
max Returns largest element.
min Returns smallest element.
prod Product of each column.
reshape
Change size
size Computes array size.
sort Sorts each column.
sum Sums each column.
Special Vector Matrix Commands
• eye
ones
zeros
cross
dot
det
inv
pinv
rank
rref
Creates an identity matrix.
Creates an array of ones.
Creates an array of zeros.
Computes cross products.
Computes dot products.
Computes determinant of an array.
Computes inverse of a matrix.
Computes pseudoinverse of a matrix.
Computes rank of a matrix.
Computes reduced row echelon form.
Basic xy Plotting Commands
• axis
fplot
grid
plot
print
title
xlabel
ylabel
Sets axis limits.
Intelligent plotting of functions.
Displays gridlines.
Generates xy plot.
Prints plot or saves plot to a file
Puts text at top of plot.
Adds text label to x-axis.
Adds text label to y-axis.
Plot Enhancement Commands
• axes
close
close all
figure
gtext
hold
legend
refresh
set
subplot
text
Creates axes objects.
Closes the current plot.
Closes all plots.
Opens a new figure window.
Enables label placement by mouse.
Freezes current plot.
Legend placement by mouse.
Redraws current figure window.
Specifies properties of objects such as axes.
Creates plots in subwindows.
Places string in figure.
Specialized Plot Commands
• bar
loglog
polar
semilogx
semilogy
stairs
stem
Creates bar chart.
Creates log-log plot.
Creates polar plot.
Creates semilog plot (logarithmic abscissa).
Creates semilog plot (logarithmic ordinate).
Creates stairs pot.
Creates stem plot.
Download