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 view (last 30 days)
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)]);

Accepted Answer

G A
G A on 9 Mar 2012
...
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)]);

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!