Class demo on March 10

We'd like to find where this function is zero, numerically.

jimsFunc[x_]:=Sin[Exp[x]]-(x^2);

Plot[jimsFunc[x],{x,0,1}]

Here's how you'd write a simple program in Mathematica.

demoSimpleProgram[f_,low_,hi_]:=Block[{x},
Print["f[low] is " , f[low]];
Print["f[high] is " , f[hi]];
];

demoSimpleProgram[jimsFunc,0.8,0.9]

So let's use that sort of program to loop until we get close to the answer we want.

(*
 Find f[x] such that |f[x]|<epsilon,
a ≤ x ≤ b, where f[a], f[b] have opposite signs.
 *)
findZero[f_,a_,b_,epsilon_]:=Block[{x,value,aa,bb},
aa=a;
bb=b;
If[f[a]==0,Return[a]];
If[f[b]==0,Return[b]];
If[(f[a]>0 && f[b]>0)||(f[a]<0&&f[b]<0),Return["Error: both 'a' and 'b' have same sign."]];
While[True,
x = (aa+bb)/2;
If[Abs[aa-bb]<epsilon,Return[x]];
value = N[f[x]];
If[value*N[f[aa]]>0,aa=x,bb=x]
]
];

|

xZero=findZero[jimsFunc,0.8,0.9, 0.0001]

jimsFunc[xZero]

sinMinusCos[x_]:=Sin[x]-Cos[x];

sinMinusCos[0.3]

sinMinusCos[0.8]

findZero[sinMinusCos,0.3, 0.8, 0.00001]

sinMinusCos[%]