I can't delete plot and line in a figure

조회 수: 4 (최근 30일)
yoonseong hwang
yoonseong hwang 2019년 4월 3일
편집: dpb 2019년 4월 3일
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?

채택된 답변

dpb
dpb 2019년 4월 3일
편집: dpb 2019년 4월 3일
...
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???

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by