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

Intro To Matlab

The document discusses basic matrix operations in MATLAB such as creating vectors and matrices, performing element-wise operations, matrix multiplication, solving systems of equations using matrices, and computing eigenvalues and characteristic polynomials of matrices. Key functions and operations mentioned include creating and manipulating vectors and matrices, matrix transpose, matrix multiplication, element-wise multiplication, solving systems of equations using backslash, eig, poly, and roots.

Uploaded by

Hadiqa naaz
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)
43 views

Intro To Matlab

The document discusses basic matrix operations in MATLAB such as creating vectors and matrices, performing element-wise operations, matrix multiplication, solving systems of equations using matrices, and computing eigenvalues and characteristic polynomials of matrices. Key functions and operations mentioned include creating and manipulating vectors and matrices, matrix transpose, matrix multiplication, element-wise multiplication, solving systems of equations using backslash, eig, poly, and roots.

Uploaded by

Hadiqa naaz
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

Basic Matrix Operations

First, let's create a simple vector with 9 elements called a.


a = [1 2 3 4 6 4 3 4 5]
Now let's add 2 to each element of our vector, a, and store the result in a new vector.

b = a + 2
Creating graphs in MATLAB is as easy as one command.

plot(b)
plot(b,'*')
axis([0 10 0 10])

One area in which MATLAB excels is matrix computation.


Creating a matrix is as easy as making a vector, using semicolons (;) to separate the rows of a matrix.
A = [1 2 0; 2 5 -1; 4 10 -1]
We can easily find the transpose of the matrix A.

B = A'
Now let's multiply these two matrices together.

C = A * B
Instead of doing a matrix multiply, we can multiply the corresponding elements of two matrices or
vectors using the .* operator.

C = A .* B

Let's use the matrix A to solve the equation, A*x = b. We do this by using the \ (backslash) operator.

b = [1;3;5]
x = A\b

There are functions to obtain eigenvalues ...


eig(A)
The "poly" function generates a vector containing the coefficients of the characteristic polynomial.
The characteristic polynomial of a matrix A is

det(λI−A)
p = round(poly(A))
We can easily find the roots of a polynomial using the roots function.

roots(p)
diff
Differentiate symbolic expression or function

Syntax
diff(F)
diff(F,var)
diff(F,n)
diff(F,var,n)
diff(F,var1,...,varN)

Description
diff(F) differentiates F with respect to the variable determined by symvar(F,1).
diff(F,var) differentiates F with respect to the variable var.
diff(F,n) computes the nth derivative of F with respect to the variable determined by symvar.
diff(F,var,n) computes the nth derivative of F with respect to the variable var.
diff(F,var1,...,varN) differentiates F with respect to the variables var1,...,varN.

Examples
Differentiation of Univariate Function
Find the first derivative of this univariate function:
syms x
f(x) = sin(x^2);
df = diff(f,x)
df(x) = 2*x*cos(x^2)
Differentiation with Respect to Particular Variable
Find the first derivative of this expression:
syms x t
diff(sin(x*t^2))
ans = t^2*cos(t^2*x)
Because you did not specify the differentiation variable, diff uses the default variable defined
by symvar. For this expression, the default variable is x:
symvar(sin(x*t^2),1)
ans = x
Now, find the derivative of this expression with respect to the variable t:
diff(sin(x*t^2),t)
ans = 2*t*x*cos(t^2*x)
Higher-Order Derivatives of Univariate Expression
Find the 4th, 5th, and 6th derivatives of this expression:
syms t
d4 = diff(t^6,4)
d5 = diff(t^6,5)
d6 = diff(t^6,6)
d4 = 360*t^2
d5 = 720*t
d6 = 720
Higher-Order Derivatives of Multivariate Expression with Respect to Particular
Variable
Find the second derivative of this expression with respect to the variable y:
syms x y
diff(x*cos(x*y), y, 2)
ans =
-x^3*cos(x*y)

Higher-Order Derivatives of Multivariate Expression with Respect to Default


Variable
Compute the second derivative of the expression x*y. If you do not specify the differentiation
variable, diff uses the variable determined by symvar. For this
expression, symvar(x*y,1) returns x. Therefore, diff computes the second derivative of x*y with
respect to x.
syms x y
diff(x*y, 2)
ans = 0
If you use nested diff calls and do not specify the differentiation variable, diff determines the
differentiation variable for each call. For example, differentiate the expression x*y by calling
the diff function twice:
diff(diff(x*y))
ans = 1
In the first call, diff differentiate x*y with respect to x, and returns y. In the second
call, diff differentiates y with respect to y, and returns 1.
Thus, diff(x*y, 2) is equivalent to diff(x*y, x, x), and diff(diff(x*y)) is equivalent
to diff(x*y, x, y).

Mixed Derivatives
Differentiate this expression with respect to the variables x and y:
syms x y
diff(x*sin(x*y), x, y)
ans =
2*x*cos(x*y) - x^2*y*sin(x*y)
You also can compute mixed higher-order derivatives by providing all differentiation variables:
syms x y
diff(x*sin(x*y), x, x, x, y)
ans =
x^2*y^3*sin(x*y) - 6*x*y^2*cos(x*y) - 6*y*sin(x*y)
int
Definite and indefinite integrals

Syntax
int(expr,var)
int(expr,var,a,b)

Description
int(expr,var) computes the indefinite integral of expr with respect to the symbolic scalar
variable var. Specifying the variable var is optional. If you do not specify it, int uses the default
variable determined by symvar. If expr is a constant, then the default variable is x.
int(expr,var,a,b) computes the definite integral of expr with respect to var from a to b. If you do
not specify it, int uses the default variable determined by symvar. If expr is a constant, then the
default variable is x.
int(expr,var,[a,b]), int(expr,var,[a b]), and int(expr,var,[a;b]) are equivalent
to int(expr,var,a,b).

Examples
Indefinite Integral of Univariate Expression
Find an indefinite integral of this univariate expression:
syms x
int(-2*x/(1 + x^2)^2)
ans = 1/(x^2 + 1)
Indefinite Integrals of Multivariate Expression
Find indefinite integrals of this multivariate expression with respect to the variables x and z:
syms x z
int(x/(1 + z^2), x)
int(x/(1 + z^2), z)
ans =
x^2/(2*(z^2 + 1))

ans = x*atan(z)
If you do not specify the integration variable, int uses the variable returned by symvar. For this
expression, symvar returns x:
symvar(x/(1 + z^2), 1)
ans = x

You might also like