Need help plotting to different plots in the same figure from a while loop. The program calculates the results for a square root using the Newton method but it doesn't plot it.
1 次查看(过去 30 天)
显示 更早的评论
a= input('Enter a positive number:');
ti= input('Percent of tolerance wanted:');
if a<0
disp('Number bigger than cero');
elseif ti<0
ti=ti*100;
end
k=1;
x=a/2;
t=101;
figure; hold on
while t>ti
k=k+1; %Interaction numbers
i=x; %Safes x
x=((x+(a/x))/2); %Estimates square root
t=((abs(x-i)/x)*100); %Tolerance
subplot(2,1,1); plot(k,x);
subplot(2,1,2); plot(k,t);
end
disp (['The square root is ', num2str(a), ' after ', num2str(k), ' interactions is:', num2str(x)]);
0 个评论
采纳的回答
G A
2012-3-9
...
figure;
clf
while t>ti
k=k+1; %Interaction numbers
i=x; %Safes x
x=((x+(a/x))/2); %Estimates square root
t=((abs(x-i)/x)*100); %Tolerance
subplot(2,1,1);
hold on
plot(k,x,'.');
subplot(2,1,2);
hold on
plot(k,t,'.');
end
hold off
disp (['The square root is ', num2str(a), ' after ', num2str(k), ' interactions is:', num2str(x)]);
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!