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

Matrices Matlab

MATLAB programs readme files

Uploaded by

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

Matrices Matlab

MATLAB programs readme files

Uploaded by

UIG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

a = 1 : 100; % declaring an array 'a'

b = 1 : 100; % declaring an array 'b'


c = sum (a); % sum of the elements of array 'a'
d = sum (b); % sum of the elements of array 'b'
e = a + b; % sum of the arrays 'a' and 'b'
f = a - b; % difference of the arrays 'a' and 'b'
g = prod (a); % product of the elements of array 'a'
h = prod (b); % product of the elements of array 'b'
i = dot (a,b); % product of the arrays 'a' and 'b'
j = rem (a,b); % remainder after division of the elements of arrays 'a' and
'b'
k = mod (a,b); % modulus of remainder after division of the elements of arrays
'a' and 'b'
K = [ 1 2 3; 4 5 6; 7 8 9 ]; % DECLARING A 3*3 SQUARE MATRIX
L = [ 1 2 3; 4 5 6; 7 8 9 ]; % DECLARING A 3*3 SQUARE MATRIX
lambda = eig (K); % determining the eigen values of matrix 'K'
d = det (K); % determining the determinant of matrix 'K'
r = rank (K); % determining the rank of matrix 'K'
M = K + L; % sum of the matrices 'K' and 'L'
N = K - L; % difference of the matrices 'K' and 'L'
O = dot (K,L); % product of the matrices 'K' and 'L'
lambda = eig (K,L); % determining the eigen vectors of matrices 'K' and 'L'
d = 1 : 4; % using the colon with integers
e = 0 : 0.1 : 0.5; % using two colons to create a vector with arbitrary
real increments between the elements
T (:,:,2) = pascal (3); %generates a three-dimensional array
M = mean (L); % mean of the values of the matrix 'L'
N = mean (L,3); % returns the mean values for the elements along the
dimension of 'L' specified by scalar 3

x= linspace(0,10); % generates linearly spaced vectors


y1= sin(x);% generates sine function
y2= cos(2*x);% generates cosine function

figure % creates figure graphics objects


subplot(2,2,1); % sin(x)
plot(x,y1); % plots the graph of functions x and y1
grid on; % enables the grid mode
title('Graph of SINE function; y=sin(x)') % establishes the title of the plot

subplot(2,2,2); % cos(2*x)
plot(x,y2); % plots the graph of functions x and y2
grid on; % enables the grid mode
title('Graph of COSINE function; y=cos(2*x)') % establishes the title of the plot

subplot(2,2,[3,4]); % Subplot of sine, cosine and superimposed one


plot(x,y1,x,y2); % plotting the required function
grid on; % enables the grid mode
title('Superimposed SINUSOIDAL functions') % establishes the title of the plot

x = linspace(-2*pi,2*pi); % generates linearly spaced vectors


y1 = sin(x);% generates sine function
y2 = cos(x);% generates cosine function

figure % creates figure graphics objects


plot(x,y1,x,y2) % plotting the required function

t = linspace(0,2*pi,200); % Generating linearly spaced vectors


a = sqrt(2); % Declaring the variable 'a'
b = sqrt(2/3); % Declaring the variable 'b'
x = a.*cos(t); % Declaring the variable 'x'
y = b.*sin(t); % Declaring the variable 'y'

%plot(((1/2)*(x.^2)), ((3/2).*(y.^2)), '-k', 'LineWidth', 1.5)

plot(x, y, '-k', 'LineWidth', 1.5) % Plotting the graph between variables


grid on % Enabling the grid
axis equal % Setting axes limits and aspect ratios to 'equal'

X = 0:0.5:10; % Declaring an array of values


Y = exp(X); % Declaring the Exponential function
grid on % Enabling the grid
plot(X,Y) % Plotting the desired graph between variables

Matrix1 = [1 : 0.1 : 100]; % Declaring a matrix


logger = log(Matrix1); % Declaring the Logarithmic function
grid on % Enabling the grid
plot(logger) % Plotting the desired graph between variables

x = linspace(-50, 50); % Creates linearly spaced vectors


y = x.^2; % Declaring the Parabolic function
grid on % Enabling the grid
plot(x,y) % Plotting the desired function between variables

f1 = @(x,y) x.^(2)/9 - y.^(2)/4 - 1; % Declaring the Hyperbolic function


fimplicit(f1, 'b'); % Plotting the Implicit function
grid on; % Enabling the grid
title('HYPERBOLIC FUNCTION PLOT') % Assigning the title to the plot
xticklabels({-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}) % To clearly illustrate where the
hyperbola crosses the x-axis.

X = -10:10; % creating an array from -10 to +10


Y = zeros(1,10); % creating an array of 10 zeroes
Z = ones(1,11); % creating an array of 11 ones
K = [zeros(1,10) ones(1,11)]; % Combining both the defined arrays
stem(x, K); % Creating the Step Function
stem(x,x); % Creating the Ramp Function
J = [zeros(1) ones(1) zeros(1,10)]; % Generating y-coordinates for impulse function
T = 1:12; % creating an array from 1 to 12
stem(T,J); % Creating the Impulse Function

A = [5 7 0; 0 2 9; 5 0 0]; % Initialising a matrix A


B = [6 6 0; 1 3 5; -1 0 0]; % Initialising a matrix B
A & B; % Logical AND operation on matrices A and B
A | B; % Logical OR operation on matrices A and B
~A; % Logical NOT operation on matrices A and B
C = xor (A,B); % Logical XOR operation on matrices A and B
A = [0 0 3;0 0 3;0 0 3]; % Initialising a matrix A
B = any(A); % Test each column for nonzero elements.
D = ~C; % Logical XNOR operation on matrices A and B
E = all(A); % Test each column for all nonzero elements.
A = [5 7 0; 0 2 9; 5 0 0]; % Initialising a matrix A
B = [6 6 0; 1 3 5; 1 0 0]; % Initialising a matrix B
K = bitor(A,B); % Bitwise OR operation on matrices A and B
L = bitand(A,B); % Bitwise AND operation on matrices A and B
M = bitxor(A,B); % Bitwise XOR operation on matrices A and B
N = bitcmp(A, 'int8'); % Bitwise Complement operation on matrices A and B
X = 5 * ones(3,3); % Generates a 3x3 square matrix will all elements equal to 5
Y = magic(3); % Generates a random 3x3 matrix
Z = X >= Y; % Implements the "Greater than or equal to" Relational Operator
W = X <= Y; % Implements the "Smaller than or equal to" Relational Operator

Xd = linspace(-1,1); % Generates linearly spaced vectors from -1 to 1


yd = linspace(0,2*pi); % Generates linearly spaced vectors from 0 to 2*pi
[x,y]=meshgrid(xd,yd); % Creates 2D grid coordinates with corresponding vectors
Z = x.*cos(y); % Declaring the surface control function
l = surf(x,y,z); % Plotting the 3D surface plot of the defined function

[X,Y] = meshgrid(-5:.1:5); % Creates 2D grid coordinates with corresponding vectors


Z = Y.*sin(X) - X.*cos(Y); % Declaring the surface trigonometric function
s = surf(X,Y,Z,'FaceAlpha',0.1); % Plotting the 3D surface plot of the defined function
ax2 = nexttile; % Creating axes in tiled chart layout
sphere(ax2,50); % Displaying Sphere with 50 number of faces
axis equal % Sets axes limits and aspect ratios to 'equal'

prompt= 'Enter the number: '; % Displays the statement on screen


N = input(prompt); % Asks the user to enter the number
n = rem(N,2); % Generates the remainder on dividing N by 2
if n==0 % Checks the condition if the remainder is zero
disp('number is even'); % Displays the statement on screen
else % Checks the condition if the remainder is non-zero
disp('number is odd'); % Displays the statement on screen
end % Concludes the if conditional statement

prompt= 'Enter the number: '; % Displays the statement on screen


N = input(prompt); % Asks the user to enter the number
if N>0 % Checks the condition if the number is positive
disp('Number is positive'); % Displays the statement on screen
elseif N<0 % Checks the condition if the number is negative
disp('Number is negative'); % Displays the statement on screen
else % Checks the condition if the number is zero
disp('Number is zero'); % Displays the statement on screen
end % Concludes the if conditional statement

You might also like