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 次查看(过去 30 天)
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)])

回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-11-18
编辑:KALYAN ACHARJYA 2019-11-18
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by