MATLAB Answers

0

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?

Asked by Lewis Harrington on 18 Nov 2019 at 11:03
Latest activity Commented on by Lewis Harrington on 18 Nov 2019 at 17:09
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.

1 Answer

Answer by KALYAN ACHARJYA on 18 Nov 2019 at 11:11
Edited by KALYAN ACHARJYA on 18 Nov 2019 at 11:14

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

How do i add the element into an array for each loop?

Sign in to comment.