1
$\begingroup$

I'm trying to generate a plot in mathematica using the Table function. Below is an example of what I'm trying to produce:

f[x_, y_, z_]:= (x + y + z)^2
Table[f[1, 2, x=z], {z, 0, 10, 2}]= {9, 25, 49, 81, 121, 169}

Now I want to plot the output from Table[f[1, 2, z], {z, 0, 10, 2}] against the same range of z values inputed into the function (0, 2, 4, 6, 8, 10). I have tried all sorts but really can't get it to work. It should be pretty basic but I haven't used mathematica in a while-appreciate your help with this

Thanks

$\endgroup$
2
  • 1
    $\begingroup$ ListPlot[{9, 25, 49, 81, 121, 169}, DataRange -> {0, 10}] $\endgroup$ Mar 6 at 17:26
  • $\begingroup$ Thanks so much Daniel-I completely forgot about ListPlot. That DataRange option is pretty handy for this! $\endgroup$
    – John_dydx
    Mar 6 at 17:28

1 Answer 1

1
$\begingroup$

One option could be to use the ListLinePlot with {{x,y},...} input. For this you need to transpose the x,y data that you have.

I changed your x=z in the code to just z. I did not see a reason to write x=z there.

ClearAll[x, y, z];
f[x_, y_, z_] := (x + y + z)^2
range = Range[0, 10, 2]
data = Table[f[1, 2, z], {z, range}]
mat = Transpose[{range, data}]

Mathematica graphics

The above is your {x,y} data. Now plot it using

ListLinePlot[mat, Mesh -> All, MeshStyle -> Red]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Thanks for your solution Nasser $\endgroup$
    – John_dydx
    Mar 6 at 17:28

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.