Beginner Matlab ODE
Beginner Matlab ODE
Beginner Matlab ODE
Content
symbolic expression
Inline function
differential and integral
Prologue
Your Petroleum E&P company just received TOC log data from
the laboratory (EX2.mat). Unfortunately, your boss seems to be unsatisfied
with a huge data table.
TASK
Graphically present your data with plot function. The first column
is TVD and the second one is TOC. Depth and TOC is in meter(m) and
%wt unit, respectively.
BEWARE: Examine your data carefully !!
Symbolic expression
variable_name = sym(variable_name)
>> x = sym(x)
x=
x
>> x = sym(x)
x=
x
>> 5*x + 3*x
ans =
8x
Symbolic expression
sym can also convert pi, which MATLAB defines as a double
variable (i.e. 3.1416), to an irrational number 3.1415926...
>> cos(pi/2)
ans =
6.1232e-17
>> cos(sym(pi/2))
ans =
inline function
>> f = inline(x.^2,x)
f=
Inline function
f(x) = x.^2
>> f([1 2 3])
ans =
[1
9]
inline function
Exercise 2.1
x 1
f
x
e
If
and g x
1.
2.
f 2
f 1 g 5
3.
f og 4
4.
fog x
f :D
where
f x 2 x 3 then
If
d
f x 2 x3 6 x 2
dx
d2
f x 2 2 x 3 12 x
dx
3 4
2 x dx 2 x c
3
0 2 x dx 2
3
Differentiation
Indefinite Integral
Definite Integral from
a to b
Limit
Note: direction = left or right
diff(function,x,order)
int(function,x)
int(function,x,a,b)
limit(function,x,point,direction)
d 2 x 1
e x
dx
2.
x 2 y y 1
yx
1.
3.
x
sin
e
dx
0
4 1
2
2
x
y
dx dy
4.
2 0
5.
1sin
r 2 dr d
2
x
4
6. lim
x 2 x 2
Exact solution
Numerical Solution
dsolve
ode45, ode113,.
Dvariable_name
* Note second-order differentiation is D2variable_name
- When you are trying to solve an equation you must use ==
instead of =.
- The following example solves y x y x 1 x
>> dsolve(Dy+y+1==x,x)
ans =
x + C2*exp(-x) -2
dx
xt 0
Example: Find the general solution and particular for
dt
with the initial condition x 0 1
>> syms x(t);
>> dsolve(diff(x,t) x*t==0)
ans =
C2*exp(t^2/2)
Dy = diff(y,x)
y x c1
d x
2x 0
2
dt
with the initial conditions: x 0 5, x 0 0
>> syms x(t);
>> Dx = diff(x,t);
>> x(t) = dsolve(diff(x,t,2)+2*x==0,x(0)==5,Dx(0)==0)
x(t) =
5*cos(2^(1/2) * t)
>> t = [0:0.1:5];
>> plot(t,x(t))
d
y y 2e 5t
dt
2
d
q
2.
5q 0
2
dt
d 2q
3.
5q 0
2
dt
1.
4.
; y 0 1
q 0 5, q 0 0
x t 0.05 x t 2 x t 0
d3y
y
5.
3
dx
and plot the solution.
x 0 5, x 0 0
, y 0 1, y 0 1, y 0
[t,y] = ode45(odefun,tspan,y0)
To define f
command
[t,y] = ode45(odefun,tspan,y0)
automatic tspan
[0 1]
specific tspan
[0:0.1:1]
(i.e. [0 0.1 0.2 . 1])
>> y0 = 0;
>> [t,y] = ode45(@(t,y) 2*t-3, [0 2 6], y0)
>> t =
0
2
6
y=
0
-2.0000
18.0000
d
Exercise 2.4: Numerically solve
y y 2e 5t in the time interval
dt
from 0 to 5 and the initial condition 0 = 1
y 1 , y 2
Problem (1)
1. A spring constant k attach to a block mass m , rested on the
frictionless floor. Suppose that the block size is very small so that air
resistance force is proportional to its velocities in the form
f bv
where b is a constant. The equation of motion of this system is
described as
d 2x
dx
m 2 b kx 0
dt
dt
where x is the block position at time
equilibrium point.
t . We define x 0 at the
TASK: Graph and the position and velocity of the block versus time
in 2 separated graphs under each scenarios:
a. m 1.00 kg, k =5.00 N/m, b 0.50 N s/m
b. m 1.00 kg, k =5.00 N/m, b 5.0 N s/m
The initial parameter for both conditions are x 0 5.00 m,
x 0 0
Problem (2)
2. (Force driven oscillation)The block in the problem 1 is undergoing
damp oscillation by the external force F t F0 cos t . Hence,
the equation of motion of the block is
m
d 2x
dx
F t
k
m 2 b kx F0 cos t
dt
dt
The general solution is x t xtransient xsteady state where
F0 / m
2
0
b
2m
4 2 2
b
tan
2
k
k
0
m
Problem (2 con.)
By using MATLAB, answer the following questions under this
circumstance:
sin x / 4 cos x / 4
3. Plot the block position versus time from 0 20 s .
4. After a long time, shows that
Problem (3)
Differential Effective Medium (DEM)
In 1985, Norris derive a Differential equation for calculating the
Effective elastic modulus tensor of any Medium. Later, Berryman (1992)
simplifies Norriss equation for an isotropic linear elastic material as
where
d
*i
*
*
1 K Ki K P
d
d
*i
*
*
Q
i
d
K , G
K i , Gi
Problem (3 con.)
In a specific case, if the inclusion is a sphere shape, the constant
*i
*i
P , Q are defined as
where
*i
Gm 9 K m 8Gm
4
Gm
K m Gm
6 K m 2Gm
*i
3
,Q
4
Gm 9 K m 8Gm
K i Gm
Gi
3
6 K m 2Gm
Gquartz 44 GPa
K water 2.2GPa
Gwater 0GPa