Saturday, February 14, 2015

MATLAB TuTer 5: Calculus Calculations using MATLAB



Calculus Calculations

1.     Taking Derivatives of a function:

To calculate the derivatives of a function for e.g.' f(x)' with respect to 'x' using MATLAB, there is a function associated with this objective which can be extracted as:

>> diff (f(x),x)

Where f(x) is the function expression or predefined function using "inline", and x is predefined symbol that represent the independent variable of the function and the derivate respective variable.

Example:
Find the derivate of [ X3 + 3X2 – 5X ], using MATLAB

Solution:

>> syms X
>> function1=inline('X^3+3*X^2-5*X','X')

function1 =
     Inline function:
     function1(X) = X^3+3*X^2-5*X

>> diff (function1(X),X)
 ans =
 3*X^2 + 6*X – 5

Example:
Function: [ log(u) + exp(u) – 3i ]

Solution:

>> syms u
>> function2 = inline('log(u)+exp(u)-3i','u')

Function2 =
     Inline function:
     Function2(u) = log(u)+exp(u)-3i
>> diff (function2(u),u)
 ans =
 exp(u) + 1/u

2.     Evaluating derivative at a point:

To use the derivative of a function and evaluate any value or point at its variable, there is two steps to do that:
·        Define the original function using inline.
·        Define other inline function for derivative function
Example: 
Function is [X2 + 3X - X ]

Solution:

>> syms X
>> function3 = inline ('X^2+3*X-X','X')

Function3 =
     Inline function:
     Function3(X) = X^2+3*X-X

>> Diff_F3 = inline (diff (function3(X), X),'X')
Diff_F3 =
     Inline function:
     Diff_F3(X) = X.*2.0+2.0

>> Diff_F3 (6)
ans =
    14

>> Diff_F3 (23.6)
ans =
   49.2000

3.     Calculate partial derivatives:

When the equation consists of more than one variable, the partial derivative can be calculated with the same way using the function and the variable with respect of.

Example:
Find derivate of [ sine (a)/cosine (b) ] with respect to "a" and to "b"

Solution:

>> syms a b
>> Function4 = inline('sin(a)/cos(b)','a','b')

Function4 =
     Inline function:
     Function4(a,b) = sin(a)/cos(b)

>> diff (Function4(a,b),a)
 ans =
 cos(a)/cos(b)

>> diff (Function4(a,b),b)
 ans =
 (sin(a)*sin(b))/cos(b)^2

The partial derivative result also can be used as a function to evaluate any point in it.  

Example:
X3 + 2Y2 – 3Z

Solution:
>> syms X Y Z
>> F5 = inline('X^3+2*Y^2-3*Z','X','Y','Z')

F5 =
     Inline function:
     F5(X,Y,Z) = X^3+2*Y^2-3*Z

>> Diff_F5_X = inline(diff(F5(X,Y,Z),X),'X')
Diff_F5_X =
     Inline function:
     Diff_F5_X(X) = X.^2.*3.0

>> Diff_F5_Y = inline(diff(F5(X,Y,Z),Y),'Y')
Diff_F5_Y =
     Inline function:
     Diff_F5_Y(Y) = Y.*4.0

>> Diff_F5_Z = inline(diff(F5(X,Y,Z),Z),'Z')
Diff_F5_Z =
     Inline function:
     Diff_F5_Z(Z) = -3.0

>> Diff_F5_X_Y =inline(diff(F5(X,Y,Z),Y),'X','Y')
Diff_F5_X_Y =
     Inline function:
     Diff_F5_X_Y(X,Y) = Y.*4.0

>> Diff_F5_X_Y_Z = inline(diff(F5(X,Y,Z),X),'X','Y','Z')
Diff_F5_X_Y_Z =
     Inline function:
     Diff_F5_X_Y_Z(X,Y,Z) = X.^2.*3.0


      Then any point can be evaluated in any function of the new derivative functions created just rely on the independent variable that with respect to.

>> Diff_F5_X_Y(23,15)
ans =
        1587

>> Diff_F5_X_Y_Z(12,15.5,22)
ans =
   432

Ordered degree derivatives:

The nth order derivative of the function f(x) along x can be expressed as follow:

>> diff(f(x),n), where n is the nth order of derivative.

Example:

>> f1 = inline('x^3+2*x^2-3*x-5','x')

f1 =
     Inline function:
     f1(x) = x^3+2*x^2-3*x-5
% the function is defined for x independent.

>> syms x

>> diff(f1(x)) % get the 1st order derivative.
ans = 3*x^2 + 4*x - 3

>> diff(f1(x),x)% same result as before.
ans = 3*x^2 + 4*x - 3

>> diff(f1(x),2)% the 2nd order derivative.
ans = 6*x + 4

>> diff(f1(x),3)% the 3rd order derivative.
ans = 6

-   Another example if we have more than one independent variable in a function:

>> syms a b

>> Function4 = inline('sin(a)/cos(b)','a','b')

>> diff(Function4(a,b),2,b)% 2nd order along b.
ans =sin(a)/cos(b) + (2*sin(a)*sin(b)^2)/cos(b)^3

>> diff(Function4(a,b),2,a)% 2nd order along a.
ans = -sin(a)/cos(b)

>> diff(Function4(a,b),3,a)% 3rd order along a.
ans = -cos(a)/cos(b)

 

4.     Taking the integrals of a function:

Integral function in MATLAB is "int(f(x),x)" but in integral there is no need to define the variable that integral taken with respect to, this in case there is only one variable in the equation.

Example:
Sin(t) + e(-2t)^2 +2t3

Solution:

>> syms t

>> F7 = inline ('sin(t)+exp(-2*t^2)+2*t^3','t')

F7 =
     Inline function:
     F7(t) = sin(t)+exp(-2*t^2)+2*t^3

>> INT_F7 = inline(int(F7(t)))

INT_F7 =
     Inline function:
     INT_F7(t) =
-cos(t)+t.^4.*(1.0./2.0)+sqrt(2.0).*sqrt(pi).*erf(sqrt(2.0).*t).*(1.0./4.0)

>> INT_F7(10)
ans =
   5.0015e+03

>> pretty(INT_F7(t))
                        1/2                4
  2822212540896131 erf(2    t)            t
  ---------------------------- - cos(t) + --
        4503599627370496                  2


Example:
2V3 + 3V2 – T3, find the integral with respect to V, and evaluate the value (12.5,22.3)

Solution:

>> syms V T

>> F6 = inline ('2*V^3+3*V^2-T^3','V','T')
F6 =
     Inline function:
     F6(V,T) = 2*V^3+3*V^2-T^3

>> int_F6_V = inline (int(F6(V,T),V),'V','T')

int_F6_V =
     Inline function:
     int_F6_V(V,T) =
                 -T.^3.*V+V.^3+V.^4.*(1.0./2.0)

>> int_F6_V(12.5,22.3)

ans =
  -1.2446e+05

 

4.1.         Definite integration:

The examples before demonstrate indefinite integration. To calculate definite integration for "f(x)" with lower bound "a" and higher bound "b" using the "int" function we just modify the syntax by:

int (f(x),a,b)

Which mean calculate the integral for "f(x)" and evaluate it from "a" to "b".

Example:
Find the integral of [X2 + 3X] and evaluate the integration from 2 to 5

Solution:

>> syms X

>> F8 = inline('x^2+3*x','x')

F8 =
     Inline function:
     F8(x) = x^2+3*x

>> INT_F8 = int(F8(X))

INT_F8 =(X^2*(2*X + 9))/6

>> INT_F8_def = int(F8(X),2,5)

INT_F8_def = 141/2

Example2:
Find the integral of [sine(S) + cos(S)] and evaluate the integration from –pi/2 to pi/2

Solution:

>> syms S

>> F9 = inline('sin(S)+cos(S)','S')

F9 =
     Inline function:
     F9(S) = sin(S)+cos(S)

>> INT_F9 = int(F9(S))

 INT_F9 = sin(S) - cos(S)

>> INT_F9_def = int(F9(S),-pi/2,pi/2)
 INT_F9_def = 2

5. Calculate the limit of a function:

To calculate limits using MATLAB the command line syntax should be:

limit(f(x),x,a)

      where "f(x)" is the function, and "x" is the independent variable of the function, and a the limit value or approach.

Example:

>> syms K

>> limit(sin(K)/K,K,0)
ans = 1

>> F10 = inline('sin(K)/K','K')

F10 =
     Inline function:
     F10(K) = sin(K)/K

>> limit(F10(K),K,0)
ans = 1

>> syms x

>> g = inline('(2*x^3+4*x^2+x-3)/(3*x^3-8)','x')
g =
     Inline function:
     g(x) = (2*x^3+4*x^2+x-3)/(3*x^3-8)

>> limit(g(x),x,Inf)

No comments:

Post a Comment