Looping through vectorized functions for a piecewise solution

  • Context: Mathematica 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Functions
Click For Summary

Discussion Overview

The discussion revolves around implementing a piecewise function in Mathematica, specifically focusing on how to loop through vectorized functions to automate the creation of multiple components of the function. Participants seek clarification on specific lines of code and the behavior of pure functions within the context of piecewise definitions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant asks for clarification on a specific line of code from a proposed solution regarding the implementation of a piecewise function.
  • Another participant notes that the line in question is simply a list of powers of x, indicating a misunderstanding of the code's purpose.
  • There is a discussion about the use of pure functions in Mathematica, with references to their utility and a link to further resources.
  • A participant expresses confusion about why certain expressions involving x do not yield outputs, while others do, leading to a deeper inquiry into the behavior of the code.
  • Another participant points out that the code creates multiple copies of the same function, suggesting a limitation in the approach being used.
  • A later reply indicates that clearing the kernel resolved issues with function behavior, leading to a better understanding of pure functions.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the implementation of piecewise functions and pure functions, with some clarifications being made but no consensus on the best approach to automate the function creation.

Contextual Notes

Participants mention specific behaviors of the code that depend on the definitions used and the context in which functions are applied, indicating potential limitations in the current understanding of the implementation.

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,078
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
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 2 ·
Replies
2
Views
4K