2
$\begingroup$

I need a function fSum that computes the angular sum for some expression, like this:

fSum[3 Sin[2 x], 0, 2 π]

Or from a predefined function, like this:

f[x] = 2 Sin[3 x];

fSum[f, 0, 2 π]

Or plots it like this (exact syntax will probably have to be different here):

Plot[fSum[a Sin[x]], {a, 0, 1}]

I have this stub for fSum:

fSum[expr_, from_ ?NumericQ, to_ ?NumericQ] := Module[{f},
   f = ???? (* How to do this? *)
   Return[NIntegrate[Abs[D[ArcTan[f'[x]], x]], {x, from, to}]];
   ]];

I have asked about something similar before and have then received some helpful answers that solved the particular problem at the time. But now I need to make the fSum function more general so that I also can plot fSum relative to some plot variables in the expression (or function) passed to it. The only thing I can get to work at ???? in the stub is a hardcoded expression, which is very far from what I need. I have searched this forum and have tried many different solutions, but can not make them work in this case. It would be great if the expr parameter can be both an expression, function, and a string containing the inputform text for the expression.

$\endgroup$
2

1 Answer 1

4
$\begingroup$
Clear[fSum];

fSum[str_String, rest__] := fSum[ToExpression@str, rest]

fSum[f_, {from_, to_}] := NIntegrate[Abs[D[ArcTan[f'[x]], x]], {x, from, to}];

fSum[expr_, {var_, from_, to_}] := With[{f = Function @@ {var, expr}},
   fSum[f, {from, to}]];

fSum[expr_, from_, to_] := 
 With[{symbol = 
    First@Union@Cases[expr, _Symbol?(Context[#] =!= "System`" &), Infinity]},
  fSum[expr, {symbol, from, to}]]

Usage:

Clear[f, x]
f[x_] = 3 Sin[2 x];

fSum[f, {0, 2 Pi}]
(* 11.2452 *)

fSum[3 Sin[2 x], {x, 0, 2 π}]
(* 11.2452 *)

fSum[3 Sin[2 x], 0, 2 Pi]
(* 11.2452 *)

fSum["f", {0, 2 Pi}]
(* 11.2452 *)

fSum["3 Sin[2 x]", {x, 0, 2 Pi}]
(* 11.2452 *)

fSum["3 Sin[2 x]", 0, 2 Pi]
(* 11.2452 *)

Plot[fSum[a Sin[x], 0, 2 Pi], {a, 0, 1}]

enter image description here

$\endgroup$
10
  • $\begingroup$ Wow, works perfectly and very elegant! Thank you very much xzczd. I will study the last overload carefully, and hopefully I will learn a little about this strange Wolfram language. I have been programming in many different programming languages for more than 40 years now, but never has the learning curve been this steep. $\endgroup$
    – Mikl
    Commented Mar 24 at 8:29
  • $\begingroup$ @Mikl If you have difficulty in understanding the code, feel free to continue to ask in the comment :) . $\endgroup$
    – xzczd
    Commented Mar 24 at 9:01
  • $\begingroup$ Yeah, can you please explain what is happening in the With[{symbol = ....] in the last overload. Looks very similar to sanskrit to me. $\endgroup$
    – Mikl
    Commented Mar 24 at 9:14
  • $\begingroup$ @Mikl In last overload, we're detecting the independent variable automatically. Usually the independent variable in expression is a Symbol that's not in "System`" Context, so we use the pattern _Symbol?(Context[#] =!= "System`" &) to extract the symbol. ?, _, &, etc. are just some shorthand, you can check their FullForm with e.g. Cases[expr, _Symbol?(Context[#] =!= "System`" &), Infinity] // Hold // FullForm and check their document by pressing F1. (Actually this isn't necessary, because almost all of the shorthand can be directly found in document by pressing F1. ) $\endgroup$
    – xzczd
    Commented Mar 24 at 9:26
  • $\begingroup$ Okay, thank you. I will read up on it. $\endgroup$
    – Mikl
    Commented Mar 24 at 9:54

Your Answer

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

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