Some MATLAB commands for MATH-1
Some 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 𝑥 ∈ ℝ)
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 )
Plotting commands:
syms x
Plotting of graph f(x)= …
fplot( f )
Specifying plotting range
Complex numbers:
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 )
syms x y
Defining a function (2 variables)
f=… or: f(x,y) = …
Plotting (2 variables) fsurf (f)
𝑑𝑥 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)