Skip to content

Instantly share code, notes, and snippets.

@stevenlr
Created March 8, 2012 21:57
Show Gist options
  • Save stevenlr/2003711 to your computer and use it in GitHub Desktop.
Save stevenlr/2003711 to your computer and use it in GitHub Desktop.
------ AVANT -------
Dichotomie := proc(expr, m_a, m_b, e)
local f, a, b, r, n_steps;
a := m_a;
b := m_b;
n_steps := 0;
f := unapply(expr, x);
while abs(a - b) > e do
r := f((a + b) / 2);
if r >= 0 then
b := (a + b) / 2;
else
a := (a + b) / 2;
fi;
n_steps := n_steps + 1;
od;
RETURN([a, b], n_steps);
end;
------ APRES -------
Dichotomie := proc(expr,m_a,m_b,e)
local f,a,b,r,n_steps;
a := m_a;
b := m_b;
n_steps := 0;
f := unapply(expr,x);
while abs(a-b)>e
r := f((a+b)/2);
if r>=0 then
b := (a+b)/2;
else
a := (a+b)/2;
fi;
n_steps := n_steps+1;
od;
RETURN([a,b],n_steps);
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment