Functions recursion using graphs in MATLAB

Spread the word!

This is the MATLAB implementation of the idea explained here by using the useful graph functions already given in MATLAB. Here an additional feature is given, which is the possibility to stop to the first found path (also shortest) from the starting to the target number, or to continue and find all possible paths.

A short mathematical background in the comments explain what main formulas were used.

The main code:

Example of a plotted tree with n=2,depth=4, input=10, stopIfMatched=0:

graph

Possible developments & applications:

  • Given a limited set of functions and a few input / output pairs a common path between inputs and outputs could be found (if exists). In this case all inputs would be part of the domain and all outputs of the co-domain of a single function made by the composition of the user defined functions.
  • A brute force way to solve Rubik’s cube from any initial configuration could be implemented and the shortest set of moves can be given as output.
  • The script can be extended to symbolic functions. Let’s suppose we have a set of possible operations (like integration, derivative, elevation to some power, addition of constant, multiplication by constant). The question “How to turn a given function f(x) in another given function g(x) just by combining the given operations” could be answered.
  • Something (chess piece, robots, …) has a limited and well defined set of moves and need to go from point A to point B.

Be the first to comment

Leave a Reply

Your email address will not be published.


*