0% found this document useful (0 votes)
14 views7 pages

Matlab Fundamentals

Uploaded by

khandagaleom738
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)
14 views7 pages

Matlab Fundamentals

Uploaded by

khandagaleom738
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/ 7

MF1

% Assignment of values
a = 4,A = 6;x = 1;

% complex numbers
x = 2+i*4
% predefined variables
pi
% additional precision
format long
% Now when pi is entered the result is
displayed to 15 significant figures:
pi
% four decimal version
format short

MF2

% One dimensional arrays are


called vectors and two-dimensional
arrays are called matrices
% row vector
a = [1 2 3 4 5]
% column vector
b = [2;4;6;8;10]
% Transpose
b = [2 4 6 8 10]'
% Matrix
A = [1 2 3; 4 5 6; 7 8 9]
% a list of all current variables
can be obtained by entering the
who command
who
% Access an individual element of
an array
b(4)
A(2,3)
% matrix of zeros
E = zeros(2,3)
% matrix of ones
u = ones(1,3)

MF3
% colon operator
format short
t = 1:5
t = 1:0.5:3
t = 10:-1:5
A = [1 2 3; 4 5 6; 7 8 9];
% the second row of the matrix A can be
selected as in
A(2,:)
% linspace which generates n points
between x1 and x2.
linspace(0,1,6)
% The logspace function generates a row
vector that is logarithmically equally spaced
% between decades 10^x1 and 10^x2.
logspace(-1,2,4)
% character strings. Aside from numbers,
alphanumeric information or character strings
can be represented by
% enclosing the strings within single
quotation marks.
f = 'Miles ';
s = 'Davis';
x = [f s]
% that very long lines can be continued by
placing an ellipsis (three consecutive
% periods) at the end of the line to be
continued.
a = [1 2 3 4 5 ...
6 7 8]
quote = ['Any fool can make a rule,' ...
' and any fool will mind it']

MF4
% mathematical operations
2*pi
% For example, because exponentiation has
higher priority then negation,
% the following result would be obtained.
y = -4 ^ 2
% Parentheses can be used to override the
priorities as in
y = (-4) ^ 2
x=2+4*i; y= 16;
3*x
1/x
x^2
x+y
a = [1 2 3 4 5]
% column vector
b = [2;4;6;8;10]
% The inner product of two vectors (dot
product) can be calculated using the *
operator,
a*b
% and likewise, the outer product
b*a
a = [1 2 3]; b = [4 5 6]';
A = [1 2 3; 4 5 6; 7 8 9]
a*A
A*b
A*A
A^2
% square each element of A
A.^2

% USE OF BUILT-IN FUNCTIONS


help log
log(A)
% rounding
E = [-1.6 -1.5 -1.4 1.4 1.5 1.6];
round(E)
% sum function
F = [3 5 4 6 1];
sum(F)
min(F),max(F),mean(F),prod(F),sort(F)
length(F)
MF5
% Graphics
t = (0:2:20);
g = 9.81; m = 68.1; cd = 0.25;
v = sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t);
plot(t, v)
hold on
plot(t, v, 'o')
% plot(t, v, t, v, 'o')
title('Plot of v versus t')
xlabel('Values of t')
ylabel('Values of v')
grid

You might also like