Looping through vectorized functions for a piecewise solution

  • Mathematica
  • Thread starter member 428835
  • Start date
  • Tags
    Functions
In summary, the conversation discusses the use of pure functions in Mathematica and how to implement a loop inside a piecewise function. The second line of the proposed solution uses hashtags and an ampersand to define a pure function, which is then applied to a list of numbers. The conversation also mentions the usefulness of pure functions and provides a link for further learning. Lastly, it discusses a code snippet and troubleshooting steps taken to get it to work properly.
  • #1
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: 954
Physics news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #5
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/]
 
  • #6
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##).
 
  • #7
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

1. What is the purpose of looping through vectorized functions for a piecewise solution?

The purpose of looping through vectorized functions for a piecewise solution is to efficiently apply a set of functions to a large dataset. This allows for a more streamlined and faster way to process and analyze data, especially when dealing with complex calculations or multiple conditions.

2. How does looping through vectorized functions work?

Looping through vectorized functions involves using a single function to apply operations to multiple data points at once, rather than processing each data point individually. This is done by creating a vector of data and then using a function to perform the desired operations on the entire vector simultaneously.

3. What are the advantages of using vectorized functions for a piecewise solution?

There are several advantages to using vectorized functions for a piecewise solution, including increased speed and efficiency, as well as the ability to easily apply complex calculations and conditions to large datasets. Additionally, vectorized functions can help reduce the likelihood of errors and make code more concise and readable.

4. Can any type of function be vectorized for a piecewise solution?

In most cases, any type of function can be vectorized for a piecewise solution. However, some functions may not be suitable for vectorization, such as those that require iteration or have side effects. It is important to carefully consider the function and how it will be applied before attempting to vectorize it.

5. Are there any limitations to using vectorized functions for a piecewise solution?

While vectorized functions offer many advantages, there are a few limitations to keep in mind. For example, some functions may not be easily vectorizable, and the use of vectorized functions can sometimes lead to less readable or more complex code. Additionally, not all programming languages or environments may support vectorization.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
761
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
Replies
6
Views
972
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Differential Equations
Replies
1
Views
767
  • Engineering and Comp Sci Homework Help
Replies
27
Views
5K
  • Differential Equations
Replies
2
Views
1K
  • Advanced Physics Homework Help
Replies
7
Views
4K
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
Back
Top