Skip to main content
Post Closed as "Duplicate" by Szabolcs, Artes, Dr. belisarius, Sjoerd C. de Vries, rm -rf
edited tags
Source Link
Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k

Sometimes when I try to create a function inside a function, mathematcaMathematica gives me a hard time. I usually just have to play around with the syntax until it gets something it "likes" but I really have idea /why/why certain things work and others don't. Here's an example I just ran into recently:

CODE VERSION A:Code version A:

ans[tf_] :=
  NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, 
     v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;
ans[2]
Plot[{x[t], v[t]} /. %, {t, 0, 2}]

So this works beautifully. But for some reason if I try to nest my ans[2]ans[2] inside my function by replacing it with the %%, Mathematica can't evaluate it!

CODE VERSION B:Code version B:

ans[tf_] :=
  NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, 
     v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;

Plot[{x[t], v[t]} /. ans[2], {t, 0, 2}]

So bymy question boils down to: WhyWhy does Code Version A work and Code Version B doesn't?

Sometimes when I try to create a function inside a function, mathematca gives me a hard time. I usually just have to play around with the syntax until it gets something it "likes" but I really have idea /why/ certain things work and others don't. Here's an example I just ran into recently:

CODE VERSION A:

ans[tf_] :=
  NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, 
     v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;
ans[2]
Plot[{x[t], v[t]} /. %, {t, 0, 2}]

So this works beautifully. But for some reason if I try to nest my ans[2] inside my function by replacing it with the %, Mathematica can't evaluate it!

CODE VERSION B:

ans[tf_] :=
  NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, 
     v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;

Plot[{x[t], v[t]} /. ans[2], {t, 0, 2}]

So by question boils down to: Why does Code Version A work and Code Version B doesn't?

Sometimes when I try to create a function inside a function, Mathematica gives me a hard time. I usually just have to play around with the syntax until it gets something it "likes" but I really have idea why certain things work and others don't. Here's an example I just ran into recently:

Code version A:

ans[tf_] :=
  NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, 
     v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;
ans[2]
Plot[{x[t], v[t]} /. %, {t, 0, 2}]

So this works beautifully. But for some reason if I try to nest my ans[2] inside my function by replacing it with the %, Mathematica can't evaluate it!

Code version B:

ans[tf_] :=
  NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, 
     v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;

Plot[{x[t], v[t]} /. ans[2], {t, 0, 2}]

So my question boils down to: Why does Code Version A work and Code Version B doesn't?

added 46 characters in body
Source Link
Dr. belisarius
  • 116.2k
  • 13
  • 205
  • 456

Sometimes when I try to create a function inside a function, mathematca gives me a hard time. I usually just have to play around with the syntax until it gets something it "likes" but I really have idea /why/ certain things work and others don't. Here's an example I just ran into recently:

CODE VERSION A: ans[tf_] := NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten; ans[2] Plot[{x[t], v[t]} /. %, {t, 0, 2}]

ans[tf_] :=
  NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, 
     v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;
ans[2]
Plot[{x[t], v[t]} /. %, {t, 0, 2}]

So this works beautifully. But for some reason if I try to nest my ans[2] inside my function by replacing it with the %, Mathematica can't evaluate it!

CODE VERSION B: ans[tf_] := NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;

Plot[{x[t], v[t]} /. ans[2], {t, 0, 2}]

ans[tf_] :=
  NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, 
     v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;

Plot[{x[t], v[t]} /. ans[2], {t, 0, 2}]

So by question boils down to: Why does Code Version A work and Code Version B doesn't?

Sometimes when I try to create a function inside a function, mathematca gives me a hard time. I usually just have to play around with the syntax until it gets something it "likes" but I really have idea /why/ certain things work and others don't. Here's an example I just ran into recently:

CODE VERSION A: ans[tf_] := NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten; ans[2] Plot[{x[t], v[t]} /. %, {t, 0, 2}]

So this works beautifully. But for some reason if I try to nest my ans[2] inside my function by replacing it with the %, Mathematica can't evaluate it!

CODE VERSION B: ans[tf_] := NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;

Plot[{x[t], v[t]} /. ans[2], {t, 0, 2}]

So by question boils down to: Why does Code Version A work and Code Version B doesn't?

Sometimes when I try to create a function inside a function, mathematca gives me a hard time. I usually just have to play around with the syntax until it gets something it "likes" but I really have idea /why/ certain things work and others don't. Here's an example I just ran into recently:

CODE VERSION A:

ans[tf_] :=
  NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, 
     v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;
ans[2]
Plot[{x[t], v[t]} /. %, {t, 0, 2}]

So this works beautifully. But for some reason if I try to nest my ans[2] inside my function by replacing it with the %, Mathematica can't evaluate it!

CODE VERSION B:

ans[tf_] :=
  NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, 
     v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;

Plot[{x[t], v[t]} /. ans[2], {t, 0, 2}]

So by question boils down to: Why does Code Version A work and Code Version B doesn't?

Source Link

Issues with creating functions

Sometimes when I try to create a function inside a function, mathematca gives me a hard time. I usually just have to play around with the syntax until it gets something it "likes" but I really have idea /why/ certain things work and others don't. Here's an example I just ran into recently:

CODE VERSION A: ans[tf_] := NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten; ans[2] Plot[{x[t], v[t]} /. %, {t, 0, 2}]

So this works beautifully. But for some reason if I try to nest my ans[2] inside my function by replacing it with the %, Mathematica can't evaluate it!

CODE VERSION B: ans[tf_] := NDSolve[{x'[t] == v[t], v'[t] == -x[t] - v[t], x[0] == 1, v[0] == 1}, {x[t], v[t]}, {t, 0, tf}] // Flatten;

Plot[{x[t], v[t]} /. ans[2], {t, 0, 2}]

So by question boils down to: Why does Code Version A work and Code Version B doesn't?