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)])
0 个评论
回答(1 个)
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!