Skip to content

Instantly share code, notes, and snippets.

@d3ep4k
Created April 13, 2024 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d3ep4k/272ea7444f2b003c0cb98cbd6f5634fa to your computer and use it in GitHub Desktop.
Save d3ep4k/272ea7444f2b003c0cb98cbd6f5634fa to your computer and use it in GitHub Desktop.
​def f(x){ // we are taking equation as x^3+x-1
def f = 2*x + x - 1;
return f;
}
def secant(x1, x2, E) {
def n = 0, xm, x0, c;
while(true){
// calculate the intermediate value
x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
// check if x0 is root of equation or not
c = f(x1) * f(x0);
// update the value of interval
x1 = x2;
x2 = x0;
// update number of iteration
n++;
// if x0 is the root of equation then break the loop
if (c == 0)
break;
xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
if(Math.abs(xm - x0) >= E) continue;
else break;
}
// until the convergence
return x0.round(6)
//document.write("Root of the given equation=" + x0.toFixed(6));
//document.write("No. of iterations = " + n);
}
response = [];
response["transit"] = secant(0, 10, 0.000001);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment