Intro To Matlab
Intro To Matlab
b = a + 2
Creating graphs in MATLAB is as easy as one command.
plot(b)
plot(b,'*')
axis([0 10 0 10])
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
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)
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