I am new to Mathematica and was experimenting with a simply problem of plotting a tangent to a curve and being able to move the point at which the tangent is drawn using Manipulate. When I run the following, the cell flickers a lot and it also seems to consume a lot of CPU? Is there a way to avoid this?
Manipulate[
Module[{f, x, y, dx, dy},
dx = dy = 1;
f[x_] := x^2;
tangent[x_, y_] := {{x - dx, y - f'[x] dy}, {x + dx, y + f'[x] dy}};
g = Graphics[Line @ tangent[x0, f[x0]], PlotRange -> {{-5, 5}, {-5, 5}}];
p = Plot[f[x], {x, -5, 5}];
Show[p, g]],
{{x0, 2}, -4, 4}]
Moduleinside aManipulateis not a good idea, nor is evaluating the derivative of a simple function over and over. – m_goldberg Feb 17 '13 at 17:23Manipulate, but taking the derivative ofx^2many times when it could done once is not good. – m_goldberg Feb 17 '13 at 17:33porg, Mathematica will update the Manipulate (i.e., reevaluate it). You can avoid it by rewriting the code, usingTrackedSymbols :> {x}, or localizingp,g,tangentinside the Module. – Michael E2 Feb 17 '13 at 17:48