Here are some additional Matlab example programs for the sections of CPS118 taught by Ilkka Kokkarinen, to be demonstrated in suitable places during the course. I will add more links on this page as I create more examples.

Here are the slides for the first lecture.

All files in one tar archive: matlab.tar

- apportion_congress.m Apportion congressional seats by Huntington-Hill method.
- binary_string.m Convert an integer to a string of binary digits.
- boid_dance.m A variation of a Boids system where the each boid tries to avoid its randomly chosen enemy and move closer to its randomly chosen friend.
- cellular.m Compute the evolution of an elementary cellular automaton with respect to the given 8-bit rule.
- close_enough.m Check if all values in array are “close enough” to each other.
- collatz.m Count the steps needed to reach 1 in Collatz 3n+1 progression.
- days_in_month.m Determine the number of days in given month.
- days_in_month_switch.m Same as previous, but uses the Matlab switch construct.
- dead_sea_scrolls.m Use Matlab’s numerical and symbolic equation solvers to solve a problem of radioactive decay.
- dejong.m Plot out the Dejong attractor for the desired parameter values.
- diff_demo.m Demonstrate the numerical and symbolic solving of differential equations in Matlab.
- file_demo.m Showcase the file input and output mechanisms of Matlab.
- first_past_post.m Determine the winner of a single-winner first-past-post election.
- fizzbuzz.m The Internet-famous fizzbuzz job interview chestnut.
- gol.m Compute the next step in Conway’s Game of Life.
- hanoi.m Recursive solution to the classic Towers of Hanoi puzzle.
- heron.m Heron’s square root algorithm, another blast from the ancients.
- is_leap_year.m Practice writing if-else ladders to determine if the parameter year is a leap year.
- knapsack.m Recursive solution to the famous NP-hard knapsack problem.
- linalg_demo.m Demonstrate Matlab’s linear algebra equation solvers.
- loops_demo.m Play around with vectors with some classic programming exercises.
- lorenz_plot.m Use Matlab’s ode45 differential equation solver to plot the evolution of a Lorenz System for given parameters.
- mandelbrot.m Calculate the famous Mandelbrot set fractal image.
- monte_carlo_pi.m Demonstrate the idea behind the Monte Carlo approximation methods with a simple approximation of pi.
- multifig.m Showcase the concept of figure and object handles to juggle several figures simultaneously.
- my_accum.m Loop example to accumulate the values in the given array.
- my_isprime.m Loop example to determine if the given integer is a prime.
- n_bodies.m Solve and plot the n-body problem under Newtonian gravity as a set of numerical differential equations.
- number_guess.m The simple number guessing game to demonstrate Matlab loops and input/output facilities.
- numerics_demo.m Demonstrate the numerical integration, root finding and minimization functions of Matlab.
- one_time_pad.m Encrypt and decrypt a text string using one-time pad encryption to demonstrate bitwise logical operations and type conversions between vectors.
- plots_3d_demo.m Showcase the 3D plotting functions of Matlab.
- polydemo.m Demonstrate the polynomial evaluation, integration, differentiation, root finding and least squares interpolation functions of Matlab.
- predator_prey.m Use Matlab’s ode45 differential equation solver to plot the evolution of a Lotka-Volterra predator-prey model of two populations for given parameters.
- ramanujan_plot.m Solve numerically the nested radicals puzzle originally stated by Ramanujan.
- sierpinski.m Calculate the Sierpinski triangle fractal recursively.
- sound_demo.m Create a sound as a Matlab vector and play it.
- spherefill.m Perform fractal planar filling using random spheres that do not intersect.
- stat_demo.m Demonstrate the numerical statistical functions of Matlab.
- subset_sum.m Recursive solution to the famous NP-hard subset sum problem.
- sum_of_digits.m Calculate the sum of digits of the parameter integer in the given base.
- sym_demo.m Demonstrate the manipulation of symbolic formulas along with their mathematics in Matlab.