With my function $f(x)$ I would like to Plot3D
$\int_a^b f(x) dx$
with the parameter values $a \in [0,1]$ and $b \in [1,3]$. The challenging part has to do with my integrand, which is as follows
$f(x)=x$ if $a \leq b \leq 2a$,
$f(x)=x^2$ if $b > 2a$.
My Mathematica code is as follows:
f = Integrate[If[a <= b <= 2\ a, x, x^2], {x, a, b}]; Flatten[Table[{a, b, f}, {a, 0, 1, .1}, {b, 1, 3, .1}], 1]
When I run this, I get results that look correct if the condition is 'true', but if the condition is 'false' I get results with the associated integral value Undefined. I wonder if there is anything wrong with my code. Thanks!
Answer
You can use If to get the job done
f = If[a <= b <= 2 a, x, x^2]
ifmethod = Flatten[Table[{A, B, Integrate[f /. {a -> A, b -> B}, {x, A, B}]}, {A, 0, 1, .1}, {B, 1, 3, .1}], 1]
The key here is to tell f what a,b are with the replacement before the integration takes place so that you get the right form (e.g. $x$ or $x^2$).
f[a_, b_, x_] := Piecewise[{{x, a <= b <= 2 a}, {x^2, b > 2 a}}]
piecewisemethod = Flatten[Table[{a, b, Integrate[f[a, b, x], {x, a, b}]}, {a, 0, 1, .1}, {b, 1, 3, .1}], 1]
ifmethod == piecewisemethod
True
Comments
Post a Comment