3
$\begingroup$

I am quite new to mathematica, and I simply want to know how to make a custom function, take another arbitrary function as an input.

I have attached code that shows what I am trying to accomplish (with output in the form of a list of lists). The last line of code is trying to be descriptive with reagrds to the roles that the different inputs should play, however it is only the "somefunction" part with its "variable" that I am mainly interested in knowing how it works.

Best regards.

sildeben[Sin[x], {x, 0, Pi, 2}]

Output:{{0., 0.}, {1.5708, 1.}, {3.14159, 0.}}

f[x_] := 3 x^3 - 6 x

sildeben[f[t], {t, -3, 3, 10}]

Output:{{-3., -63.}, {-2.4, -27.072}, {-1.8, -6.696}, {-1.2, 2.016}, {-0.6, 2.952}, {0., 0.}, {0.6, -2.952}, {1.2, -2.016}, {1.8, 6.696}, {2.4, 27.072}, {3., 63.}}

sildeben[somefunction_, {variable_, startvalue_, endvalue_, steps_}]

$\endgroup$

3 Answers 3

5
$\begingroup$
sildeben[expr_,{variable_,startvalue_,endvalue_,steps_}] := Table[
   {variable,expr},{variable,Subdivide[startvalue,endvalue,steps]}];
$\endgroup$
3
$\begingroup$
{#, Sin[#]} & /@ Subdivide[0, π, 2] // N

{{0., 0.}, {1.5708, 1.}, {3.14159, 0.}}

f[x_] := 3 x^3 - 6 x

{#, f[#]} & /@ Subdivide[-3, 3, 10] // N

{{-3., -63.}, {-2.4, -27.072}, {-1.8, -6.696}, {-1.2, 2.016}, {-0.6, 2.952}, {0., 0.}, {0.6, -2.952}, {1.2, -2.016}, {1.8, 6.696}, {2.4, 27.072}, {3., 63.}}

$\endgroup$
3
$\begingroup$

Syed's answer is fine. I am suggesting another for completeness

funct[steps_] := N@Table[{a Pi, Sin[a Pi]}, {a, 0, steps, 1/2}]

when you run

funct[1]

you get

{{0., 0.}, {1.5708, 1.}, {3.14159, 0.}}

and for the second

funct[steps_] := N@Table[{x, 3 x^3 - 6 x}, {x, -3, 3, steps}]

then

funct[0.6]

gives

{{-3., -63.}, {-2.4, -27.072}, {-1.8, -6.696}, {-1.2, 2.016}, {-0.6, 2.952}, {0., 0.}, {0.6, -2.952}, {1.2, -2.016}, {1.8, 6.696}, {2.4, 27.072}, {3., 63.}}

$\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.