Numerical hw1
Numerical hw1
Homework #1
Due: March 28, 2024
Root finding
Write your own code called hw1 that finds all the roots between 0 and 10 of any function. Use
any method or a combination of methods to find all the roots with 0.01% accuracy or less.
- Put any other subfunctions, if any, in a single file entitled hw1.m, and submit your code only.
- Feel free to start with the following skeleton, but you need to modify it to find all.
- Extensions get extra credits. For example, efficiency, rich outputs, etc.
% main loop
for i = 1:maxIter
% stopping criterion
if ea < es
return
end
x0 = x; % update info
end
xl = x0(1); xu = x0(2);
x = (xl + xu)/2;
% main loop
for i = 1:maxIter
if phantom(xl)*phantom(x) < 0
xu = x;
elseif phantom(xl)*phantom(x) > 0
xl = x;
else
return
end
x = (xl + xu)/2;
ea = norm((xu-xl)/(xu+xl))*100; % convergence check
% stopping criterion
if ea < es
return
end
end