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

Define functions

CarlosRomero
14-Alexandrite

Define functions

Hello everyone,

In the attached worksheet, I would like to define the Shekel family of functions using Mathcad.

I am stuck here. I can’t find the error, help me.

I appreciate it if you share your idea and advice on this worksheet.

All the best,

CarlosRomero

ACCEPTED SOLUTION

Accepted Solutions


@Fred_Kohlhepp wrote:

I think my computer has covid19   😉

 

Edited post to try to reinsert the picture.


Maybe its not Covid-19 but just Khoros 😉

@Fred_Kohlhepp 

I very often experience problems when embedding pictures. After copying a picture there would be created some html-code (you can switch this editor to html mode to see it) but no picture is displayed.

The html code inserted is something like

<div id="tinyMceEditorWerner_E_0" class="mceNonEditable lia-copypaste-placeholder">&nbsp;</div>

but should be something like

<p><li-image width="400" height="400" alt="Werner_E_0-1609793680999.png" align="inline" id="35549iF3DB80055CF6FD40" size="medium" resized="false" sourceType="new"></li-image></p>

Only solution is to post the message and immediately after that open it again for editing and usually it would let you embed pictures as it should be. Guess you ran into the very same bug. EDIT: It just happened to me here again when trying to insert the first pic.

@Jaime_Lee  Is this an already known forum bug? It can't be reliably reproduced but happens quite often. I am not sure what this bug depends on - length of text, time being online, time having the posting open under development, current zodiac sign, ... ?

 

@CarlosRomero 

Its noticeable that all the three minimum values given in the text are a bit smaller than the ones we can actually achieve with the function. I can't say where those values would come from, but with the table values given they can't be calculated. Also the function values at (4,4,4,4)^T always are a bit larger than the minimum values found, so Mathcad is correct at returning those values which are slightly off the (4,4,4,4)^T.

Werner_E_0-1609785953830.png

BTW, instead of using a genetic algorithm you may give "simulated annealing" a try.
Richard had provided a fine implementation here: https://community.ptc.com/t5/PTC-Mathcad/Minimizing-a-Function-using-Simulated-Annealing/m-p/445213/highlight/true#M176579

I copied his functions into the attached worksheet to give it a try

Werner_E_1-1609786271885.png

 

 

 

 

 

 

View solution in original post

18 REPLIES 18

Try this

Fred_Kohlhepp_0-1609703395065.png

Fred_Kohlhepp_1-1609703441900.png

Saved in Prime 4 below.  (Sorry, don't have 15)

 

LucMeekes
23-Emerald III
(To:CarlosRomero)

How about:

LucMeekes_0-1609710537317.png

 

Success!
Luc

Even though your function is a bit cumbersome, it is easy to repair. You have overlooked that in the text the first index of a_i, j denotes the column and not the row. This can be easily repaired, either by transposing the matrix M or by swapping the two indices in the formula (as can be seen in the picture).

It also would be easier to define c transposed, this way you can avoid the index 0 at c.

Werner_E_0-1609734757546.png

As you have seen by the answers of Fred and Luc, its more clear and elegant to define the matrix as an array.

Luc also provided a single function for all three Shekel functions which is also ORIGIN-aware.

His result differs because he missed the leading minus sign and forgot to square the summands:

Werner_E_0-1609737449371.png

 

Fred used Primes table feature (not available in real Mathcad) to build the matrix. Note that he had set the system variable ORIGIN to 1 and also transposed the matrix a, so he could use the very same indices as in the text. His result is different because he mistakenly included the c_j in the sum.

 

Werner_E_0-1609735202665.png

Werner_E
25-Diamond I
(To:Werner_E)

Here is a slight modification of Lucs implementation

Werner_E_0-1609759285232.png

 

Or, to be even closer to the notation of the formula in the text

Werner_E_1-1609759322144.png

 

 

 

CarlosRomero
14-Alexandrite
(To:Werner_E)

Thanks Fred, Luc and Wernner for your advice and good answers.

I applied a genetic algorithm to the shekel functions given by Fred to find the global optimum and it gives me values of the four parameters x1, x2, x3, x4 approximate to the answer indicated in the book of Zbigniew Michalewicz; that is 4, 4, 4, 4. But the value of the functions are not equal to s3min = -10.15320, s4min = -10.402820 y s5min = -10.53628; otherwise they give me values like the ones shown in the attached image.

 

Do you have any ideas to fix this problem?

Big thank.

Carlos Romero.shekel_test1_ answer.jpg

If I correct the mistake that Werner pointed out, then put in  the answer indicated in the book of Zbigniew Michalewicz; that is 4, 4, 4, 4 we get very close:

Fred_Kohlhepp_0-1609776460916.png

 

 

I guess thats the picture Fred intended to attach

Werner_E_0-1609770886158.png

 

Actually your genetic algorithm quite often results in less useful values (obviously because of the random values created) quite often when you recalculate the expression.

Werner_E_1-1609770932423.png

 

The values shown in the first pic belong to a minimum which is even a little bit below of what you get with a vector consisting of all 4's.

Its also unsure how the text arrives at the value -10.15320, Maybe the values in the matrix a are not exactly 3.6, etc. ?

As you can see in the picture below, Mathcads built-in "minimize" command arrives at the same values as your genetic algorithm (if the latter choses "good" random numbers).

Werner_E_3-1609770061153.png

Remark: minimize is also very sensitive with respect to the guess values and with different guess values you might only arrive at a local minimum and not the global one.

Werner_E_5-1609770280930.png

 

 

 

I think my computer has covid19   😉

 

Edited post to try to reinsert the picture.


@Fred_Kohlhepp wrote:

I think my computer has covid19   😉

 

Edited post to try to reinsert the picture.


Maybe its not Covid-19 but just Khoros 😉

@Fred_Kohlhepp 

I very often experience problems when embedding pictures. After copying a picture there would be created some html-code (you can switch this editor to html mode to see it) but no picture is displayed.

The html code inserted is something like

<div id="tinyMceEditorWerner_E_0" class="mceNonEditable lia-copypaste-placeholder">&nbsp;</div>

but should be something like

<p><li-image width="400" height="400" alt="Werner_E_0-1609793680999.png" align="inline" id="35549iF3DB80055CF6FD40" size="medium" resized="false" sourceType="new"></li-image></p>

Only solution is to post the message and immediately after that open it again for editing and usually it would let you embed pictures as it should be. Guess you ran into the very same bug. EDIT: It just happened to me here again when trying to insert the first pic.

@Jaime_Lee  Is this an already known forum bug? It can't be reliably reproduced but happens quite often. I am not sure what this bug depends on - length of text, time being online, time having the posting open under development, current zodiac sign, ... ?

 

@CarlosRomero 

Its noticeable that all the three minimum values given in the text are a bit smaller than the ones we can actually achieve with the function. I can't say where those values would come from, but with the table values given they can't be calculated. Also the function values at (4,4,4,4)^T always are a bit larger than the minimum values found, so Mathcad is correct at returning those values which are slightly off the (4,4,4,4)^T.

Werner_E_0-1609785953830.png

BTW, instead of using a genetic algorithm you may give "simulated annealing" a try.
Richard had provided a fine implementation here: https://community.ptc.com/t5/PTC-Mathcad/Minimizing-a-Function-using-Simulated-Annealing/m-p/445213/highlight/true#M176579

I copied his functions into the attached worksheet to give it a try

Werner_E_1-1609786271885.png

 

 

 

 

 

 

CarlosRomero
14-Alexandrite
(To:Werner_E)

Thanks Warner. That´s correct.

I see a function with the same name and a different number of parameters.

Can you please explain this to understand how Mathcad "thinks"...?Anneal_50121.jpg


@CarlosRomero wrote:

Thanks Warner. That´s correct.

I see a function with the same name and a different number of parameters.

Can you please explain this to understand how Mathcad "thinks"...?


If you define a function like

f(x,y):=x^y

then f(3) is somewhat equivalent to g, where g(y):=f(3,y) (which is equivalent to g(y)=3^y).

This means that if we don't give all arguments in a multiargument function, we create a function in the remaining arguments.

Werner_E_0-1609833369369.png

Instead of writing "Shekel2(5)" as argument in the "Anneal" function, I could have defined a new function "sh(x):=Shekel2(5,x)" and use the function name "sh" as first argument in "Anneal".

 

Mathcad is not fully consequent and something like Shekel(5) can't be used in all situations like a simple function name. For example we cannot write something like Shekel(5)(3)=

LucMeekes
23-Emerald III
(To:Werner_E)

Note that this trick works in Mathcad 15 (maybe also in 12 through 14), but it does not work in Mathcad 11 and before:

LucMeekes_0-1609834961942.png

and not in Prime 1 and up:

LucMeekes_1-1609835136969.png

Luc

Werner_E
25-Diamond I
(To:LucMeekes)

Yes, I was aware that this method doesn't work any more in Prime. It sure works in MC14, too, but I wasn't sure about older Mathcad versions, though. It also doesn't work with most built-in functions (for example you cant define p:=polyfit(X,Y) and use p2:=p(2) to define the quadratic fit, you must use p2:=polyfit(X,Y,2)).

I use it because its convenient and looks natural and intuitive to me, but of course you can always define extra functions to do the job as described in my answer above. I see it as a deficit that this option no longer exists in Prime.

 

CarlosRomero
14-Alexandrite
(To:Werner_E)

Thanks Werner for your experience and very good observation Luc on using multiple arguments of a function in Prime.

 

I want to check the operation of a simulated annealing algorithm with the shekel functions. The example function for this algorithm has two arguments.

CR50121_1.pngCR50121_2.png

How would you apply the trick in the shekel function to use this algorithm?

Thank you.

CR

....attached the worksheet.

CR

I am not sure what exactly you are looking for and the worksheet does not match the picture you posted.

See if the attached file helps.

I haven't looked at the algorithm you implemented, but it doesn't seem to find the minimum.

CarlosRomero
14-Alexandrite
(To:Werner_E)

Maybe for some functions works. Here an example. It takes 5 minutes.

Hello Werner.

Thanks for sharing your experience in Mathcad.

The algorithm works but it is also very sensitive with respect to the guess values.

Thank you very much

CR

Announcements

Top Tags