Reputation: 3
I am a newbie and have just started trying to write my own app, my first one is a calculator, but i'm having trouble with the mathematical functions. I have a formula and I have implemented into a Javascript function, but no matter what I do. I can't seem to get the correct return value.
Here is the formula (used in a pharmaceutical conversion):
Methadone Fudin Factor (Developed by Dr. Jeffrey Fudin and Jason Andrew Fudin, B.Eng., MS) Formula when converting morphine to methadone:
Methadone mg = X/21 {5.7- 3*sin [90/((110/x)5 + 1)] - sin [90/((320/x)7 + 1)]}
X is the Input Parameter (Morphine quantity)
My code:
function MethadoneConvert (input){
console.log("Methadone Convert Input = "+input);
var drug;
var pre1 = 110/input;
var sin1 = Math.pow(110/input,5)+1;
var sin2 = 3*Math.sin(sin1);
var sin3 = Math.pow(320/input,7)+1;
var sin4 = 90/sin1;
var sin5 = 90/sin3;
var sin6 = Math.sin(sin4);
var sin7 = Math.sin(sin5);
var sin8 = 3*sin6;
input = input/21;
var input2 = 5.7-sin8-sin7;
drug = input*input2;
console.log("MethadoneConvert result = " + drug);
return drug;
}
30mg should return 8.1mg , at the moment its returning all the wrong numbers.
I have tried simpler variations of the code with less steps but it also didn't work, obviously my understanding of code to maths is very limited.
Would appreciate any help, this calculator is used to help doctors.
edit: var sin2 is redundant and is done at var sin8 instead , apologies for my obvious severe low understanding of implementing maths into programs, currently 30 for X returns 7.56 .
The returns also dive downwards towards 50 and comes back up at 60 which should't be happening, the correlation should be linear.
Upvotes: 0
Views: 81
Reputation: 33466
Use variables where it would make the equation easier to read.
Make sure to convert to radians before using trig functions.
Pay close attention to the order of operations.
function toRads(angle) {
return angle * (Math.PI / 180);
}
function MethadoneConvert(x) {
console.log("Methadone Convert Input =", x, "mg");
var sin1 = Math.sin(toRads(90 / (Math.pow(110 / x, 5) + 1)));
var sin2 = Math.sin(toRads(90 / (Math.pow(320 / x, 7) + 1)));
var output = x / 21 * (5.7 - 3 * sin1 - sin2);
console.log("Methadone Convert Output =", output.toFixed(1), "mg");
return output;
}
MethadoneConvert(30);
Upvotes: 1