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 Center および File ExchangeSpecifying 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