MATLAB REPORT
MATLAB REPORT
1
GOVERNMENT ENGINEERING
COLLEGE ,BHOJPUR
2
Signature Signature
3
ACKNOWLEDGEMENT
VANDANA KUMARI
4
STUDENT’S DECLARATION
Vandana kumari
July ,2023
5
TABLE OF CONTENTS
1. Introduction
2. Basic syntax
3. Variables
4. Operation
5. Function
6. Plotting
6
1.INTRODUCTION
MATLAB stands for Matrix Laboratory. MATLAB was written initially to implement a simple approach to
matrix software developed by the LINPACK (Linear system package) and EISPACK (Eigen system
package) projects.
MATLAB is a modern programming language environment, and it has refined data structures, includes
built-in editing and debugging tools, and supports object-oriented programming.
MATLAB is Multi-paradigm. So, it can work with multiple types of programming approaches, such as
Functional, Object-Oriented, and Visual.
MATLAB allows several types of tasks, such as manipulations with matrix, algorithm implementation, data,
and functions plotting, and can interact with programs written in other programming languages.
Most of these functions use state-of-the-art algorithms. These are numerous functions for 2-D and 3-D
graphics, as well as for animations.
7
MATLAB supports an external interface to run those programs from within MATLAB. The user is not
limited to the built-in functions; he can write his functions in the MATLAB language.There are also various
optional "toolboxes" available from the developers of MATLAB. These toolboxes are a collection of
functions written for primary applications such as symbolic computations, image processing, statistics,
control system design, and neural neutral network.
8
The MATLAB desktop environment helps a person to run his commands, manage files and also
helps them to view their desired result. We can change the desktop layout and the various
preferences, such as initial working folder, fonts and keyboard shortcuts by our convenience.
9
2.BASIC SYNTAX
MATLAB environment behaves like a super-complex calculator. You can enter
commands at the >> command prompt.
MATLAB is an interpreted environment. In other words, you give a command
and MATLAB executes it right away.
HANDS ON PRACTICE
Type a valid expression, for example,
5+5
And press ENTER
When you click the Execute button, or type Ctrl+E, MATLAB executes it
immediately and the result returned is −
ans = 10
Let us take up few more examples −
10
warning: division by zero
Another example,
732 * 20.3
When you click the Execute button, or type Ctrl+E, MATLAB executes it
immediately and the result returned is −
ans = 1.4860e+04
MATLAB provides some special expressions for some mathematical symbols,
like pi for π, Inf for ∞, i (and j) for √-1 etc. Nan stands for 'not a number'.
ADDING COMMENTS
The percent symbol (%) is used for indicating a comment line. For example,
x=9 % assign the value 9 to x
You can also write a block of comments using the block comment operators %
{ and % }.
The MATLAB editor includes tools and context menu items to help you add,
remove, or change the format of comments.
3.VARIABLES
11
In MATLAB environment, every variable is an array or matrix.
You can assign variables in a simple way. For example,
12
For example,
sqrt(78)
MATLAB will execute the above statement and return the
following result −
ans = 8.8318
You can use this variable ans −
sqrt(78);
9876/ans
MATLAB will execute the above statement and return the
following result −
ans = 1118.2
Let's look at another example −
x = 7 * 8;
y = x * 7.89
y = 441.84
13
4.OPERATION
MATLAB allows two different types of arithmetic operations −
1
+
Addition or unary plus. A+B adds the values stored in variables A and B. A and
B must have the same size, unless one is a scalar. A scalar can be added to a
matrix of any size.
2
-
Subtraction or unary minus. A-B subtracts the value of B from A. A and B
must have the same size, unless one is a scalar. A scalar can be
subtracted from a matrix of any size.
3
*
Matrix multiplication. C = A*B is the linear algebraic product of the
matrices A and B. More precisely,
4
.*
Array multiplication. A.*B is the element-by-element product of the arrays
A and B. A and B must have the same size, unless one of them is a scalar.
5
/
14
Slash or matrix right division. B/A is roughly the same as B*inv(A). More
precisely, B/A = (A'\B')'.
6
./
Array right division. A./B is the matrix with elements A(i,j)/B(i,j). A and B
must have the same size, unless one of them is a scalar
7
\
Backslash or matrix left division. If A is a square matrix, A\B is roughly
the same as inv(A)*B, except it is computed in a different way. If A is an n-
by-n matrix and B is a column vector with n components, or a matrix with
several such columns, then X = A\B is the solution to the equation AX = B.
A warning message is displayed if A is badly scaled or nearly singular.
8
.\
Array left division. A.\B is the matrix with elements B(i,j)/A(i,j). A and B
must have the same size, unless one of them is a scalar.
9
^
Matrix power. X^p is X to the power p, if p is a scalar. If p is an integer, the
power is computed by repeated squaring. If the integer is negative, X is
inverted first. For other values of p, the calculation involves eigenvalues
and eigenvectors, such that if [V,D] = eig(X), then X^p = V*D.^p/V.
10
.^
Array power. A.^B is the matrix with elements A(i,j) to the B(i,j) power. A
and B must have the same size, unless one of them is a scalar.
11
'
Matrix transpose. A' is the linear algebraic transpose of A. For complex
matrices, this is the complex conjugate transpose.
12
.'
Array transpose. A.' is the array transpose of A. For complex matrices,
this does not involve conjugation.
EXAMPLE
The following examples show the use of arithmetic operators on scalar data. Create a script file with the
following code −
a = 10;
b = 20;
15
c=a+b
d=a-b
e=a*b
f=a/b
g=a\b
x = 7;
y = 3;
z=x^y
When you run the file, it produces the following result −
c = 30
d = -10
e = 200
f = 0.50000
g= 2
z = 343
RELATIONAL OPERATORS
Relational operators can also work on both scalar and non-scalar data. Relational
operators for arrays perform element-by-element comparisons between two arrays and
return a logical array of the same size, with elements set to logical 1 (true) where the
relation is true and elements set to logical 0 (false) where it is not.
The following table shows the relational operators available in MATLAB −
Show Examples
1
<
Less than
2
<=
Less than or equal to
3
>
Greater than
4
>=
5
==
16
Equal to
6
~=
Not equal to
17
5 .FUNCTION
A function is a group of statements that together perform a task. In MATLAB, functions
are defined in separate files. The name of the file and of the function should be the
same.
Functions operate on variables within their own workspace, which is also called
the local workspace, separate from the workspace you access at the MATLAB
command prompt which is called the base workspace.
Functions can accept more than one input arguments and may return more than one
output arguments.
Syntax of a function statement is −
function [out1,out2, ..., outN] = myfun(in1,in2,in3, ..., inN)
EXAMPLE
The following function named mymax should be written in a file named mymax.m. It
takes five numbers as argument and returns the maximum of the numbers.
Create a function file, named mymax.m and type the following code in it −
function max = mymax(n1, n2, n3, n4, n5)
18
help mymax
MATLAB will execute the above statement and return the following result −
This function calculates the maximum of the
five numbers given as input
You can call the function as −
mymax(34, 78, 89, 23, 11)
MATLAB will execute the above statement and return the following result −
ans = 89
ANONYMOUS FUNCTIONS
An anonymous function is like an inline function in traditional programming languages,
defined within a single MATLAB statement. It consists of a single MATLAB expression
and any number of input and output arguments.
You can define an anonymous function right at the MATLAB command line or within a
function or script.
This way you can create simple functions without having to create a file for them.
The syntax for creating an anonymous function from an expression is
f = @(arglist)expression
EXAMPLE
In this example, we will write an anonymous function named power, which will take two
numbers as input and return first number raised to the power of the second number.
Create a script file and type the following code in it –
power = @(x, n) x.^n;
result1 = power(7, 3)
result2 = power(49, 0.5)
result3 = power(10, -10)
result4 = power (4.5, 1.5)
When you run the file, it displays −
result1 = 343
result2 = 7
result3 = 1.0000e-10
result4 = 9.5459
19
6.PLOTTING
To plot the graph of a function, you need to take the following steps −
Define x, by specifying the range of values for the variable x, for which the
function is to be plotted
Define the function, y = f(x)
Call the plot command, as plot(x, y)
Following example would demonstrate the concept. Let us plot the simple function y =
x for the range of values for x from 0 to 100, with an increment of 5.
Create a script file and type the following code −
x = [0:5:100];
y = x;
plot(x, y)
When you run the file, MATLAB displays the following plot
Let us take one more example to plot the function y = x2. In this example, we will draw
two graphs with the same function, but in second time, we will reduce the value of
increment. Please note that as we decrease the increment, the graph becomes
smoother.
Create a script file and type the following code −
x = [1 2 3 4 5 6 7 8 9 10];
x = [-100:20:100];
20
y = x.^2;
plot(x, y)
When you run the file, MATLAB displays the following plot –
EXAMPLE
Create a script file and type the following code −
x = [0:0.01:10];
y = sin(x);
plot(x, y), xlabel('x'), ylabel('Sin(x)'), title('Sin(x) Graph'),
21
grid on, axis equal
22
SETTING COLORS ON GRAPH
MATLAB provides eight basic color options for drawing graphs. The following table
shows the colors and their codes −
Code Color
w White
k Black
b Blue
r Red
c Cyan
g Green
m Magenta
23
y Yellow
EXAMPLE-
Let us draw the graph of two polynomials
f(x) = 3x4 + 2x3+ 7x2 + 2x + 9 and
g(x) = 5x3 + 9x + 2
Create a script file and type the following code −
x = [-10 : 0.01: 10];
y = 3*x.^4 + 2 * x.^3 + 7 * x.^2 + 2 * x + 9;
g = 5 * x.^3 + 9 * x + 2;
plot(x, y, 'r', x, g, 'g')
When you run the file, MATLAB generates the following graph –
24
Create a script file and type the following code −
x = [0 : 0.01: 10];
y = exp(-x).* sin(2*x + 3);
plot(x, y), axis([0 10 -1 1])
When you run the file, MATLAB generates the following graph –
GENERATING SUB-PLOTS
When you create an array of plots in the same figure, each of these plots is called a
subplot. The subplot command is used for creating subplots.
Syntax for the command is −
subplot(m, n, p)
where, m and n are the number of rows and columns of the plot array and p specifies
where to put a particular plot.
Each plot created with the subplot command can have its own characteristics. Following
example demonstrates the concept −
EXAMPLE
Let us generate two plots −
y = e−1.5xsin(10x)
y = e−2xsin(10x)
25
Create a script file and type the following code −
x = [0:0.01:5];
y = exp(-1.5*x).*sin(10*x);
subplot(1,2,1)
plot(x,y), xlabel('x'),ylabel('exp(–1.5x)*sin(10x)'),axis([0 5 -1 1])
y = exp(-2*x).*sin(10*x);
subplot(1,2,2)
plot(x,y),xlabel('x'),ylabel('exp(–2x)*sin(10x)'),axis([0 5 -1 1])
When you run the file, MATLAB generates the following graph –
26