Parametrize iteration count for Newton's method

This commit is contained in:
Lucas 2020-03-08 22:33:18 +00:00
parent a6265017d1
commit 2b9b56737a
3 changed files with 16 additions and 11 deletions

View File

@ -17,18 +17,17 @@
#include "criticals.h"
#include "util.h"
size_t newton_iters = 100;
static size_t idx = 0;
static long double complex criticals[100];
int
behaves_critical(long double complex (*df)(long double complex),
long double complex (*ddf)(long double complex), long double complex z,
long double complex *crit, long double bailout, long double epsilon)
long double complex *crit, long niters, long double bailout,
long double epsilon)
{
size_t i;
for (i = 0; i < newton_iters && cabsl(z) < bailout; i++)
long i;
for (i = 0; i < niters && cabsl(z) < bailout; i++)
z = z - df(z) / ddf(z);
if (cabsl(df(z)) < epsilon) {

View File

@ -15,7 +15,7 @@
int behaves_critical(long double complex (*)(long double complex),
long double complex (*)(long double complex), long double complex,
long double complex *, long double, long double);
long double complex *, long, long double, long double);
void add_critical_point(long double complex);
size_t ncriticals(void);
long double complex get_critical(size_t);

16
julia.c
View File

@ -28,12 +28,14 @@
#include "fun.h"
#define OPTSTRING "a:b:c:D:d:h:Ll:N:n:w:x:y:z:"
struct color contour_color = { 0 };
static long double bailout = 1e100,
epsilon = 1e-8,
sec_w = 4.0,
contour = 0.0005L;
static long niters = 250;
static long niters = 250, newton_iters = 100;
static void
print_critical_points(void)
@ -103,7 +105,8 @@ usage(void)
fprintf(stderr, "Usage: %s julia [-L] [-x julia_x] [-y julia_y]\n"
" [-n niters] [-h height] [-w width] [-z zoom]\n"
" [-a center_x] [-b center_y] [-l light_intensity]\n"
" [-d density] [-c contour] [-D displacement]\n",
" [-d density] [-c contour] [-D displacement]\n"
" [-N newton_iters]\n",
xgetprogname());
exit(1);
}
@ -134,7 +137,7 @@ julia_main(int argc, char *argv[])
i, j;
int ch, do_light = 0;
while ((ch = getopt(argc, argv, "a:b:c:D:d:h:Ll:n:w:x:y:z:")) != -1)
while ((ch = getopt(argc, argv, OPTSTRING)) != -1)
switch (ch) {
case 'a':
cen_x = parse_double(optarg);
@ -160,6 +163,9 @@ julia_main(int argc, char *argv[])
case 'l':
light_intensity = parse_double(optarg);
break;
case 'N':
newton_iters = parse_integer(optarg, 1, LONG_MAX);
break;
case 'n':
niters = parse_integer(optarg, 1, LONG_MAX);
break;
@ -205,8 +211,8 @@ julia_main(int argc, char *argv[])
for (j = 0; j < width; j++) {
zx = cen_x + (j / (long double)width - 0.5L) * sec_w;
z = zx + I * zy;
if (behaves_critical(df, ddf, z, &crit, bailout,
epsilon))
if (behaves_critical(df, ddf, z, &crit, newton_iters,
bailout, epsilon))
add_critical_point(crit);
}
}