Message Boards Message Boards

0
|
1832 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Why isn't Return giving me output from Do?

Posted 1 year ago

Trying to make my graphs in a Do loop, just to familiarize myself with another way to do this. I've read that when Return is used with Do, then Do sometimes doesn't give Null. I looked up Return, and they use Return[a], on the examples in the documentation. I tried that; I also tried Return[y2] and Return[y[x,t]], besides experimenting with the placement of the Return command, within the Do loop. Here is one of my attempts. Why am I not getting output?

ClearAll[y,x,t]
h=1;l=10;v=1;
y[x_,t_]=Sum[(8h/(n^2 Pi^2))(2 Sin[n Pi/4]-Sin[n Pi/2]) Sin[ n Pi x/l] Cos[n Pi v*t/l], {n,140,1}];
y2=Do[Plot[y[x,t],{x,0,10}, AxesLabel->{"x","y[x,t]"}, PlotLegends->StringJoin["t=",ToString[t]],Return[a]],{t,0,2l/v,.02l/v}];
ListAnimate[y2]

Another attempt was,

ClearAll[y,x,t]
h=1;l=10;v=1;
y[x_,t_]=Sum[(8h/(n^2 Pi^2))(2 Sin[n Pi/4]-Sin[n Pi/2]) Sin[ n Pi x/l] Cos[n Pi v*t/l], {n,140,1}];
y2=Do[Plot[y[x,t],{x,0,10}, AxesLabel->{"x","y[x,t]"}, PlotLegends->StringJoin["t=",ToString[t]],Return[y[x,t]],{t,0,2l/v,.02l/v}];
ListAnimate[y2]

I also tried,

ClearAll[y,x,t]
h=1;l=10;v=1;
y[x_,t_]=Sum[(8h/(n^2 Pi^2))(2 Sin[n Pi/4]-Sin[n Pi/2]) Sin[ n Pi x/l] Cos[n Pi v*t/l], {n,140,1}];
y2=Do[Plot[y[x,t],{x,0,10}, AxesLabel->{"x","y[x,t]"}, PlotLegends->StringJoin["t=",ToString[t]],{t,0,2l/v,.02l/v}, Return[y2]];
ListAnimate[y2]

Would love any input on how I can actually get figures while using a Do loop. Thanks so much

POSTED BY: Lewis Jones
2 Replies

ListAnimate[y2] makes me think you expect Do to be able to work like Table and produce a list of results of each iteration. It cannot. Return could return a single result the first time it is executed (a.k.a. "evaluated") in the Do loop. For it to return a plot, the argument to Return should be a plot. But the argument is not a plot in any of your examples.

Further, you place Return[..] as an argument to Plot[], in a position where Plot[] expects an option. I get error messages, which seem to be ignored in question.

If you want to make a list of output (plots or whatever), use Table or Map. They're the best ways. If you want to make Do[] construct your list then try something like this:

resultList = {}; (* initialize *)
Do[ nextResult = ...; AppendTo[resultList, nextResult], {...}]

Or this:

resultLL = {}; (* initialize linked list *)
Do[ nextResult = ...; resultLL = {resultLL, nextResult}, {...}];
resultList = Flatten[resultLL]; (* does not work if nextResult is a List *)

The second, linked-list way is usually more efficient than AppendTo.

POSTED BY: Michael Rogers

At that last position Do only allows the {i,a,b} range list

Return[] is leaving the loop somwher in the middle by any function that yields as a result a Return[---] in the compound statement, separated by semicolons on the level of the first argument of

Do[ a:  b:  v;....,  dom ]  

Do[  Print[i]  ;   If[i == 3, Return[   Print["Exit at ", i]]  ]  , {i, 1, 10}]
POSTED BY: Roland Franzius
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract