FzeroDemo.m
Contents
Overview
Illustrates the NCM routine fzerotx (textbook version of fzero) to solve on the interval .
Find a root of the Bessel function
We use the built-in MATLAB function besselj to solve
Note that the routine fzero (Brent's method) requires an initial interval. Here we use
Note how fzerotx.m needs functions as input arguments.
disp('Find the first positive root of the Bessel function J0:') J0 = @(x) besselj(0,x); disp('Initial interval is [a,b] = [0,3]') x = fzerotx(J0,[0,3]); disp(sprintf('The first positive root of J0 is x = %f.\n',x))
Find the first positive root of the Bessel function J0: Initial interval is [a,b] = [0,3] The first positive root of J0 is x = 2.404826.
Compute
We solve the equation
where we use the initial interval
pause disp('Compute sqrt(2):') sqr2 = @(x) x^2-2; disp('Initial interval is [a,b] = [0,2]') x = fzerotx(sqr2,[0,2]); disp(sprintf('sqrt(2) = %f.',x))
Compute sqrt(2): Initial interval is [a,b] = [0,2] sqrt(2) = 1.414214.