Mathematica Looping through vectorized functions for a piecewise solution

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Functions
Click For Summary
The discussion focuses on implementing a piecewise function in Mathematica, specifically how to loop through vectorized functions. The user seeks clarification on a proposed solution involving pure functions and their application to a list of powers of x. After some confusion, they realize that the issue with their code was related to variable handling, which was resolved by clearing the kernel. Ultimately, they successfully grasp the concept of pure functions and how to automate the piecewise function creation. The conversation highlights the importance of understanding pure functions for effective coding in Mathematica.
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,058
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 Dale

Similar threads

  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · 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 3 ·
Replies
3
Views
28K