2
$\begingroup$
ClearAll@f;     
f[x_] := 2 Sin[x] - x - Log[x + 1]
Manipulate[Refresh[functions = Table[D[f[x], {x, n}], {n, 0, nMax, 1}];
  orders = 
   Table[D[f[x], {x, n}] // Inactivate // TraditionalForm // 
     ToString, {n, 0, nMax, 1}];
  labels = 
   MapThread[#1 <> " = " <> ToString[#2, TraditionalForm] &, {orders, 
     functions}];];
 Plot[functions, {x, 0, 3}, PlotLabels -> labels, ImageSize -> 700, 
  AspectRatio -> 1, 
  PlotLabel -> Row[{"f(x) = ", f[x]}]], {{nMax, 1, "Order"}, 1, 10, 1,
   PopupMenu}] 

The above code ran with errors. How to modify it? I want to modify the code to make it universal. You can input any function and draw the corresponding function image and derivative image.

$\endgroup$

2 Answers 2

6
$\begingroup$

A TogglerBar is more flexible for the display of multiple functions. With it, you can select any combinations of functions.

ClearAll@f;
f[x_] := 2 Sin[x] - x - Log[x + 1]

colors = ColorData[97] /@ Range[11];

Manipulate[
 funcs = Sort@funcs;
 functions = If[# == 0, HoldForm@f[x],
     HoldForm@D[f[x], {x, #}]] & /@ funcs;
 Plot[
  Evaluate[
   Tooltip[
      ReleaseHold@#,
      StringForm["``=``", Hold@#,
       Simplify@ReleaseHold@#]] & /@
    functions],
  {x, 0, 3},
  PlotStyle -> (colors[[funcs + 1]]),
  Frame -> True,
  ImageSize -> Medium,
  AspectRatio -> 1,
  PlotLabel ->
   StringForm["`` = ``", HoldForm@f[x], f[x]],
  PlotLegends -> functions],
 {{funcs, {0, 1, 2}, "Functions"},
  Prepend[Array[# -> HoldForm[Derivative[#][f][x]] &, 10],
   0 -> HoldForm[f[x]]],
  ControlType -> TogglerBar},
 SynchronousUpdating -> False,
 TrackedSymbols :> {funcs}]

enter image description here

$\endgroup$
0
$\begingroup$
ClearAll["`*"];
f[x_] := (x - 1) E^x - x^2
xr = -10 <= x <= 10
gmax = Maximize[{f[x], xr}, x]
gmin = Minimize[{f[x], xr}, x]
lmax = Solve[{D[f[x], x] == 0, D[f[x], {x, 2}] < 0, xr}, x]
lmin = Solve[{D[f[x], x] == 0, D[f[x], {x, 2}] > 0, xr}, x]
Manipulate[Refresh[functions = Table[D[f[x], {x, n}], {n, 0, nMax, 1}];
  orders = 
   Table[D[f[x], {x, n}] // Inactivate // TraditionalForm // 
     ToString, {n, 0, nMax, 1}];
  labels = 
   MapThread[#1 <> " = " <> ToString[#2, TraditionalForm] &, {orders, 
     functions}];];
 Plot[{functions}, {x, -2, 2}, 
  Epilog -> {{Darker@Green, AbsolutePointSize[20], 
     Point[{x, f[x]} /. gmax[[2]]], AbsolutePointSize[16], White, 
     Point[{x, f[x]} /. gmax[[2]]]}, {Darker@Red, 
     AbsolutePointSize[20], Point[{x, f[x]} /. gmin[[2]]], 
     AbsolutePointSize[16], White, Point[{x, f[x]} /. gmin[[2]]]}, 
    AbsolutePointSize[8], Green, Point[{x, f[x]} /. lmin], Red, 
    Point[{x, f[x]} /. lmax]}, PlotLabels -> labels, ImageSize -> 700,
   AspectRatio -> 1, 
  PlotLabel -> Row[{"f(x) = ", f[x]}]], {{nMax, 1, "Order"}, 1, 10, 1,
   PopupMenu}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.