#17 (04/10/2024)

Anonymous function in MATLAB/Octave

f1=@(x) x^2-x +1 ;
f2=@(x,y) x^2-3*y + x*y;

f1(5)
f2(4,5)

Why would you still want to use C after learning MATLAB/Octave ?


Differential equations


dy

dt
= f(t, y),
(1)
where t is the independent variable and f(t, y) is a function of t and y. Equation (1) along with the initial boundary condition of
y(t0) = y0,
(2)
is called the initial value problem.
Examples
  1. Launch angle of a projectile (Newton's second law)

    m a = F,
    (3)
    or
    m





    d2 x

    d t2
    d2 y

    d t2






    =


    Fx
    Fy



    ,
    (4)
    or
    m





    d2 x

    d t2
    d2 y

    d t2






    =


    0
    m g



    .
    (5)
    The differential equations to be solved are

    d2 x(t)

    d t2
    = 0,     d2 y(t)

    d t2
    = − g.
    (6)
    The initial conditions (t = 0) are

    x(0) = 0,     y(0) = 0,     d x

    dt


    t = 0 
    = v0 cos θ,     d y

    dt


    t = 0 
    = v0 sin θ.
    (7)
    By integrating eq.(6)

    d x(t)

    d t
    = c1,     d y(t)

    d t
    = − g t + c2.
    (8)
    By integrating eq.(8) again, one obtains

    x(t) = c1 t + c3,     y(t) = − 1

    2
    g t2 + c2 t + c4.
    (9)
    The unknown constants, c1  ∼ c4, can be determined from eq.(7) as

    c1 = v0 cos θ,     c2 = v0 sin θ,     c3 = 0,     c4 = 0.
    (10)
    So the solutions for x(t) and y(t) are

    x(t) = v0 cos θ t     y(t) = − 1

    2
    g t2 + v0 sin θ t.
    (11)
    When the projectile is on the ground,

    0 = − 1

    2
    g t2 + v0 sin θ t,
    (12)
    from which
    t = 2 v0 sin θ

    g
    .
    (13)
    Therefore,
    x
    2 v0 sin θ

    g

    = v0 cos θ × 2 v0 sin θ

    g
    = vo2

    g
    sin 2 θ.
    (14)
    This quantity is maximized when θ = π/4 (link).
  2. Population growth (logistic equation)
    Population growth can be modeled by
    dU(t)

    dt
    = αU (t),
    (15)
    where U(t) is the population of a country at time t and α is the growth rate.
    Equation (15) can be re-written as
    1

    U
    d U = α d t
    (16)
    By integrating equation (16) with respect to each variable, one obtains (separation of variables)

    ln U = αt + C,
    (17)
    where C is an integral constant.
    Equation (17) can be written as

    U(t) = eαt + C = eC  eαt.
    (18)
    If the initial population is U0 at t = 0, equation (18) can be written as

    U0 = eC.
    (19)
    So finally, one obtains

    U(t) = U0 eαt.
    (20)
    Correction to the above equation can be made as

    dU

    dt
    = αU (1 − U

    β
    )
    (21)
    where β is known as the carrying capacity.
Analytically solvable differential equations
Analytically solvable differential equations are extremely limited. The include
  1. Linear differential equations with constant coefficients
    Examples:
    1. y"(t) − 3 y′(t) + 2 y(t) = 0,     y(0) = a, y′(0) = b.
      (Solution): Solve λ2 − 3 λ+ 2 = 0 as λ1 = 1, λ2 = 2. Then, express y(t) = c1 et + c2 e2 t. Determine c1 and c2 to satisfy the initial conditions.
    2. y"(t) + 4 y(t) = 0,     y(0) = a, y′(0) = b.
      (Solution): Solve λ2 + 4 = 0 as λ1 = 2 i , λ2 = − 2 i. Then, express y(t) = c1 e2 i t + c2 e− 2 i t = c1 cos 2 t + c2 sin 2 t. Determine c1 and c2 to satisfy the initial conditions.
  2. Separation of variables

    dy

    dt
    = f(y) g(t) ⇒ 1

    f(y)
    d y = g(t) d t.
    Integrating both sides of the equation with respect to each variable, one gets

    1

    f(y)
    d y =
    g(t) dt.
    Example:
    d y

    d t
    = y (1 − y).

    1

    y (1 − y)
    d y = 1  d t, ⇒
    1

    y
    1

    y − 1

    d y = d t ⇒ (ln y − ln (y − 1) ) = t + C
    The general solution is
    ln  y

    y − 1
    = t + C.




File translated from TEX by TTH, version 4.03.
On 09 Apr 2024, 15:03.