0% found this document useful (0 votes)
47 views

Command MATLAB

The document defines an algorithm as a set of rules or instructions to solve a problem in a step-by-step manner to produce expected results. An algorithm should be clear, have well-defined inputs and outputs, be finite, feasible, and language independent. Algorithms are advantageous as they are easy to understand and break problems into smaller steps, but writing them can be time-consuming.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Command MATLAB

The document defines an algorithm as a set of rules or instructions to solve a problem in a step-by-step manner to produce expected results. An algorithm should be clear, have well-defined inputs and outputs, be finite, feasible, and language independent. Algorithms are advantageous as they are easy to understand and break problems into smaller steps, but writing them can be time-consuming.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

The word 

Algorithm means “a process or set of rules to be followed in calculations or other problem-solving


operations”. Therefore, Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be
executed upon in order to get the expected results. 
•Clear and Unambiguous: Algorithm should be clear and unambiguous. Each of its steps should be clear in all
aspects and must lead to only one meaning.
•Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs.
•Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well-defined
as well.
•Finite-ness: The algorithm must be finite, i.e. it should not end up in an infinite loops or similar.
•Feasible: The algorithm must be simple, generic and practical, such that it can be executed upon will the available
resources. It must not contain some future technology, or anything.
•Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain
instructions that can be implemented in any language, and yet the output will be same, as expected.
Advantages of Algorithms:
•It is easy to understand.
•Algorithm is a step-wise representation of a solution to a given problem.
•In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to
convert it into an actual program.
Disadvantages of Algorithms:
•Writing an algorithm takes a long time so it is time-consuming.
•Branching and Looping statements are difficult to show in Algorithms.
MATLAB
MATLAB is a programming language developed by MathWorks. 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.
The desktop has the following panels −

•Current Folder − This panel allows you to access the project folders and files

Command Window − This is the main area where commands can be entered at the command line. It is indicated by the command
prompt (>>)
Workspace − The workspace shows all the variables created and/or imported from files

Command History − This panel shows or return commands that are entered at the command line
Commonly used Operators and Special Characters
MATLAB supports the following commonly used operators and special characters −

Operator Purpose Operator Purpose


+ Plus; addition operator. … Ellipsis; line-continuation operator
- Minus; subtraction operator. , Comma; separates statements and elements
* Scalar and matrix multiplication operator. in a row
.* Array multiplication operator. ; Semicolon; separates columns and
suppresses display.
^ Scalar and matrix exponentiation operator.
% Percent sign; designates a comment and
.^ Array exponentiation operator.
specifies formatting.
\ Left-division operator.
_ Quote sign and transpose operator.
/ Right-division operator.
._ Nonconjugated transpose operator.
.\ Array left-division operator.
= Assignment operator.
./ Array right-division operator.
: Colon; generates regularly spaced elements
and represents an entire row or column.
() Parentheses; encloses function arguments
and array indices; overrides precedence.
[] Brackets; enclosures array elements.
. Decimal point.
Special Variables and Constants
MATLAB supports the following special variables and constants −

Name Meaning
ans Most recent answer.
eps Accuracy of floating-point precision.
i,j The imaginary unit √-1.
Inf Infinity.
NaN Undefined numerical result (not a number).
pi The number π
Command Purpose

clc Clears command window.

clear Removes variables from memory.

exist Checks for existence of file or variable.

global Declares variables to be global.

help Searches for a help topic.

lookfor Searches help entries for a keyword.

quit Stops MATLAB.

who Lists current variables.

whos Lists current variables (long display).


Commands for Working with the System
MATLAB provides various useful commands for working with the system, like saving the current work in the workspace as a file
and loading the file later.
It also provides various commands for other system-related activities like, displaying date, listing files in the directory, displaying
current directory, etc. Command Purpose
The following table displays some commonly used system-related 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 and Output Commands

MATLAB provides the following input and output related commands −

Command Purpose Format Purpose


Code
disp Displays contents of an array or string.
%s Format as a string.
fscanf Read formatted data from a file.
%d Format as an integer.
format Controls screen-display format.
%f Format as a floating point value.
fprintf Performs formatted writes to screen or file.
%e Format as a floating point value in scientific
input Displays prompts and waits for input.
notation.
; Suppresses screen printing.
%g Format in the most compact form: %f or %e.
\n Insert a new line in the output string.
\t Insert a tab in the output string.
Format Display up to
Function
format Four decimal digits (default).
short
format 16 decimal digits.
long
format Five digits plus exponent.
short e
format 16 digits plus exponents.
long e
format Two decimal digits.
bank
format + Positive, negative, or zero.
format rat Rational approximation.
format Suppresses some line feeds.
compact
format Resets to less compact display mode
loose
Vector, Matrix and Array Commands
The following table shows various commands used for working with arrays, matrices and vectors −

Command Purpose Command Purpose


cat Concatenates arrays. dot Computes matrix dot products.
find Finds indices of nonzero elements. det Computes determinant of an array.
length Computes number of elements. inv Computes inverse of a matrix.
linspace Creates regularly spaced vector. pinv Computes pseudoinverse of a matrix.
logspace Creates logarithmically spaced vector. rank Computes rank of a matrix.
max Returns largest element. rref Computes reduced row echelon form.
min Returns smallest element. cell Creates cell array.
prod Product of each column. celldisp Displays cell array.
reshape Changes size. cellplot Displays graphical representation of cell array.
size Computes array size. num2cell Converts numeric array to cell array.
sort Sorts each column. deal Matches input and output lists.
sum Sums each column. iscell Identifies cell array.
eye Creates an identity matrix.
ones Creates an array of ones.
zeros Creates an array of zeros.
cross Computes matrix cross products.
Plotting Commands
MATLAB provides numerous commands for plotting graphs. The following table shows some of the commonly used commands
for plotting −
Command Purpose
Command Purpose
gtext Enables label placement by mouse.
axis Sets axis limits.
hold Freezes current plot.
fplot Intelligent plotting of functions. legend Legend placement by mouse.
grid Displays gridlines. refresh Redraws current figure window.
plot Generates xy plot. set Specifies properties of objects such as axes.
print Prints plot or saves plot to a file. subplot Creates plots in subwindows.
title Puts text at top of plot. text Places string in figure.
xlabel Adds text label to x-axis. bar Creates bar chart.
ylabel Adds text label to y-axis. loglog Creates log-log plot.
axes Creates axes objects. polar Creates polar plot.
close Closes the current plot. semilogx Creates semilog plot. (logarithmic abscissa).

close all Closes all plots. semilogy Creates semilog plot. (logarithmic ordinate).

figure Opens a new figure window stairs Creates stairs plot.


stem Creates stem plot.

You might also like