MATLAB Answers

# 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?

3 views (last 30 days)
Lewis Harrington on 18 Nov 2019
Commented: Lewis Harrington on 18 Nov 2019
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

Sign in to comment.

### 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.

#### 1 Comment

Lewis Harrington on 18 Nov 2019
How do i add the element into an array for each loop?

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!

Translated by