Matlab commands for MATH-1
Algebraic operations:
√𝑥 sqrt(x)
𝑒𝑥 exp(x)
ln(𝑥) log(x)
log(𝑥) log10(x)
sin−1(𝑎) asin(x) ( x in radians)
sin(𝑣) sind(v) ( v in degrees)
Special functions and numbers 𝑎𝑥 a^x
|𝑦| abs(y)
𝜋 pi
𝑒 exp(1)
𝑗 = √−1 i (or j)
∞ inf
Define symbolic variables syms x y a b c
(e.g. x and y and constants a, b, c)
syms x (of course more variables can be involved!)
Simplify or reduce an expression
simplify( … )
syms x
Factorization of an expression
factor( … )
Expand syms x
(e.g. multiply expressions etc.) expand ( … )
Polynomial division: syms x
p=…
𝑝(𝑥)
(Given 𝑓(𝑥) = 𝑞(𝑥) ) q=…
quorem ( p , q ) + polynomialReduce ( p , q ) / q
Partial fractions: syms x
(of an expression) partfrac( expression )
Solve an equation syms x or: syms x real (specifies that 𝑥 ∈ ℝ)
Method 1: solve(expression 1 == expression 2)
Method 2: solve(expression1 == expression 2 , x ) (specifies the variable)
Method 3: (one solution from “guess” = a) vpasolve(expression1 == expression 2 , x , a )
Trigonometric equation syms x
(Get distinct solutions) solve( sin(x) == -0.2, x)
e.g. sin(𝑥) = −0.2 double(ans)
Equations syms x
(Get one solution in given interval)
e.g. sin(𝑥) = −0.2, 𝑥 ∈ [7; 12]
vpasolve( sin(x) == -0.2, x, [ 7, 12] )
System of equations
syms x y
𝑥+𝑦 = 5
e.g. { solve( x+y == 5, 3*x+2*y == 14 )
3𝑥 + 2𝑦 = 14
[ans.x , ans.y] (this last command only needed for non-linear equations)
Inequalities syms x
S = solve( x^2-x < 0, x, 'ReturnConditions', true )
e.g. 𝑥2 − 𝑥 < 0 S.conditions
Approximation of last answer double (ans) or: double(value)
More digits in last answer vpa( ans, 8)
Subtitute x=2 into last answer subs( ans, x, 2)
Write in symbolic form (if possible) sym(ans)
Clear variables clear
Functions (of one variable):
syms x
Define a function
f(x)= … or: f= …
Function value f(3) or: subs(f,x,3)
Composite function
(𝑓 ∘ 𝑔)(𝑥) = 𝑓(𝑓(𝑥))
f(g(x)) or: compose ( f, g )
Minimum in given interval syms x
3 2
(𝑓(𝑥) = 𝑥 − 3𝑥 − 𝑥, − 2 ≤ 𝑥 ≤ 4) fminbnd ( @(x) x^3-3*x^2-x , -2 , 4 )
Maximum in given interval syms x
(𝑓(𝑥) = 𝑥 3 − 3𝑥 2 − 𝑥, − 2 ≤ 𝑥 ≤ 4) fminbnd ( @(x) -(x^3-3*x^2-x) , -2 , 4 ) note the sign!
Plotting commands:
syms x
Plotting of graph f(x)= …
fplot( f )
Specifying plotting range
e.g. 𝑥 ∈ [−10; 10] fplot ( f , [ -10 , 10 ] ) (comma in interval not necessary)
e.g. 𝑥 ∈ [−10; 10] ∧ 𝑦 ∈ [−8; 12] fplot ( f )
xlim ( [ -10 , 10 ] )
ylim ( [ -8 , 12 ] ) (commas in intervals not necessary)
Include x-axis yline(0)
Include y-axis xline(0)
syms x
f(x) = …
Plotting more graphs in one plot
g(x) = …
fplot( [ f , g ] ) (comma not necessary)
Plotting a parametric syms x y t
representation x(t) = … ; x = … (is enough for just plotting)
y(t) = … ; y=…
( t-interval is not mandatory. Default
is[−5; 5] )
fplot ( x , y , [ t1 , t2 ] )
Plotting graph of an implicit
syms x y
function
fimplicit ( x^2 + y^2 == 1 , [ -3 , 3 ] ) (interval not mandatory)
e.g. 𝑥 2 + 𝑦 2 = 1
syms theta
Polar plot (polar coordinates) r = sin ( theta )
e.g. 𝑟 = sin 𝜃 fpolarplot (r) (need MATLAB 2024a for this feature)
or: ezpolar (r) (this is an old command, which will disappear from MATLAB at some point)
Complex numbers:
Defining a complex number z = 2 + 3*j or: z = 2 +3j
Real part of z real (z)
Imaginary part of z imag (z)
Argument (phase angle) of z angle (z)
The conjugate of z conj (z)
Modulus (magnitude) of z abs (z)
Vector and matrix algebra:
Defining a vector a=[1 2 3] or: a=[1,2,3]
Scalar product dot( a, b)
Vector product cross( a, b)
Matrix definition
2 1 3 A=[2 1 3;1 3 2] or: A=[2,1,3;1,3,2]
(Example: 𝐴 = [ ])
1 3 2
A=[ … ]
Transposed matrix
A’
A=[ … ]
Determinant of matrix
det (A)
A=[ … ]
Adjoint matrix
adjoint (A)
A=[ … ]
Inverse matrix
A^-1 or: inv(A)
A=[ … ]
Co-factor matrix
( det(A) * A^-1 )' or: (adjoint (A) ) ‘
Eigenvectors and eigenvalues of a A=[ … ]
matrix [M,S]=eig(A)
Differentiation and integration:
syms x
Differentiation of an explicit
f(x) = …
function
diff ( f , x )
syms x
n’th order derivative of an explicit
f(x) = …
function
diff ( f , n )
syms x y t
x=x(t);
Parametric differentiation
y=y(t);
diff (y,t) / diff (x,t)
Implicit differentiation syms x y
(on form 𝑓(𝑥, 𝑦) = 𝑐 ) -diff ( f, x) / diff ( g , y)
Indefinite integration syms x
(of function expression f(x) ) int ( f , x)
Definite integration syms x
𝑏
( ∫𝑎 𝑓(𝑥)𝑑𝑥 ) int ( f , x , a , b )
Taylor polynomial of order n about syms x
x=a taylor ( f , x , a , 'order' , n+1 )
Maclauren series expansion of syms x
order n taylor ( f , x , 0 , 'order' , n+1 )
Limit lim (𝑓(𝑥)) syms x
𝑥→𝑎 limit ( f , x , a )
Functions of several variables:
syms x y
Defining a function (2 variables)
f=… or: f(x,y) = …
Plotting (2 variables) fsurf (f)
Contourplot (2 variables) fcontour (f)
Contourplot (fixed level curves) fcontour ( f , 'LevelList' , [-2,-1,0,1,2,3,4] )
Partial derivatives
𝜕𝑓 diff ( f, y)
(e.g. )
𝜕𝑦
Direct 2nd order partial derivatives
𝜕2𝑓 diff ( f, y, 2)
(e.g. 𝜕𝑦 2 )
Cross-partial derivatives
𝜕2 𝑓 diff ( diff( f, y), x)
(e.g. 𝜕𝑥𝜕𝑦 )
Differential equations:
𝑑𝑥 syms x(t)
General solution to = 𝑓(𝑡, 𝑥)
𝑑𝑡 dsolve ( diff (x) = = f( t, x) )
𝑑𝑦
syms y(x)
e.g. = 3 − 2𝑦
𝑑𝑥 dsolve ( diff (y) = = 3-2*y )
Particular solution
syms x(t)
𝑑2𝑥 𝑑𝑥 𝑑𝑥 Dx = diff(x,t);
e.g. +9 = 2, 𝑥(0) = 4, | =3
𝑑𝑡 2 𝑑𝑡 𝑑𝑡 𝑡=𝜋/3 dsolve( diff (x,2) + 9*diff (x) == 2 , x(0) = = 4 , Dx(pi/3) = = 3)
Numerical solution (i.e. graph only) [ t , x ] = ode45 ( @(t,x) f(t,x) , [𝑡0 , 𝑡𝑒𝑛𝑑 ], 𝑥(𝑡𝑜 ))
plot (t,x)
𝑑𝑥
of = 𝑓(𝑡, 𝑥)
𝑑𝑡 where you substitute f(t,x) with the right-hand-side of the ODE, 𝑥(𝑡0 ) is
the x-value for the initial condition(i.e. at 𝑡0 ), and 𝑡𝑒𝑛𝑑 is the upper end
of the domain in which you want the plot.
Direction field: [t,x]=meshgrid(0:0.2:5,-1:0.2:4);
Define S = dx/dt: S=x.*(1-x).*t; Note: Use dot in front of * and / and ^
Example here for dx/dt = x ( 1 - x ) t L=sqrt(1+S.^2);
with t ∈ [0;5] and x ∈ [-1;4] and with quiver(t,x,1./L,S./L,0.5)
tangent lines of length 0.5 spaced by 0.2
(colours just indicate positions in the
code)