I can't delete plot and line in a figure

3 Ansichten (letzte 30 Tage)
yoonseong hwang
yoonseong hwang am 3 Apr. 2019
Bearbeitet: dpb am 3 Apr. 2019
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?

Akzeptierte Antwort

dpb
dpb am 3 Apr. 2019
Bearbeitet: dpb am 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???

Weitere Antworten (0)

Kategorien

Mehr zu Specifying Target for Graphics Output finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by