Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X
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
Solved! Go to Solution.
@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 😉
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"> </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, ... ?
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.
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
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.
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:
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.
Here is a slight modification of Lucs implementation
Or, to be even closer to the notation of the formula in the text
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.
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:
I guess thats the picture Fred intended to attach
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.
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).
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.
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 😉
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"> </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, ... ?
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.
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
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"...?
@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.
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)=
Note that this trick works in Mathcad 15 (maybe also in 12 through 14), but it does not work in Mathcad 11 and before:
and not in Prime 1 and up:
Luc
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.
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.
How would you apply the trick in the shekel function to use this algorithm?
Thank you.
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.
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