DMelt:JMathlab/5 Calculus

From HandWiki
Limitted access. First login to DataMelt if you are a full DataMelt member. Then login to HandWiki as a user.



diff(function,x) differentiates function with respect to the symbolic variable x. The main variable of function is used if x is not provided. Functions defined by user programs can often be handled as well.

>> syms a,x
>> diff(a*x^3)
ans = 3*a*x^2
>> diff(a*x^3,a)
ans = x^3
>> diff(3*sqrt(exp(x)+2),x)
ans = 1.5*exp(x)/sqrt(exp(x)+2)
>> diff(sin(x))      % no variable specified
ans = 1              % use z=sin(x) as variable
>> diff(sin(x),x)    % more reasonable
ans = cos(x)
>> function y=ttwo(x) y=2*x; end
>> diff(ttwo(sin(x)),x)
ans = 2*cos(x)


taylor(function, x, x0, n) calculates the n-th Taylorpolynomial in the symbolic variable x at the point x0.

>> syms x
>> taylor(log(x),x,1,1)
ans = x-1
>> rat( taylor(exp(x),x,0,6))
ans = 1/720*x^6+1/120*x^5+1/24*x^4+1/6*x^3+1/2*x^2+x+1
>> float( taylor(x^3*sin(2*x+pi/4),x,pi/8,2))
ans = 1.057*x^2-0.36751*x+4.1881E-2

Indefinite Integral

integrate(function, x) integrates expression function with respect to the symbolic variable x. Jasymca uses the following strategy: Integrals of builtin-functions and all polynomials are provided:

>> syms x
>> integrate(x^2+x-3,x)
ans = 0.33333*x^3+0.5*x^2-3*x
>> integrate(sin(x),x)
ans = -cos(x)

If function is rational (i.e. quotient of two polynomials, whose coefficients do not depend on x)) we use the standard approach: Separate a polynomial part, then separate a square free part using Horowitz' [11] method, and finally integrate the rest using partial fractions. The final terms are collected to avoid complex expressions.

>> syms x
>> y=(x^3+2*x^2-x+1)/((x+i)*(x-i)*(x+3))       
y = (x^3+2*x^2-x+1)/(x^3+3*x^2+x+3)
>> integrate(y,x)
ans = -1/4*log(x^2+1)+(-1/2*log(x+3)+(-1/2*atan(x)+x))
>> diff(ans,x)           % control
ans = (x^3+2*x^2-x+1)/(x^3+3*x^2+x+3)

Expressions of type $g(f(x))\cdot f'(x)$ and $\frac{f'(x)}{f(x)}$ are detected:

>> syms x
>> integrate(x*exp(-2*x^2),x)                  
ans = -0.25*exp(-2*x^2)
>> integrate(exp(x)/(3+exp(x)),x)               
ans = log(exp(x)+3)

Substitutions of type $(a\cdot x+b)$ are applied:

>> syms x
>> integrate(3*sin(2*x-4),x)                    
ans = -1.5*cos(2*x-4)

Products $polynomial(x)\cdot f(x)$ are fed through partial integration. This solves all cases where $f$ is one of exp, sin , cos , log , atan.

>> syms x
>> integrate(x^3*exp(-2*x),x)                  
ans = (-0.5*x^3-0.75*x^2-0.75*x-0.375)*exp(-2*x)
>> integrate(x^2*log(x),x)                    
ans = 0.33333*x^3*log(x)-0.11111*x^3

All trig and exp-functions are normalized. This solves any expression, which is the product of any number and any type of exponentials and trigonometric functions, and some cases of rational expressions of trig- and exp-functions

>> syms x
>> integrate(sin(x)*cos(3*x)^2,x)         
ans = -3.5714E-2*cos(7*x)+(5.0E-2*cos(5*x)-0.5*cos(x))
>> integrate(1/(sin(3*x)+1),x)             
ans = -2/3*cos(3/2*x)/(sin(3/2*x)+cos(3/2*x))

The special case $\sqrt{ax^2+bx+c}$ is implemented:

>> syms x
>> integrate(sqrt(x^2-1),x)                     
ans = 0.5*x*sqrt(x^2-1)-0.5*log(2*sqrt(x^2-1)+2*x)

Clever substitutions may be supplied manually through subst(). If all fails, integrate numerically using quad or romberg. The symbolic variable may be omitted if it is the main variable of expression. Integrations can be quickly verified using diff() on the result.

Numerical Integration

Two routines are supplied for numerical integration, which are both not very sophisticated. quad('expression',ll,ul) is modelled after the Octave/Matlab integration function, but much simpler. Simpson's method is applied with a fixed number of nodes. This function uses the "eval"-method rather than symbolic variables. The function has to be supplied as quoted string, and must be compatible with vector arguments. Finally, the variable name must be x. romberg uses symbolic function definitions, and a symbolic variable has to be supplied. The maximum number of iterations is set by the variable rombergit (default $11$) and accuracy by rombergtol (default: $10^{-4}$).

>> quad('exp(-x.^2)',0,5)
s = 0.88623
>> syms x
>> romberg(exp(-x^2),x,0,5)
ans = 0.88623

Differential Equations

ode(expression,y,x) solves the linear first-order differential equation $y'=f(x)\cdot y + g(x)$. expression is the complete right-hand-side of the equation, x and y are symbolic variables. Free constants in the solution are marked C.

>> ode(x,y,x)
ans = 0.5*x^2+C
>> syms k
>> ode(-k*y,y,x)
ans = C*exp(-k*x)
>> ode(y*tan(x)+cos(x),y,x)
ans = (0.5*cos(x)*sin(x)+(0.5*x+C))/cos(x)