-
Notifications
You must be signed in to change notification settings - Fork 0
/
testfindroot.m
55 lines (42 loc) · 1.97 KB
/
testfindroot.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
% testfindroot
% Here is a function to find the root of
desiredroots1 = [4, -1, -8]; % the roots of the function, where we
% intend for the positive root at +4 to be found.
polywithroots1 = poly(desiredroots1); % coefficients of a corresponding polynomial
% The function handle is constructed from the coefficients here:
fhandle1 = @(x) polyval(polywithroots1, x); % a function with a handle
% the function above maybe called with fhandle(x), and has the desired
% roots. It also a function handle, meaning it can be sent to another
% function without needing the @f.
x = -10:0.1:10; % take a look at the function
plot(x, fhandle1(x));
xlabel('x'); ylabel('fhandle1'); title('Function with multiple roots');
abstol = 1e-5; % a tolerance to test for root finding; make sure this is
% greater than the tolerance used by findroot
% Here is another function that can cause trouble for findroot.
% This has a root at zero, but will not converge
fhandle2 = @(x) x.^(1/3);
fhandle3 = @(x) [(x(1)-1); x(2).^3+1]; % roots at [1; -1]
%% Test case 1
x0 = 10; % an initial guess at the root
assert( abs(findroot(fhandle1, x0) - desiredroots1(1)) < abstol, ...
'Findroot does not satisfy tolerance');
%% Test case 2 "wrong" root
x0 = 0; % CHANGE this to a different number to cause wrong root found
assert( abs(findroot(fhandle1, x0) - desiredroots1(1)) >= abstol, ...
'Findroot found an unintended root');
assert( abs(findroot(fhandle1, x0) - desiredroots1(2)) < abstol, ...
'Findroot does not satisfy tolerance');
%% Test case 3 No convergence
% ADD a test findroot does not find correct root for fhandle2
x0 = 2;
desiredroots2 = 0;
[xstar3, cnvrg3] = findroot(fhandle2, x0);
assert( ~cnvrg3||~isreal(xstar3), ...
'Findroot converged');
%% Test case 4 Vector function
% ADD a test that demonstrates that vector fhandle3 does yield
% correct root
x0 = [2; 2]; %% ADD info here
assert( sqrt(sum(findroot(fhandle3, x0) - [1; -1]).^2)/2 < abstol, ...
'Vector Findroot does not satisfy tolerance')