Looping through vectorized functions for a piecewise solution

  • Mathematica
  • Thread starter joshmccraney
  • Start date
  • #1
joshmccraney
Gold Member
1,959
102
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

Answers and Replies

  • #2
30,321
6,792
Can someone explain the second line of the proposed solution on this thread to me
It is just a list of powers of x.
 
  • #3
joshmccraney
Gold Member
1,959
102
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.
 
  • #5
joshmccraney
Gold Member
1,959
102
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/]
 
  • #6
DrClaude
Mentor
7,505
3,786
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##).
 
  • #7
joshmccraney
Gold Member
1,959
102
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 Dale

Related Threads on Looping through vectorized functions for a piecewise solution

  • Last Post
Replies
1
Views
854
Replies
5
Views
2K
Replies
1
Views
4K
Replies
5
Views
4K
Replies
2
Views
1K
Replies
1
Views
5K
Replies
5
Views
912
Replies
2
Views
6K
Replies
1
Views
8K
Top