Contenuto principale

solveRecurrence

Solve recurrence relations

Since R2026a

Description

sol = solveRecurrence(eqn,f) solves the symbolic recurrence relations in eqn for the functions f.

example

sol = solveRecurrence(eqn,f,cond) solves the recurrence relations with the initial conditions cond.

example

Examples

collapse all

Find the solution of the recurrence relation f(n)=nf(n-1) for the gamma function.

Create a symbolic function f(n) and a symbolic equation eqn to represent the recurrence relation.

syms f(n)
eqn = f(n) == n*f(n-1);

Find the solution. The result is the gamma function with a constant multiplier.

sol = solveRecurrence(eqn,f)
sol = C1Γgamma(n+1)

Create a symbolic function f(n) and a symbolic equation eqn to represent the recurrence relation for the Fibonacci and Lucas sequences.

syms f(n)
eqn = f(n) == f(n-1) + f(n-2)
eqn = f(n)=f(n-1)+f(n-2)

Specify the initial conditions for the Fibonacci sequence.

cond = [f(0) == 0, f(1) == 1]
cond = (f(0)=0f(1)=1)

Solve the recurrence relation using these initial conditions.

sol = solveRecurrence(eqn,f,cond)
sol = 

552+12n5-512-52n5

Find the 10th Fibonacci number.

sol10 = simplify(subs(sol,n,10))
sol10 = 55

Next, specify the initial conditions for the Lucas sequence.

cond = [f(0) == 2, f(1) == 1]
cond = (f(0)=2f(1)=1)

Solve the recurrence relation using these initial conditions.

sol = solveRecurrence(eqn,f,cond)
sol = 

12-52n+52+12n

Find the 10th Lucas number.

sol10 = simplify(subs(sol,n,10))
sol10 = 123

Create a system of linear recurrence relations.

syms a(n) b(n)
eqn = [a(n+1) == a(n) + 2*b(n); ...
       b(n+1) == 3*a(n) + b(n)]
eqn = 

(a(n+1)=a(n)+2b(n)b(n+1)=3a(n)+b(n))

Specify the initial conditions for the functions a(0) and b(0).

cond = [a(0) == 2; b(0) == 3]
cond = 

(a(0)=2b(0)=3)

Solve the system of recurrence relations using these initial conditions.

sol = solveRecurrence(eqn,[a b],cond)
sol = 

(σ2+σ4-σ1+σ3σ2+3σ42-σ1+3σ32)where  σ1=6σ32  σ2=6σ42  σ3=1-6n  σ4=6+1n

Check if the solutions for a(n) and b(n) satisfy the recurrence relations. Substitute [a; b] with sol, and use isAlways to determine if both sides of the recurrence relations are equal.

tf = isAlways(subs(eqn,[a; b],sol))
tf = 2×1 logical array

   1
   1

Check if the solutions also satisfy the initial conditions.

tf = isAlways(subs(cond,[a; b],sol))
tf = 2×1 logical array

   1
   1

Create a recurrence relation for the Bessel functions.

syms J(n) z
eqn = J(n+1) == 2*n/z*J(n) - J(n-1)
eqn = 

J(n+1)=2nJ(n)z-J(n-1)

Specify the initial conditions using the Bessel function of the first kind.

cond = [J(0) == besselj(0,z); J(1) == besselj(1,z)]
cond = 

(J(0)=Jbesselj0(z)J(1)=Jbesselj1(z))

solveRecurrence cannot always solve recurrence relations with variable coefficients. In this case, the function returns an unevaluated call.

sol = solveRecurrence(eqn,J,cond)
sol = 

solveRecurrence(J(n+1)=2nJ(n)z-J(n-1),J(n),[J(0)=Jbesselj0(z),J(1)=Jbesselj1(z)])

However, if you substitute a specific value for the indexing parameter into the solution, the solver might be able to return an analytic solution. For example, substitute n=4. The analytic solution is in terms of the variable z and the initial conditions J0(z) and J1(z).

soln4 = subs(sol,n,4)
soln4 = 

-8z2Jbesselj1(z)-z3Jbesselj0(z)+24zJbesselj0(z)-48Jbesselj1(z)z3

Check if this solution is equal to the Bessel function of the first kind of order 4.

tf = isAlways(soln4 == besselj(4,z))
tf = logical
   1

Input Arguments

collapse all

Recurrence relations to solve, specified as a symbolic equation or vector of symbolic equations. The symbolic equations must contain symbolic functions that depend on a single indexing parameter, or the function argument must have only one independent variable.

The solveRecurrence function consistently solves symbolic equations that define a system of linear recurrence relations with constant coefficients. In some cases, solveRecurrence can also solve linear recurrence relations with nonconstant coefficients or a system of nonlinear recurrence relations.

Example: syms f(n); eqn = f(n) == 3*f(n-1) - f(n-2)

Example: syms f(n); eqn = f(n+1)*f(n) == 2*f(n) + 1

Functions to solve for, specified as a symbolic function or vector of symbolic functions.

Initial conditions, specified as a symbolic equation or vector of symbolic equations.

Output Arguments

collapse all

Solution of the recurrence relations, returned as a symbolic expression or vector of symbolic expressions.

If the solveRecurrence function cannot solve the recurrence relations, then it returns an unevaluated call.

More About

collapse all

Version History

Introduced in R2026a

See Also

Functions