-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputeNewtonTrigGPU.m
36 lines (27 loc) · 963 Bytes
/
computeNewtonTrigGPU.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
function logCount = computeNewtonTrigGPU(xlim, numx, ylim, numy, maxIters)
% Compute the Newton's Method trig fractal using GPU arrayfun.
% Copyright 2019-2020 The Mathworks, Inc.
% Create the input arrays
x = gpuArray.linspace(xlim(1), xlim(2), numx);
y = gpuArray.linspace(ylim(1), ylim(2), numy);
[x,y] = meshgrid(x, y);
tolerance = sqrt(eps("double"));
% Calculate
logCount = arrayfun(@processElement, x, y, tolerance, maxIters);
% Gather the result back to the CPU
logCount = gather(logCount);
function logCount = processElement(x0, y0, tolerance, maxIterations)
% Evaluate the Burning Ship function for a single element
z = complex(x0, y0);
w = complex(Inf,0);
count = 0;
while (count < maxIterations) && (abs(z-w) > tolerance)
w = z;
z = z - f(z) ./ df(z);
count = count + 1;
end
logCount = log(count+1);
function z = f(x)
z = tan(sin(x)) - sin(tan(x));
function z = df(x)
z = cos(x)*(tan(sin(x))^2 + 1) - cos(tan(x))*(tan(x)^2 + 1);