Finding the Min/Max of Polynomials

I’m trying to code a quadratic equation with the ability to find the min and max at the same time within a given range. Later these points can be used to start adding structure too but I’m having trouble getting the code started. Eventually I’d like to find min/max of a series of sin waves but will start will quadratic equations first. :slight_smile: Thanks,

def quadmin(a, b, c, xmin, xmax);

q0 = (a * xmin + b) * xmin + c;
q2 = (a * xmin + b) * xmax + c;
x1 = -b/(2*a);

if (xmin < x1 ) and (x1 < xmax);
q1 = (a * x1 + b) * x1 +c;
return min(q0, q1, q2);
else
return min(q0, q2);

Hello @PerfectArchCo,

Do you want to write the code in DesignScript or python?

I recommend you to start with the Dynamo language guide (DesignScript) and try to get the formatting correct. Any ifs and loops have to be innside a [Imperative] block.

Hmmm. Definately designscript but I will try to add the impetive block. Forgot about that.

You need to use curly brackets in your function, and rember the equal sign after return:

def getTimesTwo(arg)
{
return = arg * 2;
}

and is &&:

result = true && false;

2 Likes

1 Like

Thanks. I’l try to run these changes through at lunch. Looks promising. I saw those Min/Max Itemkeys nodes and was wondering what they do. Would you recommend them in an application like this?

After working through some of the changes suggested I guess I should have been more specific since I’ve known what I’m aiming for since the beginning. I guess what I’m trying to find are all the inflection points where the curve changes direction. Normally this is done by simply finding where the slope is zero but implimenting calculus in Dynamo continues to be a struggle for me.

The derivatives node seems to only work with surfaces and I can’t think of a good way to use the Tangent at Parameter node (line.bytangent) to search the polynomical. How would others approach this?

Here is a suggestion for you.

  • Get the Z values for the points along the curve
  • Calculate a delta beween the points (Zn - Z(n-1))
  • Find two delta values in a row that changes sign ( + * - = -)

1 Like

This is a little old but should get you started:

Hmmm. Things didn’t go very smoothly when I was updating the code today with your suggestions and the website help but I’m pretty sure it’s on my end. I’ll keep working on it and follow up later. I guess calculating out the finite differences will be the route to go. But I don’t understand the “Find two delta values in a row that changes sign ( + * - = -)” works out to the slope.

Not very well explained, sorry about that.

If you scroll through a list of z values, you can see where the values goes from increasing to decreasing and vice versa.

In the list, if you take one value and subtract it from the next value you will get a positive or negative number. Positive = increasing, negative =decreasing.

If you then multiply those numbers, you will get positive sign if a) both are increasing, b) both are decreasing. If one is increasing and the other decreasing you get negative sign. That is where you have your local max and min points.

Hope that helps. (Did you try to build the script from the screenshot in my last post?)

1 Like

Thanks for the update. I did print off the screenshot. Evidently my two work monitors are not enough sometimes. I just need to tweek it tomorrow at lunch. It’s computationally heavy but will work once I properly code it I think.

Just an update before the weekend: I worked out two ways to find approximate Min/Max. @Einar_Raknes 's Finite Difference method worked really well (red). I kept studying the problem and also tried to find the Min/Max using the 1st derivative (green). (I’m still trying to work out if the Ncalc Formula node has this capablity built-in?) In anycase, hopefully everyone can see the codeblocks and offer some suggestions on how to improve it. It’s pretty computationally heavy at the moment. The reason I’m so focused on finding the exact Min/Max points is because thoses are the points where the most interesting geometeric things happen. Ideally we’d like to see the tangent lines as close to horizontal as possible. Thanks.

1 Like

Hopefully everyone can see the image. After doing some further checks on the code this afternoon, as the number of parameters rises the finite difference method (red) does an admirable job of finding the min max points. My first derivative approach (green), which should do better at directly finding the min/max, is haddicapped as the slope numerically approuches zero and passes close values along with the closest value. I don’t know a good way to filter values as they approuch zero if anyone has any suggestions?

1 Like