1
$\begingroup$

I want to integrate a a simple function for different t values.

At the moment I have the following code;

t = 2;
y[x_] = x^t;
SetAttributes[y , Listable]

yy = Integrate[y[x], x]
ListPlot[Table[{x,yy}, {x,0,10}]]

How could I return at once the yy solutions, but with different t terms and plot the multiple yy solutions for different t terms all in one graph?

Also I would like to use this solutions in later functions. How could I pass on solutions for multiple t values to ensure in future that functions using the solutions would be able to access the solution for any t value?

I am new to mathematica not sure how to do this.

$\endgroup$
1
$\begingroup$

could I return at once the yy solutions but for different t terms and plot multiple yy solutions for different t terms all in one graph?

One way could be to change the definition of your y to also accept t. Something like

ClearAll[y, t, x]
y[x_, t_] = x^t;
tValues = Range[4];
yy = Table[Callout[Integrate[y[x, t], x], Row[{"t =", t}]], {t, tValues}];
Plot[Evaluate@yy, {x, 0, 2}, AxesLabel -> {"x", "f(x)"},BaseStyle -> 12,
     GridLines -> Automatic, GridLinesStyle -> LightGray]

Mathematica graphics

$\endgroup$
1
$\begingroup$

Here is an implementation that has the advantage that you are not committed to the independent variable being x.

SetAttributes[y, Listable]
y[t_] = #^t &;
SetAttributes[yInt, Listable]
yInt[t_] = Integrate[y[t][#], #] &;

Then you can plot yInt multiple values of t with

Plot[Evaluate @ Through[yInt[Range[4]][x]], {x, 0, 2}, PlotLegends -> "Expressions"]

plot

You can map yInt over a range in the usual way.

yInt[#][x] & /@ Range[-4, 4] 

{-(1/(3 x^3)), -(1/(2 x^2)), -(1/x), Log[x], x, x^2/2, x^3/3, x^4/4, x^5/5}

Or do it with Through making use of it Listable attribute.

Through[yInt[Range[-4, 4]][u]]

{-(1/(3 x^3)), -(1/(2 x^2)), -(1/x), Log[x], x, x^2/2, x^3/3, x^4/4, x^5/5}

And, of course, call from other functions.

Integrate[yInt[1/2][z], z]

(4 z^(5/2))/15

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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