Mathematica Asked on May 7, 2021
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}longrightarrowmathbb{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}longrightarrowmathbb{R}^n$ function again. I.e. I want to be able to apply this (and other such operators) on the result again…
$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 &*)
Correct answer by wuyudi on May 7, 2021
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
Answered by cvgmt on May 7, 2021
Same as @cvgmt's solution but defined as an operator, so that it also works on $mathbb{R}tomathbb{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}] *)
Answered by Roman on May 7, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP