Polyroots, arrangement of solutions - Сообщения
Wrote
3. Your cubic is incorrect vs the conventional attached and Polyroots.
4. That's another story but can be done from scanning trough the various
vectors you want to explore. Possibly plot/stack all explorations.
More and + interesting ... Jean
cubicroots [sanity check].sm (37 КиБ) скачан 39 раз(а).
Jean, I think you got the order of the coefficients wrong on my cubic solver.
Best regards
Wrote
........................
2. The cubic module as constructed is same as polyroots with the nuance.
cubic takes the vector [3] ... polyroots takes [4]
The nuance is that if the last v[4 is not '1' then the plot is scaled
to that factor... otherwise: if v[4 is 1 same roots.
3. At this point of your project, you have reduced the long arguments
by sol_1(x), sol_2(x). At this next point, if you want either/both functions
to pass a cubic polynomial through, you define a set of 4 points that
make sense wrt the nature of your expectation, then solve for the coefficients of the cubic polynomial.
Doing so, no needs for cubic, neither polyroots
neither for symbolic of any kind, neither partial fraction expansion.
................
The attached linfit module does all that, no sweat/quick,
infinitely re-usable for each set of new experimental XY
What it returns is simply your 4 set of the resulting design values
that produce your experimental demand
BTW, linfit is unlimited in the order of polynomial fit.
Cheers ... Jean.
Cubic solver QUICK Explorer.sm (49 КиБ) скачан 39 раз(а).
I am failing to see how I can use this in my project. I guess using polyroots and the neat trick to filter the relevant roots has solved my problem.
Best regards,
My last understanding was that you wanted to design some new sol_1/2(x)
based on experimental data set. That's what the attached achieves.
You could do similar by playing the Polyroots coefficients. But, you won't
see how it construct from experiment. After all, those demo experiment
they could come from data measurements.
Plug your proposal in the attached document, for better understanding
and possibly more towards a conclusion.
Cheers ... Jean
cubic Experiment User.sm (83 КиБ) скачан 30 раз(а).
cubic PPolyroots ChebyShev.sm (9 КиБ) скачан 32 раз(а).
Wrote
Re 3, I think I worded myself poorly. Numeric evaluation of algebraic equations are fine by me, I just didnt want to code with a numeric tool like solve () that just checks all the values wihtin it's range to find the roots.
I guess there is no reason to use my own code to solve polynomials when SMath has polyroots. The cubic solver from my last post was something I made before I discovered polyroots.
Don't worry. I know that symbolic solver for 3er degree it's very hard to implement in SMath just because I try it for get eigenvalues and the Jordan canonical form of a matrix. Obviously, only after I fail before with the 4th degree.
Wrote
Re 4, does this look like what you would have made to serve this purpose?
Yep. It looks very well as a filter for the numerical roots given a real interval. Just only one very small "best practices" recommendation: notice that if you have defined roots.filtered as an array with more than n elements before your routine then it take roots.filtered as a global variable an preserve the last elements. A way to avoid that is defining roots.filtered:=0 in the body of the procedure, probably just after n:=0.
As a side effect you can handle the case where there are not real roots in your interval asking if roots.filtered it's equal to zero, and then do nothing if it's true, and sorting it in the else case, just for prevent an error calling sort() of an scalar or an undefined variable. Finally, you can use the same question for check the result of your entire procedure. An usual technique in Mathcad was redefining rows() and cols() for return zero for scalars, but this doesn't works in SMath, so storing zero also can help for handle that case anytime that you call your function.
Best regards.
Alvaro.
Wrote... more and more confusing.
My last understanding was that you wanted to design some new sol_1/2(x)
based on experimental data set. That's what the attached achieves.
You could do similar by playing the Polyroots coefficients. But, you won't
see how it construct from experiment. After all, those demo experiment
they could come from data measurements.
Plug your proposal in the attached document, for better understanding
and possibly more towards a conclusion.
Cheers ... Jean
cubic Experiment User.sm (83 КиБ) скачан 30 раз(а).
Jean,
I just wanted a reliable way to find one (or two, depending on some of the input) root(s) of the cubic θ(x). I had problems with polyroots switching the order of the solutionvector, so I needed something else to make it reliable.
Best regards,
WroteHi hanskl.
Don't worry. I know that symbolic solver for 3er degree it's very hard to implement in SMath just because I try it for get eigenvalues and the Jordan canonical form of a matrix. Obviously, only after I fail before with the 4th degree.
Yep. It looks very well as a filter for the numerical roots given a real interval. Just only one very small "best practices" recommendation: notice that if you have defined roots.filtered as an array with more than n elements before your routine then it take roots.filtered as a global variable an preserve the last elements. A way to avoid that is defining roots.filtered:=0 in the body of the procedure, probably just after n:=0.
As a side effect you can handle the case where there are not real roots in your interval asking if roots.filtered it's equal to zero, and then do nothing if it's true, and sorting it in the else case, just for prevent an error calling sort() of an scalar or an undefined variable. Finally, you can use the same question for check the result of your entire procedure. An usual technique in Mathcad was redefining rows() and cols() for return zero for scalars, but this doesn't works in SMath, so storing zero also can help for handle that case anytime that you call your function.
Best regards.
Alvaro.
Alvaro,
Thanks for the tips for "best practices", they are very helpful and much appreciated.
I didnt understand what you meant in the part I highlighted.
RealPolyRootsRange.sm (9 КиБ) скачан 36 раз(а).
Best regards,
Wrote
cubicroots.sm (9 КиБ) скачан 33 раз(а).
Notice that this output could be an scalar representing only one root for a degenerate polynomial or a vector with two or three values. If you goes to check the number of outputs with rows() you could get an error in the case of scalar. To avoid that kind of errors is that I say that in Mathcad one can redefine rows() and cols() for return zero for scalars. Also, SMath doesn't have functions like IsScalar() nor IsMatrix().
Now, you return an string if there are not solutions in the interval. Thus the outputs of your function could be a vector or a string. That's could be not the best option, I guess that best is return zero.
Check for example the outputs of findrows(): a vector or the scalar zero. Or findstring(): a vector or the scalar -1. Both have an answer for the empty output and then you can test before use rows() for get the number of "solutions" to avoid the error rows() of an scalar, and go further in you calculations handling the empty cases.
Best regards.
Alvaro.
WroteHi hanskl. For example, check the outputs of this procedure
Notice that this output could be an scalar representing only one root for a degenerate polynomial or a vector with two or three values. If you goes to check the number of outputs with rows() you could get an error in the case of scalar. To avoid that kind of errors is that I say that in Mathcad one can redefine rows() and cols() for return zero for scalars. Also, SMath doesn't have functions like IsScalar() nor IsMatrix().
Now, you return an string if there are not solutions in the interval. Thus the outputs of your function could be a vector or a string. That's could be not the best option, I guess that best is return zero.
Check for example the outputs of findrows(): a vector or the scalar zero. Or findstring(): a vector or the scalar -1. Both have an answer for the empty output and then you can test before use rows() for get the number of "solutions" to avoid the error rows() of an scalar, and go further in you calculations handling the empty cases.
Best regards.
Alvaro.
Alvaro,
I now understand the problem. But polyroots seems to always return a vector (even when the length = 1), so the only instance where use of rows() will give me problems is if the argument "c" is a scalar? That will also give polyroots trouble?
Best regards,
The filter can result in none roots, one two or theee.
Best regards
Álvaro.
WroteHi. Im talking about the otput of the filter not the output of polyroots.
The filter can result in none roots, one two or theee.
Best regards
Álvaro.
Just to make sure I understand, I should replace "No real roots within the range" with "0" if in fact the filter returns zero roots?
That would also work better if I wanted to apply units to my solution. If the function returns a string I would get an error if I apply a unit.
Thanks for your patience and helpful insight.
Best regards,
WroteHowever, since the roots of a polynomial are usually calculated as the eigenvalues of the companion matrix, and numerical procedures usually order these eigenvalues according to their magnitude, which is a real number, since it does have applications to keep the large eigenvalues and neglect the small ones, perhaps that is the order in which the roots appear in polyroots (): ordered by their complex magnitude, but I can't be sure, since I don't know the SMath routine for its calculation.
FYI I've quickly looked at the code and there isn't any post-processing on the order of the results given by the internal algorithm. About the imaginary parts of the roots of the polynomial, the code comment is: "The elements are numbered from 1 to N. In this case, the complex-conjugate roots go one after the other."
Wrote
FYI I've quickly looked at the code and there isn't any post-processing on the order of the results given by the internal algorithm. About the imaginary parts of the roots of the polynomial, the code comment is: "The elements are numbered from 1 to N. In this case, the complex-conjugate roots go one after the other."
Hi Davide. Good to know. Matlab, for example, calls eig() for it's roots() function ( you can see that at https://octave-online.net/ with "> type roots" ). And from https://www.mathworks.com/help/matlab/ref/eig.html you can read: "By default eig does not always return the eigenvalues and eigenvectors in sorted order. Use the sort function to put the eigenvalues in ascending order and reorder the corresponding eigenvectors."
For an optimal procedure in the sense of minimizing roundoff errors you can see Numerical Procedures in C at https://www.cec.uchile.cl/cinetica/pcordero/MC_libros/NumericalRecipesinC.pdf page 207 of the pdf.
Best regards.
Alvaro.
Wrote
Just to make sure I understand, I should replace "No real roots within the range" with "0" if in fact the filter returns zero roots?
Hi. Not mandatory, I only show two examples of how SMath handle empty cases. Usually string as a message is for error messages and not for the output of a function. In this case zero it's a good option because the other cases are always a vector. BTW in Mathcad and for some operations in SMath arrays of size 1x1 are converted to scalars ... and then you can't distinguish between a root with zero value or an empty vector. Knowing the output for the special cases you can handle them. Another point is that unfortunately there are no way to show warning messages.
Best regards.
Alvaro.
Wrote
Hi. Not mandatory, I only show two examples of how SMath handle empty cases. Usually string as a message is for error messages and not for the output of a function. In this case zero it's a good option because the other cases are always a vector. BTW in Mathcad and for some operations in SMath arrays of size 1x1 are converted to scalars ... and then you can't distinguish between a root with zero value or an empty vector. Knowing the output for the special cases you can handle them. Another point is that unfortunately there are no way to show warning messages.
Best regards.
Alvaro.
Thank you, that makes sense.
Best regards,
WroteFYI I've quickly looked at the code and there isn't any post-processing on the order of the results given by the internal algorithm. About the imaginary parts of the roots of the polynomial, the code comment is: "The elements are numbered from 1 to N. In this case, the complex-conjugate roots go one after the other."
I'm not sure I understand your post correctly, but I have seems to me polyroots() are doing some sort of sorting/ordering of the solutionvector. Have a look at this sheet comparing polyroots with a cubic solver.
Best regards,
RealPolyRootsRange.sm (20 КиБ) скачан 37 раз(а).
WroteI'm not sure I understand your post correctly, but I have seems to me polyroots() are doing some sort of sorting/ordering of the solution vector. Have a look at this sheet comparing polyroots with a cubic solver.
The Laguerre Polyroots code is universal for purpose.
Your cubic root is incorrect because your polynomial does not follow
again the Bible code Lagrange ... it should be d+c*x+b*x^2+a*x^3
Polyroots is best exemplified by finding the roots of orthogonal Polynomials
Name them: Legendre ChebyShev, Hermite, Radau, Laguerre ...
They come in alternate [±] order biggest first smaller in descending order.
This is the way you can sanity Abramowitz and Stegun, thus the way to proceed
Legendre discrete integration.
Cheers ... Jean
RealPolyRootsRange OTHERWISE_Comment.sm (76 КиБ) скачан 34 раз(а).
to canonical Lagrange in the attached not done, just freaked.
Cheers ... Jean
Файл не найден.Файл не найден.
Thank you, I will reverse the order of the polynomial coefficients.
In my limited maths classes on school we were taught polynomials was supposed to be written on the form f(x)=ax2 + bx + c with descending powers. I was wondering why polyroots() needed the coefficients in the opposite order.
Best regards,
Wrote... if you wish [good suggestion] adapt your module code
to canonical Lagrange in the attached not done, just freaked.
Cheers ... Jean
Файл не найден.Файл не найден.
Jean,
I got some strange results with your cubic solver, see the attachment.
Best regards,
complex residuals.sm (27 КиБ) скачан 37 раз(а).
-
Новые сообщения
-
Нет новых сообщений