Escolar Documentos
Profissional Documentos
Cultura Documentos
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 also plot with specific range values by passing them as an array.
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.
## 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
We can plot the trajectory of a ball thrown at `30mi/hour` straight into the air.
### Taxes
contribution(x) = x - takehome(x)
### 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).
We can see that the length of the loan and the interest rate combine in interesting
ways to determine the monthly payment.
That `15` year loan is right on the knee of the curve, perhaps it's not a great
idea.
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:
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.