Dear forum(adsbygoogle = window.adsbygoogle || []).push({});

Please help - I am feeling very useless and depressed as something which worked years ago while at uni now seems to beat me !!

I am trying to write a simple script (javascript actually) which calculates the root of a quadratic given two guesses of x (one guess giving y<0, the other giving y>0). From these values of x1 and x2 it calulates y at the mid-point, and depending on whether y is positive or negative it moves one of the boundaries half-way towards the midpoint.

I will paste the code below. The program runs but doesn't converge:

<html>

<body>

<script type="text/javascript">

document.write("Solving the equation : 3x^2-5=0");

x1 = window.prompt ("Enter a VERY LOW initial guess for x");

x2 = window.prompt ("Now enter a VERY HIGH initial guess for x");

x=(x1+x2)/2;

y=3*x^2-5;

document.write(y);

while (Math.abs(y) >= 0.01)

{

if(y<0)

{x1=(x+x1)/2;

}

else if(y>0)

{x2=(x+x2)/2;

}

x=(x1+x2)/2;

y=3*x^2-5;

document.write(x1);

document.write(x2);

document.write(y);

}

</script>

</body>

</html>

I realise there are probably better ways of acheiving the same thing, but I'm sure this (or something very similar) used to work.

Thank you in anticipation

Matt Klein

**Physics Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Script to solve quadratic - help !

Loading...

Similar Threads - Script solve quadratic | Date |
---|---|

I Solving integral | Feb 9, 2018 |

I Understanding an integral | Jan 31, 2018 |

B Solve for x (natural log equation) | Dec 6, 2017 |

I Solving this integral equation | Nov 3, 2017 |

B Help with understanding Nature of Roots for Quadratic and Cu | Oct 23, 2016 |

**Physics Forums - The Fusion of Science and Community**