Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Algorithms

1.3.Algorithms

An idealized mathematical problem f(x)f(x) can usually only be approximated using a finite number of steps in finite precision. A complete set of instructions for transforming data into a result is called an algorithm. In most cases it is reasonable to represent an algorithm by another mathematical function, denoted here by f~(x)\tilde{f}(x).

Even simple problems can be associated with multiple algorithms.

1.3.1Algorithms as code

Descriptions of algorithms may be presented as a mixture of mathematics, words, and computer-style instructions called pseudocode, which varies in syntax and level of formality. In this book we use pseudocode to explain the outline of an algorithm, but the specifics are usually presented as working code.

Of all the desirable traits of code, we emphasize clarity the most after correctness. We do not represent our programs as always being the shortest, fastest, or most elegant. Our primary goal is to illustrate and complement the mathematical underpinnings, while occasionally pointing out key implementation details.

As our first example, Function 1.3.1 implements an algorithm that applies Horner’s algorithm to a general polynomial, using the identity

p(x)=c1+c2x++cnxn1=(((cnx+cn1)x+cn2)x++c2)x+c1.\begin{split} p(x) &= c_1 + c_2 x + \cdots + c_n x^{n-1} \\ &= \Bigl( \bigl( (c_n x + c_{n-1}) x + c_{n-2} \bigr) x + \cdots +c_{2} \Bigr)x + c_{1}. \end{split}

1.3.2Writing your own functions

Any collection of statements organized around solving a type of problem should probably be wrapped in a function. One clue is that if you find yourself copying and pasting code, perhaps with small changes in each instance, you should probably be writing a function instead.

Functions can be defined in text files with the extension .m, at the command line, or in Live Scripts.

As seen in Function 1.3.1, one way to start a function definition is with the function keyword, followed one or more output arguments in brackets, the function name, and the input arguments in parentheses. For example, to represent the mathematical function esinxe^{\sin x}, we could use the following in a file called myfun.m:

function [y] = myfun(x)
    s = sin(x);
    y = exp(s);
end

Whatever value is assigned to y when the function terminates will be returned as the output of the function.

For a function with a short definition like the one above, there is a more compact syntax to do the same thing:

myfun = @(x) exp(sin(x));

The syntax on the right of the = above defines an anonymous function (called a lambda function in computer science), which can be used in place without giving it a name as we did here. We’ll have examples of doing this later on.

As in most languages, input arguments and variables defined within a function have scope limited to the function itself. However, they can access values defined within an enclosing scope, with those values being locked in at the time of creation. For instance:

c = 1;
mycfun = @(x) exp(c*sin(x));
mycfun(3)   % returns exp(1*sin(3))
c = 2;  
mycfun(3)   % also returns exp(1*sin(3))
mycfun = @(x) exp(c*sin(x));   % redefines mycfun
mycfun(3)   % now returns exp(2*sin(3))

There’s a lot more to be said about functions, but this is enough to get started.

1.3.3Exercises