Created
September 29, 2015 16:48
-
-
Save normalhuman/2c1c7918b4305f716fd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function adaptivePL | |
t = linspace(0, 7, 1000); % the default number of points, 100, may be not enough here | |
points = refine(t(1), t(end)); % determine the nodes of interpolation | |
plot(points, f(points), 'r-*') % piecewise linear plot with asterisks for data points | |
hold on | |
plot(t, f(t)) % the original function for comparison | |
end | |
function points = refine(x1,x2) | |
xm = (x1+x2)/2; | |
if abs((f(x1)+f(x2))/2-f(xm)) < .005 % decide whether to subdivide | |
points = [x1, x2]; % decided not to | |
else | |
left = refine(x1,xm); % decided to divide | |
right = refine(xm,x2); | |
points = [left, right(2:end)]; % merge without duplication | |
end | |
end | |
function y = f(x) % used f as named function, so it can be accessed from any other function | |
y = exp(-x/4).*sin(x.^2+6); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment