I have this code but I want to add each of my values of xn into an array then plot these on a graph. How would i do this?
1 view (last 30 days)
Show older comments
a = input('Enter a value for a');
e = input('Enter a maximum error');
xn = input('Enter a starting value');
y=1;
while abs(y)>=e
xn=(xn+a./xn)./2;
y=xn.^2-a;
end
disp(['The estimated value of the square root of a is: ', num2str(xn)])
ra=sqrt(a);
disp(['The actual value of the square root of a is: ', num2str(ra)])
0 Comments
Answers (1)
KALYAN ACHARJYA
on 18 Nov 2019
Edited: KALYAN ACHARJYA
on 18 Nov 2019
Do array indexing inside while loop
likewise
% Define i=1;
% also define y(1) and xn(1)
while condition
xn(i)=(xn(i)+a/xn(i))./2;
y(i)=xn(i)^2-a;
i=i+1;
end
After that xn and y become vectors, do as you wish (plot).
Hopefully, these are enough clues to resolve the issue.
See Also
Categories
Find more on Loops and Conditional Statements 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!