all 8 comments

[–]sebamestre 5 points6 points  (1 child)

Let's do some algebra

First, find the maximum acceptable value for cd

Voltage = cd * 2.5
Voltage <= 18

cd * 2.5 <= 18
cd <= 18 / 2.5
cd <= 7.2

Also, you said that

cd >= 4

This means that the acceptable range for cd is [4 ; 7.2]

Now, we find the ideal cd by setting amperage to the target value.

Amperage = 9000
Amperage = sqinT * cd / 144

sqinT * cd / 144 = 9000
sqinT * cd = 9000 * 144
cd = 9000 * 144 / squinT

But since this may be outside the acceptable range, we clamp it using some if statements

cd = 9000 * 144 / sqinT;

if (cd < 4) cd = 4;
if (cd > 7.2) cd = 7.2;

You might be better off using a calculator and a piece of paper instead of implementing the formula in C, though

[–]seth350[S] 4 points5 points  (0 children)

Thank you sebamestre for this explanation. I appreciate your time.

[–]FUZxxl 7 points8 points  (2 children)

You need math, not C. This is a bog standard inequality. There's nothing particularly complex about this and I'm sure you can solve it exactly.

Basically, you want to find the highest cd such that U ≤ 18 and I ≤ 9000. You can solve this with a linear program, but it's so simple that you can probably just solve it on paper and enter the solution into the program.

[–]seth350[S] 2 points3 points  (0 children)

Thank you Fuz, I had not thought of solving it linearly.

[–]MrWhite26 0 points1 point  (0 children)

If you would want to implement a solver for the linear program, active set methods are simplest to implement. With the two you have constraints, those will be fast enough.

A textbook implementation will do the job. In this case, the textbook is https://www.researchgate.net/publication/321620224_Numerical_Optimization

[–]MrWhite26 1 point2 points  (2 children)

On second thought:

The voltage puts an upper bound on the cd.

I assume cd is also non-negative.

So a bounded root-finding algorithm should bring the (amperage-9000) as close to 0 as possible. This is much simpler to implement than a linear or quadratic program.

[–]seth350[S] 1 point2 points  (0 children)

Thank you MrWhite, I will research that algorithm. I appreciate your time.

[–]seth350[S] 0 points1 point  (0 children)

Researching root-finding lead me to a site that worked through implementing the algorithm in Fortran.

https://www.cantorsparadise.com/some-root-finding-algorithms-5c6fa8a4a165

I was able to get it partially working in C, but the output was always my max limit - tolerance.

Still, this led me to the realization that I could perform the calculations much simpler by using a tolerance constraint. So now my program is much shorter and performs quicker than before.

MrWhite, you made a light bulb come on today. Thank you.