Looping through vectorized functions for a piecewise solution

  • Context: Mathematica 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Functions
member 428835
Hi PF!

Can someone explain the second line of the proposed solution on this thread to me

https://mathematica.stackexchange.com/questions/138919/how-to-implement-a-loop-inside-piecewise

Specifically, I have a function un(x) that looks like
Screen Shot 2018-03-26 at 4.47.07 PM.png

I am trying to make this function piecewise such that un[x][[1]] is plotted over a domain ##x\in(0,2h)## and then un[x][[2]] ##x\in(2h,4h)##. I'm unsure how to loop through this procedure. Ultimately un[x] will have many more components, so automating this would be helpful.

Thanks!
 

Attachments

  • Screen Shot 2018-03-26 at 4.47.07 PM.png
    Screen Shot 2018-03-26 at 4.47.07 PM.png
    13.4 KB · Views: 1,090
Physics news on Phys.org
joshmccraney said:
Can someone explain the second line of the proposed solution on this thread to me
It is just a list of powers of x.
 
Dale said:
It is just a list of powers of x.
Oh shoot, I said second but I meant first. Sorry, it's been a busy day.
 
Dale said:
The hashtags and the ampersand denote a “pure function” that is not given a name. It is a very useful construction, I highly recommend learning to use them.

http://reference.wolfram.com/language/tutorial/PureFunctions.html

Once that function is defined it is applied to {0,1,2}
Got it! Looked it up and it makes sense. However, the following code seems to only fork when I put ##x## in the table (I get results if I put ##x+1-1## and also if I put a constant) but if I put ##2x## or ##x+1## I do not get an output. Any ideas?
Code:
h = 0.25;
cond = 2 (# - 1) h < x < 2 # h & /@ Range[1, 1/(2 h)];
f = Table[x+1, {i, 1, 1/(2 h)}];
g = Piecewise[Transpose[{f, cond}]];
Plot[g, {x, 0, 1}, PlotRange -> {{0, 1}, {0, 3}}]
[code/]
 
joshmccraney said:
Code:
f = Table[x+1, {i, 1, 1/(2 h)}];
This only create a certain number of copies of the same function (here ##x+1##).
 
DrClaude said:
This only create a certain number of copies of the same function (here ##x+1##).
Yea, I think I had to clear my Kernal. For some reason it was only allowing ##x## and not something like ##x+1##. I have it working now and finally understand (to some level) pure functions! Thanks all!
 
  • Like
Likes   Reactions: Dale

Similar threads

  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K