annotate findZeros.m @ 569:f1a01a48779c feature/grids/laplace_refactor

Close branch feature/grids/laplace_refactor
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 01 Sep 2017 10:58:07 +0200
parents 8368beb0d1b3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
306
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
1 % findZeros looks for solutions to the equation f(x)==0 within
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
2 % the limits lim with a granularity of h.
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
3 % Returns a sorted list of unique solutions.
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
4 function z = findZeros(f, lim, h)
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
5 n = ceil((lim(2)-lim(1))/h);
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
6 z0 = linspace(lim(1), lim(2), n);
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
7
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
8 z = zeros(1,n);
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
9
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
10 for i = 1:n
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
11 zt(i) = fzero(f, z0(i));
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
12 end
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
13
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
14 zt = sort(zt);
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
15
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
16 z = [];
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
17 for i = 1:n
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
18 if zt(i) < lim(1) || zt(i) > lim(2)
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
19 continue
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
20 end
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
21
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
22 if ~isempty(z) && abs(z(end) - zt(i)) < 1e-6
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
23 continue
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
24 end
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
25
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
26 z = [z zt(i)];
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
27 end
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
28
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
29 % z = unique(z);
8368beb0d1b3 Added function to calculate multiple solutions to an equation.
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
30 end