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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Translate the entire conversation x

How to get the max value from a self define function?

GT_13604326
3-Newcomer

How to get the max value from a self define function?

GT_13604326_0-1749102709048.png

 

As the picture above, I want to get the max value for the function K,under a certain condition, but the max() return the value like a matrix,so  how can I get the right value?

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:GT_13604326)

Ahh! So far I had overlooked that you just used the first value of vector Q when you tried to apply the "max" function, not the full vector.

So you are only interested in the maximum value of K for m=6 and Q=0.2.

This can be done the way you had tried. Just 2 corrections:

1) You have to define F as a vector, not a range

2) You must apply vectorization (the arrow over the expression)

Werner_E_0-1749128690280.png

actually just the function call of K must be vectorized

Werner_E_1-1749128809760.png

This gives you the max value of K, but does not reveal the corresponding F-value.

You could use the "match" function to get the index of the max values and calculate the F-value from this:

Werner_E_3-1749130671575.png

Werner_E_4-1749130948984.png

 

As already written in a previous answer you could also use the derivative to get F a bit more accurate.

Werner_E_1-1749130467612.png

 

BTW, in case you are not using Prime 11, the "vec" function is not available. But its easy to create your own "vec" function:

Werner_E_2-1749130539353.png

 

View solution in original post

11 REPLIES 11
Werner_E
25-Diamond I
(To:GT_13604326)

Problem is that F is not a vector but rather a range variable.

So you want m to be constant 6 and find the maximum of the 59406 values you get when you combine any of the 6 Q-values with any of the 9901 F-values?

First you have to define F as a vector. If you are using Prime 11 you can use the new function "vec" -> Fx:=vec(0.1, 0.101, 10)

Then you have to create a matrix (6 x 9901) with all the values. You may either use a small program with two nested for loops or use two range variables to index the elements. The last step is to use the "max" function to find the maximum of all the values in the matrix.

Werner_E_0-1749105078714.png

Of course you would get a different result as I did not bother to use your function and your values in the vectors.

 

Another option would be to do without the "max" function but simply looping through all the values and keeping the largest one so far:

Werner_E_0-1749107024106.png

 

For future questions keep in mind that you always should attach the worksheet itself, not just a picture and that you should say which version of Prime you are using.

 

 

Hi,thanks a lot! 

okay! I will remember that when asking for help, it's better to attach the worksheet. 

By the way, I have found that in order to get XY plot , I need to define the Fx as  a range variable (And I am using Prime-11). But the max() is working need to define the Fx as a vector.

GT_13604326_0-1749177993049.png

My early idea is to get the max value to draw the line like this picture.

GT_13604326_1-1749178154800.png

And the final result is to get like this picture, to differentiate the areas between left and right

GT_13604326_2-1749178244580.png

 

 

image.png

Werner_E
25-Diamond I
(To:GT_13604326)


@GT_13604326 wrote:

Hi,thanks a lot! 

okay! I will remember that when asking for help, it's better to attach the worksheet. 

By the way, I have found that in order to get XY plot , I need to define the Fx as  a range variable (And I am using Prime-11). But the max() is working need to define the Fx as a vector.

 


Yes, usually you would use a range variable to plot a function, but you can also use a vector.

But if you use a vector at the x-axis, you also have to use a vector at the y-axis. So to use a vector Fx for the plot, you would have to vectorize the function calls at the y-axis.

A range is kind of an implicit loop, not a data structure. So when you call "max" with Fx being a vector, Prime loops through Fx and applies the max function at each loop. So each turn juts one value is feeded in the K function and max returns the maximum of the result (whoch is just one value as well). All these results are then presented in a way which looks like a vector, but isn't.

Actually the very same result would be to expected when I accidentally vectorized the whole max(K(...)) function call and not just the K(....) inside of the max function. Fortunately "max" is a function which can't be vectorized and so this faux pas had no negative effect 😉

 

In the attached sheet I vectorized already the function definition of K, that way I don't have to use vectorization whenever K is called with a vector F as argument. I thought that the vectorization arrows look too ugly in the plot.

EDIT: On the other hand there is no need for F to be a vector anymore because I am using the derivative and the "root" function to determine the peaks. So you could also define F as a range used just for plotting and don't need to vectorize the definition of K.

 

Furthermore I use a function to determine the F-value of the maximum. I am not using the brute force way (creating a vector of values and choosing the largest value) but rather let the "root" function determine the zero of the derivative. That way its easy to plot the brown locus of the peaks.

Werner_E_1-1749202783882.png

 

 

Wow! Thanks again!

 

Finally, I got it!And I have learned a lot! 

GT_13604326_0-1749261321742.png

In the end, Best wishes!😁

 

Werner_E
25-Diamond I
(To:GT_13604326)

And here is the version with a range F instead of a vector and without having to vectorize the definition of function K.

Vectorization (over vector Q) is only necessary for the matrix of the peak values.

Werner_E_0-1749270580781.png

 

Hmm.  It's worth drawing a graph first:

 (I've just set m to the constant value of 6, and not included it as an input variable below) 

Fn.png

 

 

EDIT

When F = sqrt(1/6), Q=0, then K = 8.333333340*10^8

 

 

 

Werner_E
25-Diamond I
(To:AlanStevens)

You are right - the value you found is not the max, When you set Q=0 as you did, we have a singularity at F=1/sqrt(6), So the "max" here is infinity 😉

 

But we don't know what @GT_13604326  means when he writes "under a certain condition".

Maybe he is only interested in the six Q values (0.2 to 5) and 9901 F values (0.1 to 10) he provided. So maybe Q never should drop beneath 0.2.

In this case the maximum is 2.522 for Q=0.2 and F=0.426. This max value could be increase by about 10^-5 if F is allowed to be set at 0.42576594386598241263.

For a value of Q>0 we can use the derivative wrt to F to find the max position.

Werner_E_6-1749125685952.png

Werner_E_7-1749126078906.png

 

 

Werner_E_5-1749125189137.png

 

 


@Werner_E wrote:

We don't know what @GT_13604326  means when he writes "under a certain condition".

Very true.  

Thanks for reply!

Werner_E
25-Diamond I
(To:GT_13604326)

Ahh! So far I had overlooked that you just used the first value of vector Q when you tried to apply the "max" function, not the full vector.

So you are only interested in the maximum value of K for m=6 and Q=0.2.

This can be done the way you had tried. Just 2 corrections:

1) You have to define F as a vector, not a range

2) You must apply vectorization (the arrow over the expression)

Werner_E_0-1749128690280.png

actually just the function call of K must be vectorized

Werner_E_1-1749128809760.png

This gives you the max value of K, but does not reveal the corresponding F-value.

You could use the "match" function to get the index of the max values and calculate the F-value from this:

Werner_E_3-1749130671575.png

Werner_E_4-1749130948984.png

 

As already written in a previous answer you could also use the derivative to get F a bit more accurate.

Werner_E_1-1749130467612.png

 

BTW, in case you are not using Prime 11, the "vec" function is not available. But its easy to create your own "vec" function:

Werner_E_2-1749130539353.png

 

Announcements


Top Tags