Dear Student in this post, I discuss introduction to MATLAB bascis. One can learn MATLAB by following the link in.mathworks.com also.
Introduction
MATLAB(short name for MATrix LABoratory) is a special-purpose computer program optimized to perform engineering and scientific calculations. It started life as a program designed to perform matrix mathematics, but over the years it has grown into a flexible computing system capable of solving essentially any technical problem.
The fundamental unit of data in any MATLAB program is the array.An array is a collection of data values organised into rows and columns, and known by a single name. Individual data values within array may be accessed by including the name of the array followed by subscripts in parentheses that identify the row and column of the particular value.
The MATLAB Desktop
When you start MATLAB, a special window called MATLAB desktop appears. The default configuration of the MATLAB desktop is shown in figure. It integrates many tools for managing files, variables, and applications within the MATLAB environment. The major tools within or accessible from the MATLAB desktop are the following:
The middle of the MATLAB desktop contains the Command Window. A user can enter interactive commands at the command prompt ($\texttt{>>}$)in the Command Window, and they will be executed on the spot.
Suppose you type:
$\texttt{>>}$ area = pi * 2.5^2
area =
19.6350
MATLAB calculates the answer as soon as the Enter key is pressed and stores the answer in a variable ($1 \times 1$ array) called $\texttt{area}$
If a statement is too long to type on a single line, it may be continued on successive lines by typing an $\textbf{ellipsis}$($\ldots$) at the end of the first line, and then continuing the next line. For example, the following two statements are identical.
x1 = 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6;
x1 = 1 + 1/2 + 1/3 + 1/4 ...
+ 1/5 + 1/6;
A series of commands may be placed into a file, and then the entire file may be executed by typing its name in the Command Window. Such files are called $\textbf{script files}$. Script files are also known as M-files because they have a file extension of ".m"
Variables and Arrays
The fundamental unit of data in any MATLAB program is the $\textbf{array}$. Arrays can be classified as either $\textbf{vectors}$ or $\textbf{matrices}$. The $\textbf{size}$ of an array is specified by the number of rows and the number of columns in the array, with the number of rows mentioned first. The total number of elements in the array will be rows $\times$ columns. A MATLAB variable is a region of memory containing an array, which is known by a user-specified name. MATLAB variable names must begin with a letter, followed by any combination of letters,numbers, and the underscore ($\_$)character.
Initializing Variables
There are three common ways to initializing a variable in MATLAB.
See the Video for Assign Statement:
If an assignment statement is typed without the semicolon, the results of the statement are automatically displayed in the Command Window.
Shortcut Expressions: MATLAB provides a special shortcut notations when the array contains hundreds or even thousands of elements. The general expression is $\texttt{first:incr:last}$
Built-in Functions: Arrays can also be initialized using built-in MATLAB FUNCTIONS
$\texttt{zeros(n)}$ Generates an $n \times n$ matrix of zeros.
$\texttt{zeros(n, m)}$ Generates an $n \times m$ matrix of zeros.
$\texttt{zeros(size(arr))}$ Generates a matrix of zeros of the same size as arr.
$\texttt{ones(n)}$ Generates an $n \times n$ matrix of ones.
$\texttt{ones(n, m)}$ Generates an $n \times m$ matrix of ones.
$\texttt{ones(size(arr))}$ Generates a matrix of ones of the same size as arr.
$\texttt{eye(n)}$Generates an $n \times n$ identity matrix.
$\texttt{eye(n, m)}$ Generates an $n \times m$ identity matrix.
$\texttt{length(arr)}$ Returns the length of a vector or the longest dimension of a 2-D array.
$\texttt{size(arr)}$Returns two values specifying the number of rows and columns in arr.
$\texttt{reshape(A,m,n)}$ Rearrange a matrix A that has $r$ rows and $s$ columns to have $m$ rows and $n$ columns. $r \times s$ must be equal to $m \times n$.
$\texttt{diag(v)}$ When v is a vector creates a square matrix with the elements of v in the diagonal.
$\texttt{diag(A)}$ When A is matrix, creates a vector from the diagonal elements of $\texttt{A}$.
$\texttt{length(v)}$ Returns the number of elements in the vector $\texttt{v}$.
Keyboard Input
It is also possible to prompt a user and initialize a variable with data that he or she types directly at the keyboard by using $\texttt{input}$ function.
If $\texttt{input}$ function includes the character 's' as a second argument, then the input data is returned to the user as a character string.
Multidimensional Arrays
Multidimensional Arrays have one subscript for each dimension,and individual element is selected by specifying a value for each subscript. The total number of elements in the array will be the product of the maximum value of each subscript.
SubArrays
To select a portion of an array, just include a list of all the elements to be selected in the parentheses after the array name. When used in array subscript the $\texttt{end}$ function returns the highest value taken by that a subscript.
$\texttt{va(:)}$ Refers to all the elements of the vector $\texttt{va}$ (either a row or a column vector).
$\texttt{va(m:n)}$ Refers to elements $m$ through $n$ of the vector $\texttt{va}$.
$\texttt{A(:,n)}$ Refers to the elements in all the rows of column $n$ of the matrix $\texttt{A}$.
$\texttt{A(n,:)}$ Refers to the elements in all the columns of row $n$ of the matrix $\texttt{A}$.
$\texttt{A(:,m:n)}$ Refers to the elements in all the rows between columns $m$ and $n$ of the matrix $\texttt{A}$.
$\texttt{A(m:n,:)}$ Refers to the elements in all the columns between rows $m$ and $n$ of the matrix $\texttt{A}$.
$\texttt{A(m:n,p:q)}$ & Refers to the elements in rows $m$ through $n$ and columns $p$ through $q$ of the matrix $\texttt{A}$
Display Format and Function
The $\texttt{format}$ command changes the default format. The $\texttt{disp}$ function accepts an array argument and display the value of array in the Command Window. $\texttt{fprintf}$ function displays one or more values together with related text.
Output Display Formats
$\texttt{format short}$ 4 digits after decimal (default format)
$\texttt{format long}$ digits after decimal
$\texttt{format short e}$5 digits plus exponent
$\texttt{format short g}$ 5 total digits with or without exponent
$\texttt{format long e}$ 15 digits plus exponent
$\texttt{format long g }$ 15 total digits with or without exponent
$\texttt{format bank}$ dollars and cents format
$\texttt{format hex}$ hexadecimal display of bits
$\texttt{format rat}$ approximate ratio of small integers
$\texttt{format compact}$ suppress extra line feeds
$\texttt{format loose}$ restore extra line feeds
$\texttt{format +}$ Only the signs of the numbers are printed
Scalar and Array Operations
Calculations are specified in MATLAB with an assignment statement:
$\texttt{variable_name = expression;}$
Array and Matrix Operations
Array Addition : $\texttt{a + b}$ Array addition and matrix addition are identical.
Array Subtraction: $\texttt{a - b}$ Array subtraction and matrix subtraction are identical.
Array Multiplication: $\texttt{a . * b}$ Element-by-element multiplication of $\texttt{a}$ and $\texttt{b}$. Both arrays must be the same shape, or one of them must be a scalar.
Matrix Multiplication: $\texttt{a * b}$ Matrix multiplication of $\texttt{a}$ and $\texttt{b}$. The number of columns in $\texttt{a}$ must equal the number of rows in $\texttt{b}$.
Array Right Division: $\texttt{a . / b}$ Element-by-element division of $\texttt{a}$ and $\texttt{b}$: $\texttt{a (i , j ) / b (i, j )}$. Both arrays must be the same shape, or one of them must be a scalar.
Array Left Division: $\texttt{a . \ b}$ Element-by-element division of $\texttt{a}$ and $\texttt{b}$, but with $\texttt{b}$ in the numerator: $\texttt{b (i, j ) /a (i, j )}$. Both arrays must be the same shape, or one of them must be a scalar.
Matrix Right Division: $\texttt{a / b}$ Matrix division defined by $\texttt{a *inv(b)}$, where $\texttt{inv (b)}$ is the inverse of matrix $\texttt{b}$.
Matrix Left Division: $\texttt{a $\smallsetminus$ b}$ Matrix division defined by $\texttt{inv (a) * b}$, where $\texttt{inv (a)}$ is the inverse of matrix $\texttt{a}$.
Hierarchy of Arithmetic Operations
MATLAB has established a series of rules governing the hierarchy, or order, in which operations are calculated within an expression.
Built-in MATLAB Functions and Constants
One of MATLAB's greatest strengths is that it comes with an incredible variety of built-in functions for use.
$\texttt{pi}$ Contains $\pi$ to 15 significant digits.
$\texttt{i, j}$ Contain the value $i$ $\sqrt{-1}$.
$\texttt{Inf}$ This symbol represents machine infinity. It is usually generated as a result of a division by 0.
$\texttt{NaN}$ This symbol stands for Not-a-Number. It is the result of an undefined mathematical operation, such as the division of zero by zero.
$\texttt{clock}$ This special variable contains the current date and time in the form of a 6-element row vector containing the year, month, day, hour, minute, and second.
$\texttt{date}$ Contains the current data in a character string format, such as $\texttt{24-Nov-1999}$.
$\texttt{eps}$ This variable name is short for epsilon. It is the smallest difference between two numbers that can be represented on the computer.
$\texttt{ans}$ A special variable used to store the result of an expression if that result is not explicitly assigned to some other variable.
Plotting Data (Two dimensional Plots)
To plot a data set, create two vectors containing the $x$ and $y$ values to be plotted, and use the $\texttt{plot}$ function.
Multiple Plots Suppose that we want to plot the function $f(x)=\sin(2x)$ and its derivative $\dfrac{d}{dx}\sin(2x)=2\cos(2x)$
MATLAB Program:MATLAB Script
Temperature conversion Program
The relationship between temperature in degree Fahrenheit $(^{0}F)$ and temperature in kelvin(K) can be given by $T(K) = \dfrac{5}{9}\left(T(^{0}F) - 32.0\right)+273.15$ The evaluation of this formula can be done by creating a script file temp$\_$conversion.m (say)
Introduction
MATLAB(short name for MATrix LABoratory) is a special-purpose computer program optimized to perform engineering and scientific calculations. It started life as a program designed to perform matrix mathematics, but over the years it has grown into a flexible computing system capable of solving essentially any technical problem.
The fundamental unit of data in any MATLAB program is the array.An array is a collection of data values organised into rows and columns, and known by a single name. Individual data values within array may be accessed by including the name of the array followed by subscripts in parentheses that identify the row and column of the particular value.
The MATLAB Desktop
When you start MATLAB, a special window called MATLAB desktop appears. The default configuration of the MATLAB desktop is shown in figure. It integrates many tools for managing files, variables, and applications within the MATLAB environment. The major tools within or accessible from the MATLAB desktop are the following:
- The Command Window
- The Command History Window
- The Edit/Debug Window
- Figure Windows
- Workspace Browser and Array Editor
- Help Browser
- Current Directory Browser
The middle of the MATLAB desktop contains the Command Window. A user can enter interactive commands at the command prompt ($\texttt{>>}$)in the Command Window, and they will be executed on the spot.
Suppose you type:
$\texttt{>>}$ area = pi * 2.5^2
area =
19.6350
MATLAB calculates the answer as soon as the Enter key is pressed and stores the answer in a variable ($1 \times 1$ array) called $\texttt{area}$
If a statement is too long to type on a single line, it may be continued on successive lines by typing an $\textbf{ellipsis}$($\ldots$) at the end of the first line, and then continuing the next line. For example, the following two statements are identical.
x1 = 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6;
x1 = 1 + 1/2 + 1/3 + 1/4 ...
+ 1/5 + 1/6;
A series of commands may be placed into a file, and then the entire file may be executed by typing its name in the Command Window. Such files are called $\textbf{script files}$. Script files are also known as M-files because they have a file extension of ".m"
Variables and Arrays
The fundamental unit of data in any MATLAB program is the $\textbf{array}$. Arrays can be classified as either $\textbf{vectors}$ or $\textbf{matrices}$. The $\textbf{size}$ of an array is specified by the number of rows and the number of columns in the array, with the number of rows mentioned first. The total number of elements in the array will be rows $\times$ columns. A MATLAB variable is a region of memory containing an array, which is known by a user-specified name. MATLAB variable names must begin with a letter, followed by any combination of letters,numbers, and the underscore ($\_$)character.
Initializing Variables
There are three common ways to initializing a variable in MATLAB.
- Assign data to the variable in an assignment statement.
- Input data into the variable from the keyboard.
- Read data from file.
See the Video for Assign Statement:
If an assignment statement is typed without the semicolon, the results of the statement are automatically displayed in the Command Window.
Shortcut Expressions: MATLAB provides a special shortcut notations when the array contains hundreds or even thousands of elements. The general expression is $\texttt{first:incr:last}$
Built-in Functions: Arrays can also be initialized using built-in MATLAB FUNCTIONS
$\texttt{zeros(n)}$ Generates an $n \times n$ matrix of zeros.
$\texttt{zeros(n, m)}$ Generates an $n \times m$ matrix of zeros.
$\texttt{zeros(size(arr))}$ Generates a matrix of zeros of the same size as arr.
$\texttt{ones(n)}$ Generates an $n \times n$ matrix of ones.
$\texttt{ones(n, m)}$ Generates an $n \times m$ matrix of ones.
$\texttt{ones(size(arr))}$ Generates a matrix of ones of the same size as arr.
$\texttt{eye(n)}$Generates an $n \times n$ identity matrix.
$\texttt{eye(n, m)}$ Generates an $n \times m$ identity matrix.
$\texttt{length(arr)}$ Returns the length of a vector or the longest dimension of a 2-D array.
$\texttt{size(arr)}$Returns two values specifying the number of rows and columns in arr.
$\texttt{reshape(A,m,n)}$ Rearrange a matrix A that has $r$ rows and $s$ columns to have $m$ rows and $n$ columns. $r \times s$ must be equal to $m \times n$.
$\texttt{diag(v)}$ When v is a vector creates a square matrix with the elements of v in the diagonal.
$\texttt{diag(A)}$ When A is matrix, creates a vector from the diagonal elements of $\texttt{A}$.
$\texttt{length(v)}$ Returns the number of elements in the vector $\texttt{v}$.
Keyboard Input
It is also possible to prompt a user and initialize a variable with data that he or she types directly at the keyboard by using $\texttt{input}$ function.
If $\texttt{input}$ function includes the character 's' as a second argument, then the input data is returned to the user as a character string.
Multidimensional Arrays
Multidimensional Arrays have one subscript for each dimension,and individual element is selected by specifying a value for each subscript. The total number of elements in the array will be the product of the maximum value of each subscript.
SubArrays
To select a portion of an array, just include a list of all the elements to be selected in the parentheses after the array name. When used in array subscript the $\texttt{end}$ function returns the highest value taken by that a subscript.
$\texttt{va(:)}$ Refers to all the elements of the vector $\texttt{va}$ (either a row or a column vector).
$\texttt{va(m:n)}$ Refers to elements $m$ through $n$ of the vector $\texttt{va}$.
$\texttt{A(:,n)}$ Refers to the elements in all the rows of column $n$ of the matrix $\texttt{A}$.
$\texttt{A(n,:)}$ Refers to the elements in all the columns of row $n$ of the matrix $\texttt{A}$.
$\texttt{A(:,m:n)}$ Refers to the elements in all the rows between columns $m$ and $n$ of the matrix $\texttt{A}$.
$\texttt{A(m:n,:)}$ Refers to the elements in all the columns between rows $m$ and $n$ of the matrix $\texttt{A}$.
$\texttt{A(m:n,p:q)}$ & Refers to the elements in rows $m$ through $n$ and columns $p$ through $q$ of the matrix $\texttt{A}$
Display Format and Function
The $\texttt{format}$ command changes the default format. The $\texttt{disp}$ function accepts an array argument and display the value of array in the Command Window. $\texttt{fprintf}$ function displays one or more values together with related text.
Output Display Formats
$\texttt{format short}$ 4 digits after decimal (default format)
$\texttt{format long}$ digits after decimal
$\texttt{format short e}$5 digits plus exponent
$\texttt{format short g}$ 5 total digits with or without exponent
$\texttt{format long e}$ 15 digits plus exponent
$\texttt{format long g }$ 15 total digits with or without exponent
$\texttt{format bank}$ dollars and cents format
$\texttt{format hex}$ hexadecimal display of bits
$\texttt{format rat}$ approximate ratio of small integers
$\texttt{format compact}$ suppress extra line feeds
$\texttt{format loose}$ restore extra line feeds
$\texttt{format +}$ Only the signs of the numbers are printed
Scalar and Array Operations
Calculations are specified in MATLAB with an assignment statement:
$\texttt{variable_name = expression;}$
Array and Matrix Operations
Array Addition : $\texttt{a + b}$ Array addition and matrix addition are identical.
Array Subtraction: $\texttt{a - b}$ Array subtraction and matrix subtraction are identical.
Array Multiplication: $\texttt{a . * b}$ Element-by-element multiplication of $\texttt{a}$ and $\texttt{b}$. Both arrays must be the same shape, or one of them must be a scalar.
Matrix Multiplication: $\texttt{a * b}$ Matrix multiplication of $\texttt{a}$ and $\texttt{b}$. The number of columns in $\texttt{a}$ must equal the number of rows in $\texttt{b}$.
Array Right Division: $\texttt{a . / b}$ Element-by-element division of $\texttt{a}$ and $\texttt{b}$: $\texttt{a (i , j ) / b (i, j )}$. Both arrays must be the same shape, or one of them must be a scalar.
Array Left Division: $\texttt{a . \ b}$ Element-by-element division of $\texttt{a}$ and $\texttt{b}$, but with $\texttt{b}$ in the numerator: $\texttt{b (i, j ) /a (i, j )}$. Both arrays must be the same shape, or one of them must be a scalar.
Matrix Right Division: $\texttt{a / b}$ Matrix division defined by $\texttt{a *inv(b)}$, where $\texttt{inv (b)}$ is the inverse of matrix $\texttt{b}$.
Matrix Left Division: $\texttt{a $\smallsetminus$ b}$ Matrix division defined by $\texttt{inv (a) * b}$, where $\texttt{inv (a)}$ is the inverse of matrix $\texttt{a}$.
Hierarchy of Arithmetic Operations
MATLAB has established a series of rules governing the hierarchy, or order, in which operations are calculated within an expression.
Built-in MATLAB Functions and Constants
One of MATLAB's greatest strengths is that it comes with an incredible variety of built-in functions for use.
$\texttt{pi}$ Contains $\pi$ to 15 significant digits.
$\texttt{i, j}$ Contain the value $i$ $\sqrt{-1}$.
$\texttt{Inf}$ This symbol represents machine infinity. It is usually generated as a result of a division by 0.
$\texttt{NaN}$ This symbol stands for Not-a-Number. It is the result of an undefined mathematical operation, such as the division of zero by zero.
$\texttt{clock}$ This special variable contains the current date and time in the form of a 6-element row vector containing the year, month, day, hour, minute, and second.
$\texttt{date}$ Contains the current data in a character string format, such as $\texttt{24-Nov-1999}$.
$\texttt{eps}$ This variable name is short for epsilon. It is the smallest difference between two numbers that can be represented on the computer.
$\texttt{ans}$ A special variable used to store the result of an expression if that result is not explicitly assigned to some other variable.
Plotting Data (Two dimensional Plots)
To plot a data set, create two vectors containing the $x$ and $y$ values to be plotted, and use the $\texttt{plot}$ function.
Multiple Plots Suppose that we want to plot the function $f(x)=\sin(2x)$ and its derivative $\dfrac{d}{dx}\sin(2x)=2\cos(2x)$
MATLAB Program:MATLAB Script
Temperature conversion Program
The relationship between temperature in degree Fahrenheit $(^{0}F)$ and temperature in kelvin(K) can be given by $T(K) = \dfrac{5}{9}\left(T(^{0}F) - 32.0\right)+273.15$ The evaluation of this formula can be done by creating a script file temp$\_$conversion.m (say)
Dear Student,after watching these videos, you can write simple MATLAB programs from your area of study. The above videos show basic commands and their respective outputs which are summarized in following document with additional commands for your reference. Send your comments about these posts.
No comments:
Post a Comment