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)]);

采纳的回答

G A
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 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by