I'm having trouble with a javascript app I am currently working on. It's a boat design thing used to calculate hydrostatics data of yacht hulls. The trouble I'm having is that I have two functions that are interrelated and both need to arrive at a specific result.
To clarify, the user inputs a design displacement and the hull is moved up or down until the immersed volume equals the design displacement.
The user also inputs a longitudinal position for the center of gravity, and the hull is pitched until the longitudinal position of the center of buoyancy (centroid of the immersed volume) aligns with the center of gravity.
However, pitching the hull will change the displacement, and moving the hull up or down will change the position of the center of buoyancy.
What I currently do (pseudocode)
function raiseAndPitch
while a < maxIterations
if boat.volume < designVolume
move boat down
else if boat.volume > designVolume
move boat up
else
done
a++
while b < maxIterations
if boat.cg.x > designCG.x
pitch boat forward
else if boat.cg.x < designCG.x
pitch boat aft
else
done
b++
if boat.cg.x == designCG.x && boat.volume == designVolume
return data
else
raiseAndPitch
I have a maximum number of recursions in place. However this sometimes fails to converge even with a large number of recursions for both parameters when a very small movement in one or the other creates a big change in either displacement volume or center of buoyancy position.
Not shown in the pseudocode is the way I converge on the values, please indicate if it's relevant to my question and I will add it.
Is there a better way to do this that would allow me to make sure that both parameters get to the required values at the same time ?