0% found this document useful (0 votes)
12 views21 pages

MATLAB Lab Experiments Guide

Uploaded by

Anjali Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views21 pages

MATLAB Lab Experiments Guide

Uploaded by

Anjali Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MATLAB First Semester Lab Experiments

Experiment 1:- Introduction to the relevant software through matrices

Basics (1) To save files in MATLAB use the [Link](without any space).
(2) Always use clc and clear all before writing any command.
(3) Use the command clear to empty the workspace.

1. (MatLab as a Calculator)

a=15
b=10
c=a+b
Ans 25
d=a-b
Ans 5
e=a*b
Ans 150
F=a/b
Ans 1.5

To Calculate an expression

1+5 3
(a) − ∗7
1+43 8

In MatLab, It becomes
>> (1+5)/(1+4^3 )-((3/8)*7)
Ans -2.5327

𝜋 𝜋
(b) Calculate:- 𝑆𝑖𝑛2 ( 6 ) + 𝐶𝑜𝑠 2 (6 )

In MatLab, It becomes
>> (sin(pi/6))^2+(cos(pi/6))^2
Ans 1
Exercise 1
35
(a)
35 −2
Ans 1.0083
√5−1
(b) 3 [ 2 − 1]
(√5+1)
Ans -2.6456
𝜋
(c) 𝑒 4 −2
Ans 0.2968
𝑖𝜋
(d) 𝑒 +1

Ans 0
Note:-
• For the Exponential function use the command exp and for square root use sqrt.
• In case of complex numbers use
abs(x) Absolute value; x.
angle(x) Angle of a complex number x.
conj(x) Complex conjugate
imag(x) Imaginary part of a complex number x.
real(x) Real part of a complex number x.

2. MATLAB for matrix


To create a matrix in MATLAB that has multiple rows separate the rows with semicolon(;).
For example
A=[1 2 3 ; 4 3 -2 ; -8 4 6]
Outpout will be like

Matrix operations :- Let A and B be two matrix then its Addition, Substraction,
Multiplication, Inverse, Determinant and Transpose is given as
A=[1 -2 3 ; 4 3 -2 ; -8 4 6]
B=[-4 -3 -2 ; 4 5 6 ; 9 -2 0]
Then
• A+B

• A-B

• A*B
• inv(A)

• inv(B)

• inv(A+B)

• det(A)
Ans 162
• det(B)
Ans -104.000
• det(A+B)
Ans 108
• transpose(A)

• transpose(B)

• transpose(A+B)

Magic Matrix:- A square matrix whose elements have the same sum along the row,
column, and diagonal is known as a magic matrix.
For magic matrix we use command magic(m) where m denotes the order of the matrix .
Example:-
>>magic(3)

Pascal Matrix:- P = pascal( n ) returns a Pascal's Matrix of order n. P is a symmetric


positive definite matrix with integer entries taken from Pascal's triangle.
For the magic matrix, we use the command magic(m) where m denotes the order of the
matrix.
Example:-
>>pascal(3)

Other Important Matrices Formation:-


• zeros(m,n) denotes zero matrix of order m * n.
• ones(m,n) denotes a matrix whose all entries are one only.
• eye(m,n) denotes Identity matrix of order m * n.
• rand(m,n) command will create a random matrix of order m * n.
• randn(m,n) command will create a normally distributed random matrix of order m*n.

Task 1. Create A and B two square matrices and find their addition, subtraction,
multiplication, transpose, trace, Inverse, and determinant.

Task 2. For m=n=3, create a zero matrix, identity matrix, and random matrix.
Exercise 2
1. Calculate the following quantities:-
• 𝑒3
Ans 0.0855
• 𝐼𝑛(𝑒 3)

Ans 3
• 𝑙𝑜𝑔10 𝑒 3

Ans 1.3029
• 𝑙𝑜𝑔10 10 3

Ans 3
• 𝑒 𝜋√(163)

Ans 2.6253741e + 17
2. Calculate the value of
𝑦 = cos2 ℎ𝑥 − sin2 ℎ𝑥 ; with x=32π
Ans 0
3. If 𝑧 = 4.5, Calculate the value of
• 0.6𝑧 4 + 2.8𝑧 3 − 16𝑒 2𝑧 − 90
Ans -1.2924e+05
𝑧 4 −2𝑒 𝑧
• 3
√𝑧 3 ∗2∗𝑧∗1
Ans 24.5747
4. Calculate the following expressions:-
1+3𝑖
• 1−3𝑖
𝑖𝜋
• 𝑒4
𝑖𝜋
• 𝑒 ( 2 )𝑖
𝜋
• 𝑒 2𝑖

3. Use of MATLAB to solve the equations:-


MATLAB is a strong software tool for numerical computations and mathematical equation
solving. It offers an easy framework for solving difficult equations, simulating data, analyzing
data, and visualizing results. Depending on the type of equation, you can use several
approaches to solve it in MATLAB.

Solution of Algebraic equation in MATLAB:- To solve the algebraic equation in


MATLAB, we have different method to solve the equation.

Method 1. Here, using command roots, we can solve the given algebraic equation.
Example:- Suppose we have given an equation
5𝑥 − 10 = 0
And to solve this equation in MATLAB, use the command
INPUT
>> f=[5 -10]
>> roots(f)
OUTPUT 2

Method 2. In this method first define the variable and then use the command solve to solve
the given algebraic equation.
Example:- Suppose given equation is
𝑥 2 + 2𝑥 − 15 = 0
And to solve this equation in MATLAB use the command
INPUT
>> syms x
>> solve(x^2+2*x-15= =0)
OUTPUT -5
3
Note. To get the numerical solution use the command “double(ans)”.

Exercise 3
Solve the following equations using command roots and solve.
1. 2x − 8 = 0
2. 𝑥 2 − 5𝑥 + 6 = 0
3. 𝑥 3 − 4𝑥 2 + 𝑥 + 6 = 0
4. 𝑥 4 − 5𝑥 3 + 9𝑥 2 − 6𝑥 + 1 = 0
5. 𝑥 5 − 6𝑥 4 + 11𝑥 3 − 6𝑥 2 + 𝑥 = 0
6. 𝑥 2 − 8𝑥 + 16 = 0
7. 𝑥 3 − 6𝑥 2 + 11𝑥 − 6 = 0
8. 𝑥 4 − 4𝑥 3 + 6𝑥 2 − 4𝑥 + 1 = 0
9. 𝑥 5 − 5𝑥 4 + 10𝑥 3 − 10𝑥 2 + 5𝑥 − 1 = 0
10. 𝑥 5 − 10𝑥 4 + 35𝑥 3 − 50𝑥 2 + 29𝑥 − 6 = 0

Symbolic Equations:- If you have symbolic equations with unknown variables, you can
use the Symbolic Math Toolbox in MATLAB. You can use this toolbox to define symbolic
variables, write symbolic expressions, and solve equations symbolically. You can define
symbolic variables using the “syms” command and create symbolic equations using these
variables.

Example:- A system of equation is given as


x + y - z = 5;
2*x - y + z = 3;
x - 2*y - z = -1;
and to solve this equation in MATLAB use the command as :-
>> syms x y z [ To define the variables, use the command syms ]
>> eq1 = x + y - z = = 5;
>> eq2 = 2*x - y + z = = 3;
>> eq3 = x - 2*y - z = = -1;
>> sol = solve([eq1, eq2, eq3], [x, y, z])
And you will get output as

Exercise 4
Solve the following system of equations:-
1. 2𝑥 + 3𝑦 = 10
4𝑥 − 5𝑦 = −2
Ans 2,2

2. 3𝑥 − 2𝑦 = 7
5𝑥 + 4𝑦 = 18
Ans 2,1
3. 𝑥 + 𝑦 = 5
2𝑥 − 𝑦 = 3
Ans 2,3
4. 2𝑥 + 3𝑦 = 7
3𝑥 − 2𝑦 = 8
Ans 2,1
5. 𝑥 + 2𝑦 − 𝑧 = 3
2𝑥 − 𝑦 + 3𝑧 = 7
3𝑥 + 𝑦 + 2𝑧 = 9

Ans 1,2,1
Use of MATLAB to find the Limit, Continuity, and Differentiability
1. Limit :- The limit of a function in mathematics is a fundamental idea that is used to
describe how a function behaves as its input approaches a particular value or as it tends
toward infinity or negative infinity. The limit reveals details about the function's behavior
close to a specific point.
Formally, let's consider a function f(x). The limit of f(x) as x approaches a particular value,
denoted by " lim 𝑓(𝑥)" or simply "lim f(x)", is defined as follows:
𝑥→𝑎

lim 𝑓(𝑥) = L
𝑥→𝑎
This means that as x gets arbitrarily close to the value a (but not equal to a), the
corresponding values of f(x) get arbitrarily close to the value L.
There are numerous varieties of limits, such as one-sided limits, infinite limits, and limits at
infinity. Each type of limit describes a unique element of a function's behavior.
Syntax for limit:-
• >>syms variable
>>limit(function, variable, limit point)
It means first define the variables that you are using in your function then write the limit and
inside it write your function, then variables and at last mention the limit point (at which point
you want to find the limit of the given function).
And if you want to calculate the left hand limit and right hand limit then after limit point
using comma just mention “left” or “right”.
• >>syms variable
>>limit(function, variable, limit point, “left”)

• >>syms variable
>>limit(function, variable, limit point, “right”)

Exercise 5
1. Find the limit for the following functions
• 𝑓(𝑥) = 𝑥 3 + 4𝑥 2 − 3 𝑎𝑡 𝑥 = 𝑐

4
• 𝑓(𝑦) = (5 − 𝑦)3 𝑎𝑡 𝑦 = −3

• 𝑓(𝑥) = √4𝑥 2 − 3 𝑎𝑡 𝑥 = −2

𝑥 4 +𝑥 2 −1
• 𝑓(𝑥) = 𝑎𝑡 𝑥 = −2
𝑥 2 +5

𝑥 2 +𝑥−2
• 𝑓(𝑥) = 𝑎𝑡 𝑥 = 1
𝑥 2 −𝑥

√𝑥 2 +100−10
• 𝑓(𝑥) = 𝑎𝑡 𝑥 = 0
𝑥2

√5ℎ+4−2
• 𝑓(ℎ) = 𝑎𝑡 ℎ = 0

3
• 𝑓(ℎ) = √3ℎ2 𝑎𝑡 ℎ = 0
+1+𝟏
2. Find the limit for the following Trigonometric functions
• 𝑓(𝑥) = sin (𝑥) 𝑎𝑡 𝑥 = 0

sin(𝑥)
• 𝑓(𝑥) = 𝑎𝑡 𝑥 = 0
𝑥

tan(𝑥)
• 𝑓(𝑥) = 𝑎𝑡 𝑥 = 0
𝑥

sin−1 𝑥
• 𝑓(𝑥) = 𝑎𝑡 𝑥 = 0
𝑥

tan−1 𝑥
• 𝑓(𝑥) = 𝑎𝑡 𝑥 = 0
𝑥

tan(𝑥)−sin (𝑥)
• 𝑓(𝑥) = 𝑎𝑡 𝑥 = 0
𝑥3

3. Find the limit of the following functions


1
• 𝑓(𝑥) = (1 + 𝑥)𝑥 𝑎𝑡 𝑥 = 0

1 𝑥
• 𝑓(𝑥) = (1 + 𝑥) 𝑎𝑡 𝑥 = ∞

𝑎 𝑥
• 𝑓(𝑥) = (1 + ) 𝑎𝑡 𝑥 = ∞
𝑥

𝑙𝑛(1+𝑥)
• 𝑓(𝑥) = 𝑎𝑡 𝑥 = 0
𝑥

𝑎𝑥 −1
• 𝑓(𝑥) = 𝑎𝑡 𝑥 = 0
𝑥

√1−cos (2x)
• 𝑓(𝑥) = 𝑎𝑡 𝑥 = 0
√2x

4. Find the left hand limit, right hand limit and limit at the given point for the following
functions
1
• 𝑓(𝑥) = 𝑥 sin (𝑥) 𝑎𝑡 𝑥 = 0

1
• 𝑓(𝑥) = 2𝑥−1 𝑎𝑡 𝑥 = 1

1
𝑒𝑥
• 𝑓(𝑥) = 1 𝑎𝑡 𝑥 = 0
𝑒 𝑥 +1

1
• 𝑓(𝑥) = 1 𝑎𝑡 𝑥 = 0
1+𝑒 𝑥
1
• 𝑓(𝑥) = 𝑥 2 sin (𝑥) 𝑎𝑡 𝑥 = 0

2. Continuity of a function :- Continuity is a fundamental concept in calculus and real


analysis that describes the behavior of a function on its domain. A function is said to be
continuous if it maintains a smooth and unbroken connection between its points. In other
words, there are no abrupt jumps, holes, or infinite oscillations in the graph of a continuous
function.
Formally, a function f(x) is continuous at a point c if the following three conditions are
satisfied :-
• The function is defined at c: f(c) is defined.
• The limit of the function as x approaches c exists: lim(x→c) f(x) exists.
• The limit and the value of the function are equal: lim(x→c) f(x) = f(c).

Experiment 2. Plotting and visualizing of functions in MATLAB

There are certain steps that you need to follow for Matlab function plot, and these are:

• Define the variable x, by highlighting the range of the values for x variable, for which
the functions can be plotted.
• Define the function, y = f(x).
• Call the command for function plot with the file name plot(x,y).

There is an example that can be used to demonstrate the basic concept of these functions. Let’s
plot the graph function of the function y = x that has the range of values from 0 to 100 for
variable x that is incremented with 5.

Generate a script file and write the following programming:-

>> x = [0:5:100]; [This expression shows that the range of x starts from 0 and takes
increment of 5 and stops when 100 reached.]

>> y = x;

>> plot(x, y)

and output will be like


There are many types of MATLAB plotting but we will discuss here few of them.
Type 1. Simple plot:- As we discussed above, Here, just follow these stps -
• Define the variable x, by highlighting the range of the values for x variable, for which
the functions can be plotted.
• Define the function, y = f(x).
• Call the command for function plot with the file name plot(x,y).

Example :-

>> x = [-100:20:100];

>> y = x.^2;
>> plot(x, y)

Important points for plot style in MATLAB


Type 2. Function plot:- The function fplot in MATLAB is used to generate a 2D plot of a
mathematical function. It enables you to see how a function behaves over a particular range
of values.
Example :-

Type 3. Overlay plot :- Overlay plotting in MATLAB means putting more than one set of
data on the same graph. This is often done to compare different sets of data or to see how
different factors are linked. There are several ways to use MATLAB to put data on top of
each other. Here are some popular ways to do it:-

Method 1:-
Remark 1. In every plot you can add title using the command ‘title(‘title of the graph’)’
and axislabel also you can add using the command xlabel(‘x-axis’) and ylabel(‘y-axis’).
Also inside the plot command after x and y you can add plotstyle using the command
‘plotstyle’.
Remark 2. When no option is specified, MATLAB uses a blue solid line by default.

Method 2:- In this method use the command hold on and hold off.
Type 4. Subplot :- With the subplot tool in MATLAB, you can make a grid of smaller axes
inside of a single figure. Each smaller axis can have its own plot or graphic material, which is
called a subplot. This lets you show more than one plot or representation in one figure.

Example :-
Type 5. 3D plot :-
1. Curves in three-dimensional space:- To plot curves in three-dimensional space using
MATLAB, you can utilize the plot3 function. Its usage is similar to the regular plot function,
but with an additional vector representing the z-coordinates. Therefore, instead of using two
vectors (x and y) as input for plot, plot3 takes three vectors: one for x-coordinates, one for y-
coordinates, and one for z-coordinates. This allows you to create 3D visualizations of curves
and explore data in three-dimensional space.
Example:- We can plot a helix with

Again, if you have the Symbolic Math Toolbox, there is a shortcut using “ezplot3”; you can
instead plot the helix with
2. Surfaces in Three-Dimensional Space:- There are two basic commands for plotting
surfaces in 3-space: mesh and surf. The former produces a transparent “mesh” surface; the
latter produces an opaque shaded one. There are two different ways of using each command,
one for plotting surfaces in which the z coordinate is given as a function of x and y, and one for
parametric surfaces in which x, y, and z are all given as functions of two other parameters. Let
us illustrate the former with mesh and the latter with surf.
To plot z = f (x, y), one begins with a meshgrid command as in the case of contour. For
example, the “saddle surface” 𝑧 = 𝑥 2 − 𝑦 2 can be plotted with

>>[X,Y] = meshgrid(-3 : 0.1 : 3, -3 : 0.1 : 2);


>> Z = X.^2 - Y.^2;
>> mesh(X, Y, Z)

The result is shown in following Figure, although it looks much better on the screen since
MATLAB shades the surface with a color scheme depending on the z [Link] could
have gotten an opaque surface instead by replacing mesh with surf.

With the Symbolic Math Toolbox, there is a shortcut command ezmesh, and you can obtain a
result very similar to above Figure with
Exercise 5.

1. Plot the following function using MATLAB.

• 𝒙 = 𝑒 −𝑡 𝑐𝑜𝑠(𝑡) 𝑡 ∈ [0,2𝜋], 𝑛 = 50
1−𝑡
• 𝒙= 𝑡 ∈ [0,5𝜋], 𝑛 = 200
𝑒𝑡

• 𝒙 = 1 + 𝑒 −𝑡 𝑡 ∈ [0,2𝜋], 𝑛 = 150

• 𝒚 = 𝑒 𝑥 𝑡𝑎𝑛 (𝑥) 𝑥 ∈ [0,3𝜋], 𝑛 = 100

• 𝒚 = −𝑥 3 𝑥 ∈ [0,27], 𝑛 = 100

1
• 𝒚 = − (𝑥 ) 𝑥 ∈ [0, 20], 𝑛 = 100
2. Plot the following function in MATLAB using ‘hold on’ and ‘hold off’.
• 𝑦 = 𝑒𝑡 𝑎𝑛𝑑 𝑧 = 𝑒 𝑡 sin(𝑡) 𝑡 ∈ [0,2𝜋] 𝑎𝑛𝑑 𝑛 = 50,100,200

• 𝑟 2 = 2 sin(5𝑡) 𝑎𝑛𝑑 𝑦 = 𝑟 sin(𝑡) 𝑡 ∈ [0,2𝜋] 𝑎𝑛𝑑 𝑛 = 50,100,150

𝑥
sin(𝑥)
• 𝑦= 𝑎𝑛𝑑 𝑧 = 𝑒 −(5) sin(𝑡) 𝑡 ∈ [0,2𝜋] 𝑎𝑛𝑑 𝑛 = 50,100
𝑥

𝑥3 𝑥5
• 𝑓=𝑥− + 120 , 𝑔 = 100 + 𝑒 𝑥 𝑎𝑛𝑑 ℎ = 𝑒 𝑥 𝑡 ∈ [0,2]
6

3. Plot the following graph using ‘subplot’ command in MATLAB.

• 𝑓1 (𝑥) = sin (𝑥), 𝑓2 (𝑥) = cos (𝑥), 𝑓3 (𝑥) = tan (𝑥) 𝑎𝑛𝑑 𝑓4 (𝑥) = 𝑒 𝑥

• 𝑔1 = 𝑠𝑖𝑛(𝑥) , 𝑔2 = 𝑠𝑖𝑛(𝑥)𝑐𝑜𝑠(𝑥) , 𝑔3 = 𝑥 𝑡𝑎𝑛(𝑥) , 𝑔4 = 𝑒 𝑥 𝑠𝑖𝑛(𝑥) 𝑥 ∈ [0,2𝜋]

Common questions

Powered by AI

In MATLAB, complex expressions involving trigonometric and exponential functions are computed using specific commands. For trigonometric functions, commands like sin, cos, and tan are used in conjunction with pi for angles in radians, e.g., (sin(pi/6))^2+(cos(pi/6))^2 equates to 1 . Exponential expressions use commands like exp for computing e raised to any power, e.g., e^(pi*sqrt(163)), resulting in approximately 2.6253741e + 17 .

MATLAB solves polynomial equations using the 'roots' or 'solve' functions. The 'roots' function requires the polynomial coefficients, e.g., solving 5x-10=0 uses f=[5 -10], returning the roots as [2]. Alternatively, 'solve' is used after defining variables, e.g., solving x^2+2x-15=0 via syms x and solve(x^2+2*x-15==0) returns the roots [-5, 3].

MATLAB provides a set of commands for creating and manipulating matrices. To create a matrix, you separate rows using semicolons, e.g., A=[1 2 3 ; 4 3 -2 ; -8 4 6]. Key operations include addition (A+B), subtraction (A-B), multiplication (A*B), finding the inverse (inv(A)), and calculating the determinant (det(A) resulting in 162). Special types of matrices, like magic and Pascal matrices, can be generated with commands such as magic(m) and pascal(n).

In MATLAB, the 'mesh' command plots transparent surfaces, representing 3D data with a wireframe model, while 'surf' plots opaque surfaces with shading. Both can plot z=f(x, y) surfaces but differ in visual representation, with 'surf' offering more solid visuals. The meshgrid command typically precedes these for grid creation .

In MATLAB, to solve a system of linear equations symbolically, you first define the variables using the 'syms' command. Then, you create equations with these variables, e.g., eq1 = x + y - z == 5. Lastly, you solve the system using the 'solve' command, which returns the solutions for the variables involved .

In MATLAB, overlay plotting is achieved using the 'hold on' and 'hold off' commands, allowing multiple datasets to be plotted on the same graph for comparison. It's crucial to use these commands correctly to maintain plot clarity and ensure separate datasets are correctly visualized without affecting each other .

Continuity of a function implies a smooth connection without jumps or breaks. A function f(x) is continuous at a point c if it's defined at c, the limit as x approaches c exists, and the limit equals f(c). Using MATLAB, one can check these criteria by evaluating limits around the point in question. Although MATLAB doesn't directly evaluate continuity, it facilitates the computation of these components and allows plotting for visual verification .

To plot a 3D helix in MATLAB, use the plot3 function, which requires three vectors for x, y, and z coordinates. You define the range and increment for each coordinate. Optionally, the ezplot3 command from the Symbolic Math Toolbox can be used for simplicity, which automatically handles the plotting of parametric curves like a helix .

The Symbolic Math Toolbox in MATLAB enables solving systems of equations symbolically by defining variables ('syms') and equations, followed by the 'solve' command for solutions. This toolbox facilitates exploring solutions in a symbolic format, useful for precision and algebraic manipulation, enhancing analytical capabilities beyond numerical solvers .

Calculating the limit in MATLAB aids in understanding a function's behavior near specific points, crucial for continuity and differentiation analysis. The process involves using the 'limit' command after defining symbolic variables with 'syms'. Syntax includes limit(f(x), x, a), where 'a' is the point of interest. Options for left/right-hand limits refine the analysis .

You might also like