Professional Documents
Culture Documents
2021-22
2
But before all that: how to define functions in
MATLAB
If you want to write a function, for example: f(x)= x2
equivalent to
the function
the independent
variable
3
How to define functions with more than one
variable in MATLAB
Area of a rectangle = length × width (apply to l=2 and w=5)
the function
4
Common challenge for engineers: how to solve
differential eqs. or integrals
a) Use your already acquired maths skills
b) Get help from symbolic calculation in Matlab
Analytically
b1) syms x; int(
int( x^2, 1, 2)
Single
b2) syms y(x); dsolve(
dsolve( diff(y) == x^2, y(1)== 0)
ODE
Numerically
a table (xi,yi) is given
e) trapz(
trapz( xi, yi)
yi)
(or a table (xi) and a function y=f(x))
dP
Solve the ODE: = x2 + 2
dx
( )
dP = x 2 + 2 dx
+ 2 dx
( )
2
dP = x
x3
P = + 2 x + constant
y ( x ) dx
3
6
b1) Get help from symbolic calculation in Matlab
(command “int
int”)
int
dP = ( x ) same example
y ( x ) dx
2
+ 2 dx
7
b1) Get help from symbolic calculation in Matlab
(command “int
int”)
int
equivalent to y ( x ) dx
using >> int(
int(y,x)
y,x) is also valid
To add the
integration
constant
Easy peasy!!
peasy!! 8
b1) Get help from symbolic calculation in Matlab
(command “int
int”)
int
Easy peasy!!
peasy!! 9
b2) Get help from symbolic calculation in Matlab
(command “dsolve
dsolve”)
dsolve
If choose to do
it analytically
Practise on your own time the command “dsolve
dsolve”
dsolve
dy
= ty
dt
dy
= ty , y(0) = 2
dt
with initial conditions 10
b2) Get help from symbolic calculation in Matlab
(command “dsolve
dsolve”)
dsolve
Enjoy
while
practising!
11
c) Command “integral
integral”
integral (numerical integration)
For example:
12
How to solve systems of nonlinear equations in
Matlab (“fsolve
fsolve”
fsolve command)
• https://www.youtube.com/watch?v=iEoSvPkJ2
g8
• https://www.youtube.com/watch?v=al-
4ZBLbV_A
13