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

MATLAB Basics: Workspace & Functions

This document provides an overview of key MATLAB concepts and commands organized into the following sections: 1. Work space, command window, and command history management. 2. Defining variables of different types (e.g. integers, vectors, matrices). 3. Mathematical functions including complex numbers, trigonometric, exponential, and vectors/arrays. 4. Details on declaring and performing operations on vectors and matrices, as well as plotting relationships between vectors. 5. Brief mentions of creating M-files, polynomials, and MATLAB help.

Uploaded by

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

MATLAB Basics: Workspace & Functions

This document provides an overview of key MATLAB concepts and commands organized into the following sections: 1. Work space, command window, and command history management. 2. Defining variables of different types (e.g. integers, vectors, matrices). 3. Mathematical functions including complex numbers, trigonometric, exponential, and vectors/arrays. 4. Details on declaring and performing operations on vectors and matrices, as well as plotting relationships between vectors. 5. Brief mentions of creating M-files, polynomials, and MATLAB help.

Uploaded by

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

LAB 1: Intro to Matlab

1.1. Work space Management


Instruction Definition

who (Displays variables in the workspace)


whos (Displays variables in the workspace with more details)
clear (Deletes MATLAB workspace (all variables))
clear(y) (Deletes the variable named y)
clear y (Deletes the variable named y (another way))
save file_name (Saves workspace variables to a storage device )
load file_name (Loads workspace variables from a storage device )

1.2. Command Window Management

clc (Clears command window)


x=1; ; (suppresses echo)
UP & DOWN (arrows Recall previously executed commands)

1.3. Command History Management


Double clicking any command in the command history will execute it again.

2. Variables in MATLAB

Identification Variable type

A=34(Integer)
X=[1 2 3 4] (Vector)
Y=[1 2 3; 4 5 6; 7 8 9];(Matrix)
S=hello(String)
R=3+2i or R=3+2j (Complex number (both declarations are equivalent))
5*3/9+12 (The result will be stored in a variable called ans)
inf (1/0) (Infinity)
pi (3.14 or )
eps (Very small number; tends to zero)
Note: MATLAB is case sensitive. So, x=3 doesnt mean that X=3 as they arent
equivalent.
3. Mathematical Functions

3.1. Complex numbers


Function Output

s=3+4j or s=3+4i (Declaring a complex number s)


real(s) [3] real part of s
imag(s) [4] imaginary part of s
abs(s) [5] absolute value of s

angle(s) ([53.1301] radian angle of s : tan 1 (4 / 3))


complex(a,b) ([a+jb] results in a complex number of a, b)
conj(s) ([3-4j] conjugate of s (real-j*imag.))

3.2. Trigonometric Functions

Y=sin(x) Y=asin(x) Y=sinh(x) Y=asinh(x)


Y=cos(x) Y=acos(x) Y=cosh(x) Y=acosh(x)
Y=tan(x) Y=atan(x) Y=tanh(x) Y=atanh(x)
Y=sec(x) Y=asec(x) Y=sech(x) Y=asech(x)
Y=csc(x) Y=acsc(x) Y=csch(x) Y=acsch(x)
Y=cot(x) Y=acot(x) Y=coth(x) Y=acoth(x)
Adding a before function name gives the inverse function of the original one.
Adding h after function name gives the hyperbolic function of the original one.
For inverse hyperbolic add both a at the front and h at the end of the function name.

3.3. Exponential Functions


Function Example

x^n xn : 3^4=81
exp(x) Exponential function: exp(5)=148.41
log10(x) Log(x)/log(10) [base 10 logarithm]: log10(100)=2
log2(x) Log(x)/log(2) [base 2 logarithm]: log2(64)=6
log(x) Ln (natural logarithm): log(4)=ln(4)=1.39
pow2(x) 2^x: pow2(3)=2^3=8
Nextpow2(x) Produces n where 2^nx: nextpow2(33)=6
sqrt(x) x : sqrt(25)=5
factorial(x) x! the factorial of x such that x0 and integer: factorial(3)=6

4. Vectors or Arrays

4.1. Declaring vectors


X=[1 5 9 -3 5] X=[element1 element2 element3 . ]
Y=0:0.1:5 (start:step:end)
Z=linspace(0,10,11) (start, end, no. of points) linear space

4.2. Operations on vectors


x(7) Displays 7th element of the vector named x
x(5:9) Displays 5th to 9th elements of x
x(6:end) x(6:length(x)) Displays elements from the 6th to the last one of x
x(1:1:6) Displays elements 1,2,3,4,5, and 6 (step one)
x(1:2:7) Displays elements 1,3,5, and 7 (step two)
x(8:-3:1) Display elements 8,5, and 2 (step down by three)
x([1 2 4 9]) Display elements 1st,2nd,4th, and 9th elements
x The transpose of x, with conjugate if x is complex
x. The transpose of x without conjugation at all
[r,c]=size(x) Returns: r = number of rows of the vector x
c = number of columns of the vector x
sum(x) Adds all elements of the vector x
mean(x) The mean value of the vector x sum(x)/length(x)
length(x) Number of elements in the vector x
find(x==2) Returns the locations of the elements in x which are equal
to 2
P=find(x>4) Returns the locations of the elements in x which are greater
than 4 in a vector P of zeros and ones (T/F)
G=x(P) G is formed of the elements in x that are greater than 4
P=find(x>4 & x<10) Returns the locations of the elements in x which are greater
than 4 and in the same time are less than 10 in a vector P
P=find(x==5 | x>=8) Returns the locations of the elements in x which are equal
to 5 or are greater than or equal to 8 in a vector P
x==5 Asks MATLAB if x equals 5 or not. Answer is 1 if true
and 0 if false.
y=x^2 Squaring the vector x; will lead to error due to dimensions
y=x.^2 Squaring the elements of the vector x; no care for
dimensions.

4.3. Plotting two vectors


To plot any two vectors, there are two techniques:
First technique:
You should be aware of the contents of one of the two vectors at least and the
relation of the other one to the known vector. An example of that is: plotting the
sinusoidal function; you may want to plot Y=sin(X), so you should be aware by either X
or Y and then use the relation between them (sin or asin according to the known vector)
to plot the relationship between them.
If X is known: Define X by any method of defining vectors, indicate the forward
relation of Y to X , which is the sin function, and then ask MATLAB to plot the two
vectors.
Note that the computing is discrete, so you cant say that X is [-2 to 2] but you should
indicate the step of calculation. Also, note that minimizing the step size will lead to very
smooth curve for the relationship and vice versa.
On the MATLAB command widow, write the following instructions:
>> X=[-2*pi:0.1:2*pi];
>> Y=sin(X);
>> plot(X,Y)
Here, we indicated that the step size is 0.1, but you may change it once to a greater value
and another to a smaller value and note the difference in the curve of the relationship
between X and Y in each case.
If Y is known: Define Y by any method of defining vectors, indicate the backward
relation of X to Y , which is the arcsin function, and then ask MATLAB to plot the two
vectors.
On the MATLAB command widow, write the following instructions:
>> Y=[-1:0.01:1];
>> X=asin(Y);
>> plot(X,Y)
Also, change the step size (0.01) once to a greater value and another to a smaller value
and note the difference in the curve of the relationship between X and Y in each case.

Second technique:
In this one, you dont know a specific relationship between the vectors.
For example, if you would like to plot the average temperature of the months of the year
to their order according to the following table:
MONTH 1 2 3 4 5 6 7 8 9 10 11 12
Temperature 17 19 22 24 27 30 34 35 31 26 23 20
Of course, you dont have a specific relation between the month order and the average
temperature. So, you should use this way for plotting the two vectors.
On the MATLAB command widow, write the following instructions:
>> Month=[1:1:12];
>> Temperature=[ 17 19 22 24 27 30 34 35 31 26 23 20];
>> plot(Month,Temperature)
%% try : >> stem(Month,Temperature)

4.4. Polynomials
Sometimes you need to write a polynomial, find its roots, multiply it by another
one, divide it by another polynomial, differentiate it, integrate it, substitute by a value in
it, put it in partial fractions form, or fit it to get a curve of specific order. Make full use of
the following table for that purpose:
P=[1 3 4 4 6] P = x4 + 3x3 + 4x2 + 4x + 6
Q=[1 6 2] Q = x2 + 6x + 2
C=poly([1 2 3]) C = (x 1)(x 2)(x 3)
conv(P,Q) Multiply P*Q
[s,r]=deconv(P,Q) Divide P/Q, where P=s+r/Q
roots(P) Find the roots of P (put P=0 get x)
polyder(P) Derivative of P
polyval(P,3) Substitute in P by x=3
polyint(P,2) Integrate P with constant of integration =2

[Link] M-files

[Link] Help

You might also like