iconEuler Examples

Demonstration of the function "root"

by R. Grothmann

"root" is an equation solver for interactive use. It can solve an
equation for one variable, and will assign the result to this
variable.

We first take a simple example.
>longformat; a:=2; x:=1; root("x^2-a",x)
1.41421356237
The solution for x is assigned to the global variable
x.
>x
1.41421356237
>sqrt(2)
1.41421356237
We could also solve for the variable "a" starting with a different
value of the variable "a".
>a:=3; root("x^2-a",a)
2
For a more complex example, we take the computation of interest
rates for a loan (or a savings account). 

Assume you get K (K>0) at time 0 and pay P (P<0) at each period,
starting from period 1 to period n-1. You then have a final depth
F (F<0). What is the interest rate?
>ex &= K*f^n+(f^(n-i0+1)-1)/(f-1)*f^i1*P+F
                   i1   n - i0 + 1
                  f   (f           - 1) P    n
                  ----------------------- + f  K + F
                           f - 1

After we set up the expression, we initialize the variables with
values. We take an approximation for f (8 %). This time we have
to pay 1000 each month. We are done (F=0) after 120 month.

We set up starting values for all variables in the formula.
>K:=100000; n:=120; f:=1.08^(1/12); P:=-1000; i0:=1; i1:=0; F:=0;
Then we solve for the correct interest rate "f".
>root(ex,f)
1.00311418195
To compute the effective interest rate per year, we must
take f^12 and compute the interest rate in %.
>(f^12-1)*100
3.80169510507
Assume we stop paying after 119 months, and the interest rate
is 8%. How much would be left after 120 month?
>f:=1.08^(1/12); i1:=1; "Dept left:"|print(-root(ex,F),2)
Dept left:  34609.30
How long would it take to pay the loan at 8%?
>F:=0; i1:=0; "Payed after "|print(root(ex,n)/12,1,0)|" years"
Payed after 13.4 years
>

Examples Homepage