Functions guide

🔗 To classpad homepage
🔗 To MATHS page
🔗 To personal homepage

We define a new function with the
Define
command.
    Define f(x,y,z,[...]) = [Function(x,y,z,[...])]
The result of
Function
is returned directly to the result line.
Example function:
cis(θ)

Firstly, a simple example.
You could use classpad-defined functions, such as
compToRect()
to convert from polar to rectangular form.
Alternatively, you could use Euler's formula, \(z = r\cdot e^{i\theta}\), but this leaves an opportunity for error if you forget the \(i\). Additionally, this form isn't taught in WA and most questions use \(\cis\).

So let's define \(\cis\) with a function!
Define cis(x)=cos(x)+i*sin(x)

Now whenever we use
cis
, we call this function.

To make this function useful, we should add it as a shift key in the
System
tab. (you can add shift-keys for anything, even text which doesn't evaluate to a defined function.)

Example function: De Moivre root finder

Let's find the roots of \(Z^5=32i\).
\begin{align} Z^5 &=32i\\ &=32\cis\left(\frac{\pi}{2}\right)\\ \implies Z_0&=\left[32\cis\left(\frac{\pi}{2}\right)\right]^{\frac{1}{5}}\\ Z_0&= 2\cis\left(\frac{\pi}{10}\right)\\ \implies Z_1 &= 2\cis\left(\frac{\pi}{10}+\frac{2\pi}{5}\right) = 2\cis\left(\frac{\pi}{2}\right)\\ Z_2 &= 2\cis\left(\frac{\pi}{10}+2\cdot\frac{2\pi}{5}\right) = 2\cis\left(\frac{9\pi}{10}\right)\\ Z_3 &= 2\cis\left(\frac{\pi}{10}-\frac{2\pi}{5}\right) = 2\cis\left(-\frac{3\pi}{10}\right)\\ Z_4 &= 2\cis\left(\frac{\pi}{10}-2\cdot\frac{2\pi}{5}\right) = 2\cis\left(-\frac{7\pi}{10}\right)\\ \end{align} This is a tedious task, with a lot of steps (adding for each power), which results in opportunity to make mistakes. This is a good opportunity/case to automate.
A function which does this tedious process automatically can help us verify if our working out is correct and will save time. (As always, we still need to show working for marks!)

This function,
zroot
, returns the De Moivre roots of some number, raised to the power
p
, equal to the complex number
z
\[Z^p = z\] Essentially, it returns the values of \(Z\), \(Z\in\mathbb{C} \).
More specifically, it returns a matrix containing these solutions in polar form on the left column and in rectangular form in the right column.


Define zsolve(p,z)=augment(listToMat(M*augment(seq(Cis(θ+n),n,0,π,d),seq(Cis(θ+n),n,-d,-π,-d))),listToMat(M*augment(seq(e^(i(θ+n)),n,0,π,d),seq(e^(i(θ+n)),n,-d,-π,-d))))|{θ=arg(z^(1/p)),M=(re(z)^2+im(z)^2)^(1/(2p)),d=2π/p}

Okay, this looks very complicated. It's a mess of brackets and functions. Let's re-format it so it's more readable.
Define zsolve(p,z)=augment(
    listToMat(
        M*augment(
            seq(Cis(θ+n),n,0,π,d),
            seq(Cis(θ+n),n,-d,-π,-d)
        ),
    ),
    listToMat(
        M*augment(
            seq(e^(i(θ+n)),n,0,π,d),
            seq(e^(i(θ+n)),n,-d,-π,-d)
        )
    )
)|{
    θ=arg(z^(1/p)),
    M=(re(z)^2+im(z)^2)^(1/(2p)),
    d=2Ï€/p
}



If we take a look at the code line-by-line, it's actually easy to understand. It appears big because we're limited in what we can store in variables, so there's a lot of duplicated code. (In this instance, we can't store lists in variables using the "with" notation, so we need to repeat some code).
*Sorry - I can't use highlight.js to make the code below colorful. The above image was generated by Carbon so I can't split it into a table.
Function walkthrough
Define zsolve(p,z)=augment(
//Defines our custom command. augment() joins two arrays together (the polar and rectangular array columns)
    listToMat(
//listToMat() converts a list (generated by seq()) to a matrix so it looks better.
        M*augment(
//We multiply the list (below) by M, The magnitude of the zeroth solution. M is multiplied to all values in the augmented (joined) list. augment() concatenates two lists.
            seq(Cis(θ+n),n,0,π,d),
//seq() is a function which generates a list based on a sequence. For example, seq(x^2,x,0,6,2) => {0,4,16,36}. Here we generate a list of polar coordinates with angles from 0 to π, with a constant θ added to n, a number which is generated from the seq() function. We increment each step by d, a constant. Also note the capital 'Cis' - this prevents it from being evaluated into rectangular form if cis() is defined. Cis() must be undefined to display in this form.
            seq(Cis(θ+n),n,-d,-π,-d)
//Similar to the last line, except we increment by -d, starting at -d (to prevent a 'double up' of Cis(θ+0)) and counting to -π
        ),
//Closing bracket for augment. We augment the sequence for positive angles and negative angles into one list.
    ),
//Closing bracket for listToMat
    listToMat(
//We repeat the process again. This time, we generate the rectangular form column.
        M*augment(
            seq(e^(i(θ+n)),n,0,π,d),
//Notice how we do not use Cis(), but Euler's formula to evaluate. We can use cis(θ+n) or cos(θ+n)+i*sin(θ+n), but Euler's formula is compact and always works (unlike cis() which might not be defined)
            seq(e^(i(θ+n)),n,-d,-π,-d)
        )
    )
)|{
//This pipe or bar character is documented as the "with" operator. For example, (2x)|{x=5} evaluates to 10. We store constants within the curly braces to make things neater to represent.
    θ=arg(z^(1/p)),
//This gets the phase of the zeroth solution. The zeroth solution is always the pth root of z.
    M=(re(z)^2+im(z)^2)^(1/(2p)),
//This is the magnitude. Simple stuff, we multiply the half by 1/p to skip a step (because the root is z^(1/p) => M^(1/p))
    d=2Ï€/p
//This is our angle difference. Remember - you need to state this for one mark.
}
//Don't forget to close your brackets!    tada!

For complex applications, a function isn't suitable. We need to use a program
When I develop a guide for programs (If I get around to it), the link will be here!
🔗 To classpad homepage
🔗 To MATHS page
🔗 To personal homepage