0
$\begingroup$

I am very new to Mathematica, and I searched the documentation and google now for quite some time.

Let's say, I want to implement a mathematica function, that does the following mapping: $f(t)\mapsto \int_0^t f(s)\ \mathrm{d}s+f'(t)$. ($f$ is assumed to be differentiable and integrable.)

Edit: It should also work for $\mathbb{R}\longrightarrow\mathbb{R}^n$ functions.

How would you do that? It should be a function like

IntAndDiff[f_]=Integrate[f,{s,0,t}]+D[f,t]

which does of course not work. How am I able to "access" the variable of $f$ in this case, so that I can define the integration and differentiation accordingly?

Edit: I also want this function to behave like an operator, so the output should be a $\mathbb{R}\longrightarrow\mathbb{R}^n$ function again. I.e. I want to be able to apply this (and other such operators) on the result again...

$\endgroup$
2
  • $\begingroup$ ClearAll[IntAndDiff];IntAndDiff[f_] = Integrate[f@s, {s, 0, t}] + D[f@t, t]? $\endgroup$
    – kglr
    Dec 31, 2020 at 0:07
  • $\begingroup$ As @kglr suggested, except use SetDelayed, i.e., IntAndDiff[f_] := Integrate[f[s], {s, 0, t}] + D[f[t], t] Examples: IntAndDiff /@ {Sin, Sin[#] &, Cos, Cos[#] &, Sqrt, Sqrt[#] &, #^2 + 2 # - 3 &} $\endgroup$
    – Bob Hanlon
    Dec 31, 2020 at 0:35

3 Answers 3

1
$\begingroup$

$f(t)\mapsto \int_0^t f(s)\ \mathrm{d}s+f'(t)$

A little analysis:

Input is actually a function $f(t)$, the output is also a function $F(t)=\int_0^t f(s)\ \mathrm{d}s+f'(t)$.

So an easy way is to introduce $t$

F[f_, t_]:= Integrate[f[s], {s, 0, t}] + f'[t]
F[Sin,t] (*1*)

Then use Function to make it a pure function

IntAddDiff[f_] := Function[t, Evaluate[Integrate[f[s], {s, 0, t}] + f'[t]]]

Then use # and & to remove t

IntAddDiff[f_] := Evaluate[Integrate[f[s], {s, 0, #}] + f'[#]] &
IntAddDiff[Sin] (*1 &*)
IntAddDiff[#^2 &] (*2 #1 + #1^3/3 &*)
$\endgroup$
1
  • $\begingroup$ For me this looks like the solution that works best for my application. I will do some testing, and after this accept and mark the question as answered. $\endgroup$
    – NG98
    Jan 3, 2021 at 10:29
2
$\begingroup$

Reply the new question.

By using Through we can deal with multiple function {F,G,H} map at t

intAndDiff[f___][t_] := 
  Integrate[Through[{f}@s], {s, 0, t}] + 
   Through[(Derivative[1] /@ {f})@t];
intAndDiff[Sin, Cos, #^2 &][x]

{1, 0, 2 x + x^3/3}

Original

IntAndDiff[f_][t_] := Integrate[f[s], {s, 0, t}] + D[f[t], t];
IntAndDiff[#^2 &][x]
IntAndDiff[Sin][x]

2 x + x^3/3

1

$\endgroup$
1
  • $\begingroup$ I think this suggestion is very close to the solution I search for. It does however not work as intended in the case, where f is a function, that takes a scalar as input and returns a vector, as I cannot "concatenate" it. I.e. I search for an operator, so I take a R->R^n function as input and get a R->R^n function as output... $\endgroup$
    – NG98
    Jan 2, 2021 at 10:34
0
$\begingroup$

Same as @cvgmt's solution but defined as an operator, so that it also works on $\mathbb{R}\to\mathbb{R}^n$ functions. Both the input and the output are now pure functions:

IntAndDiff[f_] := Function[t, Evaluate[Integrate[f[s], {s, 0, t}] + D[f[t], t]]]

IntAndDiff[#^2 &]
(*    Function[t$, 2 t$ + t$^3/3]    *)

IntAndDiff[Sin]
(*    Function[t$, 1]    *)

IntAndDiff[{#, #^2, #^3} &]
(*    Function[t$, {1 + t$^2/2, 2 t$ + t$^3/3, 3 t$^2 + t$^4/4}]    *)
$\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.