mathworks.com/help/matlab
Integration and Differentiation
integral(f,a,b)
Numerical integration
(analogous functions for 2D and 3D)
t ra p z(x,y)
Trapezoidal numerical integration
d iff(X)
Differences and approximate derivatives
gra d ient(X)
Numerical gradient
c u r l( X,Y,Z,U,V,W )
Curl and angular velocity
d iv e r g e n c e( X,..,W )
Compute divergence of
vector field
o d e45(o d e,t s p a n,y 0)
Solve system of nonstiff ODEs
ode15s(o de,tspan,y0)
Solve system of stiff ODEs
deval(sol,x)
Evaluate solution of differential equation
pdepe(m,pde,ic,...
b c,x m,t s)
Solve 1D partial differential equation
p d e v a l( m,x m e s h ,...
usol,x q)
Interpolate numeric PDE solution
Numerical Methods
fz er o(f u n,x0)
Root of nonlinear function
f m i n s earc h(f u n,x0)
Find minimum of function
fminbnd(fun,x1,x2)
Find minimum of fun in [x1, x2]
fft(x), ifft(x)
Fast Fourier transform and its inverse
Interpolation and Polynomials
interp1(x,v,xq)
1D interpolation
(analogous for 2D and 3D)
pchip(x,v,xq)
Piecewise cubic Hermite polynomial
interpolation
spline(x,v,xq)
Cubic spline data interpolation
ppval(pp,xq)
Evaluate piecewise polynomial
mkpp(breaks,coeffs)
Make piecewise polynomial
unmkpp(pp)
Extract piecewise polynomial details
p oly(x)
Polynomial with specified roots x
p o l y e i g( A 0, A 1,..., A p)
Eigenvalues for polynomial eigenvalue
problem
p o ly f it(x,y,d)
Polynomial curve fitting
re sid u e(b,a)
Partial fraction expansion/decomposition
ro ot s( p)
Polynomial roots
polyval(p,x)
Evaluate poly p at points x
polyint(p,k)
Polynomial integration
p oly d er(p)
Polynomial differentiation
Programming Methods
Functions
% Save your function in a function file or at the end
% of a script file. Function files must have the
% same name as the 1st function
function cavg = cumavg(x) %multiple ar gs. possi ble
cavg=cu msum(vec)./(1:lengt h(vec));
end
Anonymous Functions
% defined via function handles
fun = @(x) cos(x.^2)./abs(3*x);
Control Structures
if, elseif Conditions
if n<10
d isp("n smaller 10")
elseif n<=20
d isp("n between 10 and 20")
else
d isp("n larger than 20")
Switch Case
n = input("Enter an integer: ");
switch n
case -1
d isp("negative one")
case {0,1,2,3} % check four cases together
d isp("integer between 0 and 3")
otherwise
d isp("integer value outside interval [-1,3]")
end % control structures terminate with end
For-Loop
% loop a specific number of times, and keep
% track of each iteration with an incrementing
% index variable
for i = 1:3
d isp("cool");
end % control structures terminate with end
While-Loop
% loops as long as a condition remains true
n = 1;
nFactorial = 1;
while nFactorial < 1e100
n = n + 1;
nFactorial = nFactorial * n;
end % control structures terminate with end
Further programming/control commands
break
Terminate execution of for- or while-loop
continue
Pass control to the next iteration of a loop
t r y, c a t c h
Execute statements and catch errors