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

MATLAB

MATLAB, developed by MathWorks in 1984, is a high-performance language for technical computing that facilitates matrix manipulations, plotting, and algorithm implementation. It features various components like the Command Window for quick prototyping, an Editor for larger programs, and a Workspace for variable management. The document also covers matrix operations, logical and relational operators, plotting functions, complex number handling, and data types in MATLAB.

Uploaded by

sp729710
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

MATLAB

MATLAB, developed by MathWorks in 1984, is a high-performance language for technical computing that facilitates matrix manipulations, plotting, and algorithm implementation. It features various components like the Command Window for quick prototyping, an Editor for larger programs, and a Workspace for variable management. The document also covers matrix operations, logical and relational operators, plotting functions, complex number handling, and data types in MATLAB.

Uploaded by

sp729710
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Q. 1 Introduction to MATLAB .

Ans. - MATLAB stands for Matrix Laboratory. It is a high-performance


language that is used for technical computing. It was developed by
Cleve Molar of the company MathWorks.Inc in the year 1984.It is
written in C, C++, Java. It allows matrix manipulations, plotting of
functions, implementation of algorithms and creation of user
interfaces.
Getting Started with MATLAB
It is both a programming language as well as a programming
environment. It allows the computation of statements in the
command window itself.
 Command Window: In this window one must type and
immediately execute the statements, as it requires quick
prototyping. These statements cannot be saved. Thus, this is
can be used for small, easily executable programs.
 Editor (Script): In this window one can execute larger programs
with multiple statements, and complex functions These can be
saved and are done with the file extension ‘.m ‘
 Workspace: In this window the values of the variables that are
created in the course of the program (in the editor) are
displayed.
 Command History window : This window displays a log of
statements that you ran in the current and previous MATLAB
sessions. The Command History lists the time and date of each
session in the short date format for your operating system,
followed by the statements from that session.
 This window displays the exact location(path) of the program
file being created.
MATLAB Library comes with a set of many inbuilt functions. These
functions mostly perform mathematical operations like sine, cosine
and tangent. They perform more complex functions too like finding
the inverse and determinant of a matrix, cross product and dot
product Although MATLAB is encoded in C, C++ and Java, it is a lot
easier to implement than these three languages. For example, unlike
the other three, no header files need to be initialised in the
beginning of the document and for declaring a variable, the data type
need not be provided. It provides an easier alternative for vector
operations. They can be performed using one command instead of
multiple statements in a for or while loop.
Q. 2 Working with matrices
Ans. - A matrix is a two-dimensional array of numbers, symbols, or
variables arranged in rows and columns. The dimensions of a matrix
are given as "m x n," where "m" represents the number of rows, and
"n" represents the number of columns.The individual elements of the
matrix are usually referred to by their row and column indices,
denoted as "aᵢⱼ," where "i" indicates the row number and "j" indicates
the column number.
Example
Here A is a 2x3 matrix i.e. it has 2 rows and 3 columns.
A=

1 2 3
4 5 6
Here A11 = 1 , A12 = 2 , A13 = 3. A21 = 4, A22 = 5 and A23 = 6.
Q. 3 Rational and logical operation of matlab.
Ans. MATLAB has rational and logical operators that can be used to
compare values and determine what to do next:
 Rational operators
These include addition, subtraction, multiplication, division, and
exponentiation. All rational operations are done on entire arrays,
using matrix definitions.
 Logical operators
These return a logical array with elements set to true (1) or false
(0). Some examples of logical operators include:
 &: Logical AND
 | Logical OR
 ~A: Complements the elements of A
 xor(A,B): Implements the exclusive OR operation
 Relational operators
These are used to compare values of different variables. Some
examples of relational operators include:
 <: Less than
 <= Less than equal to
 >: Greater than
 >= Greater than or equal to
 == Equal to
 ~=`: Not equal to
You can use the Logical Operator block from the Logic and Bit
Operations library to include a logical operation in your model. You
can also replace the Logical Operator block with a Relational
Operator block
Q. 4 Creating a plot using plot function in matlab
Ans. - Create a simple line plot and label the axes. Customize the
appearance of plotted lines by changing the line color, the line style,
and adding markers.
Create Line Plot
Create a two-dimensional line plot using the plot function. For
example, plot the value of the sine function from 0 to 2π.
Get
x = linspace(0,2*pi,100);
y = sin(x);
plot(x,y)
Label the axes and add a title.
Get
xlabel('x')
ylabel('sin(x)')
title('Plot of the Sine Function')
Q. 5 Complex and stastical function .
Ans. - Syntax
z = complex(a,b)
z = complex(x)
Description
z = complex(a,b) creates a complex output, z, from two real inputs,
such that z = a + bi.
The complex function provides a useful substitute for expressions,
such as a + 1i*b or a + 1j*b, when
 a and b are not double or single
 b is all zeros
example
z = complex(x) returns the complex equivalent of x, such
that isreal(z) returns logical 0 (false).
 If x is real, then z is x + 0i.
 If x is complex, then z is identical to x.
example
Examples
collapse all
Complex Scalar from Two Real Scalars
Open in MATLAB OnlineCopy Code Copy Command
Use the complex function to create the complex scalar, 3 + 4i.
Get
z = complex(3,4)
z=
3.0000 + 4.0000i
Complex Vector from Two Real Vectors
Open in MATLAB OnlineCopy Code Copy Command
Create a complex uint8 vector from two real uint8 vectors. The size
of z, 4-by-1, is the same as the size of the input arguments.
Get
a = uint8([1;2;3;4]);
b = uint8([2;2;7;7]);

z = complex(a,b)
z = 4x1 uint8 column vector

1 + 2i
2 + 2i
3 + 7i
4 + 7i

Complex Scalar from One Real Scalar


Open in MATLAB OnlineCopy Code Copy Command
Create a complex scalar with zero imaginary part.
Get
z = complex(12)
z=
12.0000 + 0.0000i
Verify that z is complex.
Get
isreal(z)
ans = logical
0
Q. 6 Write R data types in matlab.
 Ans. Numeric Types
Integer and floating-point data

 Characters and Strings


Text in character arrays and string arrays

 Dates and Time


Arrays of date and time values that can be displayed in different
formats

 Categorical Arrays
Arrays of qualitative data with values from a finite set of
discrete, nonnumeric data

 Tables
Arrays in tabular form whose named columns can have different
types

 Timetables
Time-stamped data in tabular form

 Structures
Arrays with named fields that can contain data of varying types
and sizes

 Cell Arrays
Arrays that can contain data of varying types and sizes

 Function Handles
Variables that allow you to invoke a function indirectly
 Dictionaries
Map data with keys that index values

 Time Series
Data vectors sampled over time

 Data Type Identification


Determine data type of a variable

 Data Type Conversion


Convert between numeric arrays, strings and character arrays,
dates and times, cell arrays, structures, or tables
Q. 9 Write about R matrix.
Ans. - A Matrix is a two-dimensional array of elements. In MATLAB,
the matrix is created by assigning the array elements that are
delimited by spaces or commas and using semicolons to mark the
end of each row. Now let’s have a glance at some examples to
understand it better.
Syntax:
a = [elements; elements]
Example: Creating a Matrix
 MATLAB

% MATLAB program to create


% a matrix

%Number Matrix
x = [1 2 3;4 5 6;7 8 9]
%String Matrix
y = ['Geeks';'Geeks']

Output:

Example: Knowing the size of the Matrix

 MATLAB

% MATLAB program to know


% the size of a matrix

% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
xSize = size(x)

y=['Geeks';'Geeks'];
ySize = size(y)

Output:

Accessing the elements of a matrix

To reference an element in a matrix, we write matrix(m, n). Here m


and n are row and column indexes.

Example 1:

 MATLAB

% MATLAB program to access


% a particular element

% Creating matrices
x = [1 2 3 4;4 5 6 7;7 8 9 10];
x(3,2)

y=['Geeks';'Geeks'];
y(1,2)

Output:

To access multiple elements in the matrix, we write

matrix(x:y;,xy)

You might also like