# How to optimize my code?

I use Mathematica to handle data.My trial as below:

Interpolation function：

coordinateList[coordinateB_, coordinateF_, n_Integer] := Block[
{coordinateListResult, \[CapitalDelta]},(*B-Begin,F-Final*)
\[CapitalDelta] = (coordinateF - coordinateB)/n;
coordinateListResult =
Range[coordinateB, coordinateF, \[CapitalDelta]]
]


Solving the center of a circle:

circleHeart[xyB_List, xyF_List, radius_] := Block[
{circleHeartResult},
circleHeartResult =
First@Simplify@
Solve[
{(xP - xyB[[1]])^2 + (yP - xyF[[1]])^2 == radius^2,
(xP - xyB[[2]])^2 + (yP - xyF[[2]])^2 == radius^2},
{xP, yP}]
]


Solving the angle:

angleSolve[xyO_List, xyP_List] := Block[
{angleSolveResult},
angleSolveResult =
N@ArcTan @@ (xyP - xyO)
]


Solving Data:

CircleJointsVaribles[L1_, L2_, xyB_List, xyF_List, radius_, tb_, tf_, n_Integer]     :=Block[
{LineJointsVariblesResult, xyO, xP, yP, px, py, A, c1, s1,
c2, s2, θ1, θ2, \[CapitalDelta]tList},
xyO = ({xP, yP} /. circleHeart[xyB, xyF, radius]);
A[t_] := InterpolatingPolynomial[
{{tb, angleSolve[xyO, xyB]},{tf,angleSolve[xyO, xyF]}}, t];
px[t_] := First@xyO + Cos[A[t]];
py[t_] := Last@xyO + Sin[A[t]];
c2[t_] := (-L1^2 - L2^2 + px[t]^2 + py[t]^2)/(2 L1 L2);
s2[t_] := -Sqrt[1 - (c2[t]^2) ];
c1[t_] := -((-L1 px[t] - c2[t] L2 px[t] - s2[t] L2 py[t])/(
px[t]^2 + py[t]^2));
s1[t_] := -((s2[t] L2 px[t] - L1 py[t] - c2[t] L2 py[t])/(
px[t]^2 + py[t]^2));
θ2[t_] := Simplify@ArcTan[c2[t], s2[t]];
θ1[t_] := Simplify@ArcTan[c1[t], s1[t]];
\[CapitalDelta]tList = coordinateList[tb, tf, n];
Column[
ListPlot[#1, ImageSize -> 350, AxesStyle -> Arrowheads[.03], AxesLabel -> #2] &@@@
{θ1/@\[CapitalDelta]tList, θ2/@ \[CapitalDelta]tList}),
Map[Style[#, 15, Red] &,
{{"t", "θ1(t)"},{"t", "θ2(t)"}},{2}]}],
Center]
]


However,when I use the function,it takes so much time.

CircleJointsVaribles[35, 20, {10, 30}, {30, 6}, 20, 0, 10, 20]

So my question is how to optimize it?

Edit：

CircleJointsVaribles[35., 20., {10., 30.}, {30., 6.}, 20., 0., 10, 20]

It gives the result immediately,However,gives the warning information:

Solve::ratnz: Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result. >>

-
Is it necessary to use exact arithmetic? For example try using 20.0 as the radius. By the way you did not include a definition for coordinateList so your code does not run. –  Simon Woods May 19 '14 at 9:35
From what I can see one major bottleneck is the use of InterpolatingPolynomial. The question is if you really need it. You might be able to obtain your own compilable version of it (as far as I know InterpolatingPolynomial is not compilable) and speed up your code that way. –  Wizard May 19 '14 at 9:43