Вы находитесь на странице: 1из 4

# Calca Plots

Calca can plot functions! It's as simple as calling the `plot` function.

plot(x * sin(40x))

By default, the function is plotted in the range `-1..1` and is stretched to fill
the plot window. You can specify a different range as the last argument of `plot`.

plot(x^(sin(x)), 1..22)

Plots are lightly shaded in the area under the curve to show the integral.

You can plot multiple functions in one plot call:

plot(c*9/5 + 32, c, -50..50)

plot(sin(x*pi), sin(x*pi)^2, sin(x*pi)^3)

By default, plots are shown for values in the range `-1..1`.

The range can also have units.

plot(x, x in cad, x in eur, x in gbp, $1..100)

plot(rate * 8hour * 5 * 50 in $, $10..50/hour)

You can also plot arrays of data:

plot([1, 2, 2, 4, 1, -1, 0])

## Explicit Range Values

You can also plot with specific range values by passing them as an array.

plot(sin(x), [0, pi/4, 2pi/4, 3pi/4, pi])

You can plot up to about 16,000 such points.

If you plot arrays of data with explicit x coordinates then you are plotting 2D
parametric data:

m = 8
xs = map(cos(n*2pi/m), 0..m)
ys = map(sin(n*2pi/m), 0..m)

plot(ys, xs)

## Range Name

The first undefined variable that appears in a function is used as the range
variable. This means that the order in which variables are written matters.

plot(x*y, 2y*x)

Functions that take different parameters can still be plotted at the same time.
f(x) = 1/x
g(z) = 400z^3

plot(f, g)

## Interaction

You can **trace** along the plot to show the input and value of the function. The
range value is shown as the top number; and the bold bottom number is the value of
the function. The left handed derivative at that point is shown on the third line
(marked with Δ.

You can **zoom** in and out using a two finger pinch gesture. Double tapping will
also zoom in to that location.

You can **pan** by dragging anywhere in the window.

## Export

The plot can be exported as an [SVG][] file that is ready to be posted on the web,
added to a document, or integrated into a larger graphical work.

The plot data can also be exported as a [CSV][] file for import into *Numbers* or
*Excel* or any other data processing app.

[svg]: http://en.wikipedia.org/wiki/Scalable_Vector_Graphics
[csv]: http://en.wikipedia.org/wiki/Comma-separated_values

## Examples

### Thrown Ball

We can plot the trajectory of a ball thrown at `30mi/hour` straight into the air.

height of ball(t) = -9.8m/s^2 * t^2 + 30mi/hour * t in ft

plot(height of ball, 0, 0..1.5s)

### Taxes

tax rate(x) = if x > 87,000 then 28% else

if x > 36,000 then 25% else
if x > 9,000 then 15% else

takehome(x) = x - x*tax rate(x)

contribution(x) = x - takehome(x)

plot(takehome(earn), contribution(earn), earn, 0..100,000)

### Mortgage

Let's consider how long a period we should take a mortgage out for. I want to know
what the difference between a `15` year loan and a `30` year loan is.

loan = $200,000

We can start by defining a function that calculates the monthly payment given a
yearly interest rate and the number of years that we want the loan for.

payment(rate, period) =
let c = rate / 12 in
let n = period * 12 in
loan * (c*(1+c)^n) / ((1+c)^n - 1)

Now lets plot different interest rates for different loan periods: `5..30` years.
We're going to use map here because we want to plot specific values (not have a
continuous plot).

years = map(x, 5..30)

plot(payment(0.05, x), payment(0.10, x), payment(0.01, x), years)

We can see that the length of the loan and the interest rate combine in interesting
ways to determine the monthly payment.

What kind of interest are we paying?

interest(rate, years) = payment(rate, years) * 12years - loan

plot(interest(0.05, x), interest(0.10, x), interest(0.01, x), years)

That `15` year loan is right on the knee of the curve, perhaps it's not a great

### Taylor Series Approximation

Imagine I have a crazy function:

f(x) = sin(x*pi/2)^2

range = -0.1..1.4

plot(f, range)

We can find polynomial curves that fit this function using the `taylor` function.
We need to specify a location around which to base the fitting and the degree of
the final curve:

fit location = 0.5

t1 = taylor(f, x=fit location, 1)

=> 1.5708 x - 0.2854
t3 = taylor(f, x=fit location, 3)
=> 1.5708 x + 4.44089e-16*(x - 0.5)^2 - 2.58386*(x - 0.5)^3 - 0.2854
t5 = taylor(f, x=fit location, 5)

plot(f, t1, t3, t5, range)

We can see that the fifth order taylor (`t5`) very closely matches the original
function. Try zooming in to see the differences. Or, we can plot the difference:
plot(|f(x) - t5(x)|, range)

We can see that it's very accurate around `location` but is less accurate at the
extremes. Try changing `location` to see how the curves change.