Preface


This is a tutorial made solely for the purpose of education and it was designed for students taking Applied Math 0330. It is primarily for students who have very little experience or have never used Mathematica before and would like to learn more of the basics for this computer algebra system. As a friendly reminder, don't forget to clear variables in use and/or the kernel.

Finally, the commands in this tutorial are all written in bold black font, while Mathematica output is in regular fonts. This means that you can copy and paste all comamnds into Mathematica, change the parameters and run them. You, as the user, are free to use the scripts to your needs for learning how to use the Mathematica program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately.

Return to computing page for the first course APMA0330
Return to computing page for the second course APMA0340
Return to Mathematica tutorial for the second course APMA0330
Return to Mathematica tutorial for the first course APMA0340
Return to the main page for the course APMA0340
Return to the main page for the course APMA0330
Return to Part V of the course APMA0330

Recurrences


We can define the Fibonacci recurrence (which is a second order difference equation \( F_{n+2} = F_{n+1} + F_n , \quad F_0 =1, \ F_1 =1 \) ) as

fib = (If[#1 == 1 || #1 == 2, 1, #0[#1 - 1] + #0[#1 - 2]]) &
f[n_]:=Union @@ NestList[{{0,1},{1,1}}.# &, {1, 1}, n]
fibonacciList[n_] := Module[{x = 0}, NestList[x + (x = #) &, 1, n - 1]]
fibonacciList[10]
{1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
Next example from the Mathematica docs shows one way down values are more expressive (dynamic programming):
fib[n_] :=
Module[{f},
f[1] = f[2] = 1; f[i_] := f[i] = f[i - 1] + f[i - 2];
f[n] ]
fibonacciSequence[n_] :=
Module[{fPrev = 0, fNext = 1, i = 0},
While[i++ < n, {fPrev, fNext} = {fNext, fPrev + fNext}];
fNext]
Another efficient way to define the Fibonacci recurrence is to use the explicit formula (which is not available for most other nonlinear recurrences): `
\[ f_n = \frac{1}{\sqrt{5}} \left[ \left( \frac{1 + \sqrt{5}}{2} \right)^n - \left( \frac{1 - \sqrt{5}}{2} \right)^n \right] , \qquad n=0,1,2,\ldots . \]
fibos[n_] := RootReduce@(((1 + Sqrt[5])/2)^n - ((1 - Sqrt[5])/2)^n)/Sqrt[5]
Mathematica also has a special command to evalaute n-th Fibonacci number: Fibonacci[n] .

Tail recursive Fibonacci sequence generator

fiboSequence[n_, a_, b_] := fiboSequence[n - 1, b, Sow[a] + b]
fiboSequence[0, a_, b_] := Sow[a]
fiboSequence[n_] := Reap[fiboSequence[n, 0, 1]][[2, 1]]

fiboSequence[15]
{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}
Pattern matching Fibonacci sequence generator
fiboSequence2[n_] :=
Quiet@ReplaceRepeated[{0, 1}, {x___, a_, b_} :> {x, a, b, a + b}, MaxIterations -> n - 1]

fiboSequence2[15]
{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}
Matrix method:
fn[n_]:=First[MatrixPower[{{1,1},{1,0},n-1].{1,0}]
Another useful way uses LinearRecurrence:
LinearRecurrence[{1, 1}, {1, 1}, 10]

 

Plotting Solutions to ODEs

Direction Fields

Separable Equations

Equations Reducible to the Separable Equations

Equations with Linear Fractions

Exact Equations

Integrating Factors

Linear Equations

RC circuits

Bernoulli Equations

Riccati Equations

Existence and Uniqueness

Qualitative Analysis

Bifurcations

Orthogonal Trajectories

Population Models

Applications

 

Return to Mathematica page

Return to the main page (APMA0330)
Return to the Part 1 (Plotting)
Return to the Part 2 (First Order ODEs)
Return to the Part 3 (Numerical Methods)
Return to the Part 4 (Second and Higher Order ODEs)
Return to the Part 5 (Series and Recurrences)
Return to the Part 6 (Laplace Transform)
Return to the Part 7 (Boundary Value Problems)