TopolMComplex

u/TopolMComplex


r/programminghelp•

im trying to calculate a solution for an equation in Wolfram Mathematica, the code is as following.

(*Define the function*) f[x_] := x*Sin[x] - 1

(*Define the Bisection Method function*)

BisectionMethod[f_, a_?NumericQ, b_?NumericQ, tol_?NumericQ] :=

Module[{c, fc}, While[Abs[b - a] > tol, c = (a + b)/2;

fc = f[c];

If[fc == 0, Return[c]];

If[Sign[f[a]] == Sign[fc], a = c, b = c]];

Return[(a + b)/2]]

(*Set the interval and tolerance*)

lowerBound = 0;

upperBound = 2;

tolerance = 0.0001;

(*Apply the Bisection Method*)

solution = BisectionMethod[f, lowerBound, upperBound, tolerance]

11