0
$\begingroup$

I am trying to solve a problem using Procedural programming paradigm in Mathematica.

I want to find a way to plot a list of functions in a single graph.

The code is as follow, let me know what is wrong.

Show[Plot[Do[L[i], {i, 0, n}]], {xo, -20, 20}]

where L[i] is list of functions, n is the number of elements of the plot and xo is the variable on which all the functions as the elements of L[i] are dependent on. When I plot each element of L, Mathematica shows the plot but not in this case where I want to plot them all together.

I have tried debugging, does'nt help. I do not want to use Table[] as it will be digressing away from Procedural Programming, as I am trying to write a code for a course.

$\endgroup$

closed as off-topic by Szabolcs, rhermans, MarcoB, J. M. will be back soon Aug 23 '17 at 15:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Szabolcs, rhermans, MarcoB, J. M. will be back soon
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ Do does not return results for each iteration. You'd need Table instead. If you really wanted a procedural approach, you'd probably do something along the lines of plots={};Do[AppendTo[plots,Plot[...]],{i,0,n}];... which is arguably worse than just using Table $\endgroup$ – Lukas Lang Aug 23 '17 at 9:29
  • 2
    $\begingroup$ The first rule of debugging in Mathematica: try each part of your input separately and see if it returns what you expected. See what Do[L[i], {i, 0, n}] returns (answer: nothing). Look up Table. $\endgroup$ – Szabolcs Aug 23 '17 at 9:33
  • 1
    $\begingroup$ @Chetan see: merging-accounts, looks like you need it. $\endgroup$ – Kuba Aug 23 '17 at 10:53
1
$\begingroup$

You can do

 Plot[{Sin[x], Cos[x], Sinh[x]}, {x, -Pi, Pi}, PlotLegends -> "Expressions"]

enter image description here

Or

Grid[{Plot[#[x], {x, -Pi, Pi}, PlotLabel -> #] & /@ {Sin, Cos, Sinh}}]

enter image description here

$\endgroup$
0
$\begingroup$

As it is already mentioned by @Szabolcs Table is a much better choice for what you want.

However, if you necessarily want to take the procedural way, keep in mind that loops make operations specified inside, but return nothing. Indeed, the following operation:

    lst = {};

Do[AppendTo[lst, Sin[i*x]], {i, 0, 5}]

return nothing. Try to evaluate it.

However, a list, lst, has already been created, as you can make sure by evaluating it:

lst

(* {0, Sin[x], Sin[2 x], Sin[3 x], Sin[4 x], Sin[5 x]}  *)

Thus, to plot it you simply evaluate the following:

Plot[lst, {x, 0, 2 \[Pi]}]

enter image description here

Have fun!

$\endgroup$

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