I can't delete plot and line in a figure

3 views (last 30 days)
clc
clear all
close all
h = figure;
filename = 'Newton.gif';
f = @func;
df = @dfunc;
xr = 5;
maxit = 50;
es = 0.0001;
iter = 0;
fplot(f, [-10 10])
xlabel('x axis')
ylabel('y axis')
title('Newton Raphson Method Root Tracking')
axis tight manual
grid on
hold on
for i = 1:50
p1 = line([xr xr],[0 f(xr)]);
line([xr xr],[0 f(xr)])
fprintf("xr is ")
disp(xr)
fprintf("f(xr) is ")
disp(f(xr))
x = [-10 0.01 10];
y = (x-xr).*df(xr)+f(xr);
p2 = plot(x,y);
plot(x,y)
xrold = xr;
xr = xr - f(xr)/df(xr);
iter = iter + 1;
drawnow
frame = getframe(h);
img = frame2im(frame);
[imind, cm] = rgb2ind(img ,256);
if i==1
imwrite(imind,cm,filename, 'gif', 'Loopcount', inf);
else
imwrite(imind,cm,filename, 'gif', 'WriteMode', 'append');
end
if f(xr) ~= 0
ea = abs((xr - xrold)/xr) * 100 ;
elseif f(xr) <= es
break
end
if ea <= es || iter >= maxit
break
end
delete(p1)
delete(p2)
pause(0.2)
end
root = xr;
fprintf("Root is")
disp(root)
Hi. I made code to track root with newton raphson method,
I wanna delete the previous line of x=xr and tangent line of f(xr)
But the delete function didn't work.
What should I do?

Accepted Answer

dpb
dpb on 3 Apr 2019
Edited: dpb on 3 Apr 2019
...
p1 = line([xr xr],[0 f(xr)]);
line([xr xr],[0 f(xr)])
...
x = [-10 0.01 10];
y = (x-xr).*df(xr)+f(xr);
p2 = plot(x,y);
plot(x,y)
...
delete(p1)
delete(p2)
...
I'm sure that the two handles you delete are gone; problem is you drew the same line twice for each and didn't save a handle to the second rendering so it's still there...not sure why those two were there--probably leftovers from early attempts???

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!