### Translated by

이 페이지의 내용은 영어에서 자동 번역되었습니다. 자동 번역 기능을 해제하려면 "<a class="turn_off_mt" href="#">여기</a>"를 클릭하십시오.

## I can't delete plot and line in a figure

### yoonseong hwang (view profile)

on 3 Apr 2019
Latest activity Edited by dpb

on 3 Apr 2019

### dpb (view profile)

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?

on 3 Apr 2019
Edited by dpb

### dpb (view profile)

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???