cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

A sin function with various number of iterations in each cycle

mzarrabi
12-Amethyst

A sin function with various number of iterations in each cycle

Hello everyone. I wonder how to have a sin function with 3  cycles (or more) that each cycle of it has different number of iterations. For example, I want iteration steps to be 1 in the first cycle and decrease to 0.5 in the second and third cycles (without decreasing or increasing the number of cycles). 

 

Physical Mathematics

1 ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:mzarrabi)

Try a function like this:

LM_20171231_SineCycles1.png

Where the third argument (PointsPerCycle) can be a scalar or an array, that determines the number of points per cycle.

Here you see it called in both modes:

LM_20171231_SineCycles2.png

And the resulting plots are:

LM_20171231_SineCycles3.png

As you can see for the first cycle (15 points per cycle) the two point sets coincide.

For the second cycle there are more boxes than x-es, in the third and following cycles there are twice as many boxes as there are x-es.

 

Success!
Luc

View solution in original post

4 REPLIES 4
LucMeekes
23-Emerald III
(To:mzarrabi)

Try a function like this:

LM_20171231_SineCycles1.png

Where the third argument (PointsPerCycle) can be a scalar or an array, that determines the number of points per cycle.

Here you see it called in both modes:

LM_20171231_SineCycles2.png

And the resulting plots are:

LM_20171231_SineCycles3.png

As you can see for the first cycle (15 points per cycle) the two point sets coincide.

For the second cycle there are more boxes than x-es, in the third and following cycles there are twice as many boxes as there are x-es.

 

Success!
Luc

mzarrabi
12-Amethyst
(To:LucMeekes)

That is exactly what I am looking for. Thank you so much @LucMeekes for that. I just don't understand some part of it, especially the first line. Actually, I have never used "not boolean operator". Beside that, you are using an "if" statement without using an "otherwise" part for that. Also, what is the role of setting "Origin" to "r" in the middle of the function? Would you be so kind to briefly explain these?  Best,

LucMeekes
23-Emerald III
(To:mzarrabi)

The first line just asks if the parameter PointsPerCycle is an array (a vector) rather than a scalar. IF that is not the case, the statements of the body of the IF-statment are executed. This is a simple case with a constant number of points per cycle, for each cycle the same value. At the end of the IF, the resulting matrix with the x- and y-data is returned by the function. The function ends here.

IF PointsPerCycle is an array, the body of the IF statement is not executed, but execution continues after the IF statement with the first assignment to r (which is used as the index to the matrix R) I preset it to ORIGIN. ORIGIN is a worksheet variable that points to the first element of an array (or first row and first column of a matrix). ORIGIN is usually set to 0, but you can set it to other values, the most common of which is 1. I want my functions to operate with any valye of ORIGIN (= be independent of the actual value of ORIGIN), hence I use its value, rather than assuming the user will have set it to 0...

I don't need an OTHERWISE for the IF because when the IF test is TRUE, the function returns R as part of the last statement in the IF body. I could use it, and it is 'better programming practice'. It would mean that all statements from r<- ORIGIN to the end would move to the body of the OTHERWISE.

But I usually choose to end the function when it's done, using the RETURN statement.

 

Success!

Luc

 

mzarrabi
12-Amethyst
(To:LucMeekes)

Thank you @LucMeekes for your thorough explanation. That was interesting and helped me a lot. Best,

Top Tags