Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views5 pages

Matlab Assignment 2

Uploaded by

swanandk878
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)
5 views5 pages

Matlab Assignment 2

Uploaded by

swanandk878
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
You are on page 1/ 5

Amrita School of Engineering, Bengluru-35

Department of Mechanical Engineering


23MAT126 – Mathematics for Intelligent Systems - 1

Plotting

• x=-10:0.01:10; y=x.^2; plot(x,y) Plots the function f(x)=x2 in [-10,10] by creating vectors x and y
linspace(a,b,c) creates a vector with ‘ c’ equally distributed points
in (a,b).
• x=linspace(-10,10,2000); y=x.^2; plot(x,y)

➢ t=0:0.0001:2*pi, plot(cos(t),sin(t)) Plots the unit circle with centre at origin using parametric form

➢ x=-10:1:10; y=x.^2; stem(x,y) Gives the stem plot of the function f(x)=x2 in [-10,10]

To Plot a line segment from a point A(a1,a2) to a point B(b1,b2)

➢ plot([a1,b1],[a2,b2])
➢ plot([1,3],[2,4]) will plot a line from (1,2) to (3,4)
➢ plot([0,10],[0,0]) will plot X axis from (0,0) to (10,0)
➢ plot([0,0],[-5,5]) will plot Y axis from (0,-5) to (0,5)

Creation of a script file(M-file) for plotting

➢ Click on on top left of the MATLAB window, to create a new script file(m-file)
and type the following in it. After typing save the file as circle and run it. Look out for
the figure which appears in the figure window.
plots a circle with unit radius
theta = linspace(0,2*pi,100);
x=cos(theta); centred at the origin with
y=sin(theta); title and labels for axes.
plot(x,y)
axis('equal')
xlabel('x')
ylabel('y')
title('Circle of unit radius centred at the origin')

➢ Create a script file(M-file), type the following in it, save the file as multipleplot and
execute it. Look out for the figure which appears in the figure window
x=-4:0.001:4;
y=x;
plot(x,y,'Color','red') plots multiple curves in the same figure with different
hold on colours and linestyles also giving the length of the
y1=x.^2;plot(x,y1,'linestyle','--') axes.
hold on Command ‘hold on’ is used when multiple curves
plot(x,sin(x),'Color','green','linestyle','-.') need to be plotted in the same figure
axis([-4 4 -4 4])

Linear Algebra
Art of Creating Matrices with Given Properties
Commands to generate random numbers, random integers and matrices with random numbers,
random integers

➢ rand(1) Generation of a random number between 0 and 1

➢ rand(2)
➢ rand(2,3) Generation of a random matrix(square/rectangular) with elements between 0 and 1

➢ randi(5) Generates an integer random number between 0 and 5

➢ randi([3,15]) Generates an integer random number between 3 and 15

➢ randi([0,5],2) Generates random square matrix of order 2, with elements

between 0 and 5
➢ randi([2,6],3,4)
Generates random 3×4 matrix with elements between 2 and 6

➢ magic(5) Generates a magic matrix that has row sum, column sum to be same

1. Generation of an integer valued symmetric matrix


>> A=randi([1 9],3,3)

>> S=A+A’

2. Generation of an integer valued skew-symmetric matrix


>> A=randi([-9 9],5,5)
>> S=A-A’

3. Generation of random integer matrices with given rank


❖ Results used –
1. Maximum Rank of an m×n matrix is min(m,n)
2. Rank(AB)≤min(Rank(A),Rank(B))

>> A=randi([0 9],3,1)*randi([0 9],1,3)


% Generates a random 3×3 matrix of rank 1.
>>rank(A)
>>B=randi([0 9],5,2)*randi([0 9],2,7)
% Generates a random 5×7 matrix of rank 2.
>>rank(B)
>>C=randi([0 9],6,3)*randi([0 9],3,4)
% Generates a random 6×4 matrix of rank 3.
>>rank(C)
4. Generating an upper triangular matrix (extracting the upper triangular elements of a given
matrix)
>> A=randi([0,9],4,4)

>> L=triu(A)

5. Generating a lower triangular matrix (extracting the lower triangular elements of a given
matrix)
>> A=randi([0,9],4,4)

>> L=tril(A)

6. Generating an integer valued square matrix with integer valued inverse


A=randi([0,9],3,3);
B= A-diag(diag(A)); % make diagonal elements as zero
B=B+eye(3) ; % Make a matrix with all diagonal elements as 1
A1=triu(B); % det(A1)=1
A2= tril(B); % det(A2)=1
AA=A1*A2 ; % det(A1*A2)= det(A1)*det(A2)=1
% Therefore its inverse will also be integer matrix
% det(AA)
detAA=det(AA)
inverseAA=inv(AA)
%AA is the integer mtrix which has inverse also as an integer matrix
Exercise Problems
1. Plot y=cosx , x= -2𝜋 to 2π.
( x + 5)2
2. Plot the function f (t ) = −3  x  5
4 + 3x 2
5sin x 3x
3. Plot the function f ( x) = −0.75 x

x+e 5
4. Create an integer valued 5x5 matrix of rank 4.
5. Create an integer valued 8x4 matrix of rank 3.
6. Create an integer valued 9x12 matrix of rank 5.
7. Create a magic matrix of order 6 and find its determinant and rank.
8. Create an integer valued 10x10 symmetric matrix.
9. Create an integer valued 8x8 skew-symmetric matrix.
10. Create an integer valued 7x7 matrix which has an integer valued inverse.
11. Obtain a random integer square matrix of order 20 and find the rank of it.
12. Obtain 2 random integer square matrices A and B of order 5.
(a) Find the rank of A
(b) Find the rank of B
(c) Find the rank of A+B
(d) Find the rank of A-B
(e) Find the rank of A*B
(f) Find the rank of kB, by choosing k as any real number

13. Using MATLAB, generate a 5x5 matrix A of rank 4.


a. Retrieve an element with row index 3, and column index 5.
Ans: a=A(3,5)
b. Retrieve first row from A and store in b
Ans: b=A(1, :)
c. Retrieve first and third row from A and store in C.
Ans: C=A([1, 3], :)
d. Retrieve second column from A and store in d
Ans: d=A( :, 2)
e. Retrieve second and fourth column from A and store in E.
Ans: E=A(:, [ 2, 4])

14. Using MATLAB generate a 9× 9 matrix A of rank 2. Obtain a symmetric matrix


B=A+A’ and find rank of B.

15. Using MATLAB generate a 10 × 5 matrix A of rank 3.


(a) Obtain a symmetric matrix S1=A*AT and S2=AT*A
(b) Find the rank of S1 and S2.

You might also like