How to find optimal values for this equation?

315 Views Asked by At

I have a question regarding solving values Period and Prescaller from the equation below

$$F_k = \frac{F_{mcu}}{(\text{Period} + 1) (\text{Prescaller} + 1)}$$

where $F_{mcu}$ and $F_k$ are known. Also, $F_{mcu} > F_k$. Period and Prescaller are 16 bit, they can take values from 0 to 65535. The point is to find values of Period and Prescaller so the equation will give as close value to known $F_k$ as possible. What would be the algorithm here that is more efficient than checking all possible values in a loop? I would appreciate all help.

1

There are 1 best solutions below

12
On

Let $x=Period$ and $y=Prescaller$. Your problem is equivalent to finding a feasible solution to \begin{cases} \frac{F_{mcu}}{F_k} = (x+1)(y+1) \\ x \le 65535 \\ y \le 65535 \\ x,y \in \mathbb{N} \end{cases}

You can solve this graphically. Draw a $65535$ x $65535$ grid, and the implicit function $\frac{F_{mcu}}{F_k} = (x+1)(y+1)$. Check if the curve passes through one of the points of the grid.

Note that the equation $\frac{F_{mcu}}{F_k} = (x+1)(y+1)$ is equivalent to $$ y= \frac{ \frac{F_{mcu}}{F_k} -1-x}{x+1} $$

so $$(x,y) = \left(0, \left \lfloor \frac{F_{mcu}}{F_k} -1 \right \rfloor \right)$$ is a feasible solution (provided that $\frac{F_{mcu}}{F_k} -1 \le 65535$) that should be close enough to the optimum (the $\left \lfloor \cdot \right \rfloor$ is the floor function).