Solving matlab problems BAKEGRUND OF THE QUESTION The square root of a positive
ID: 2268629 • Letter: S
Question
Solving matlab problems
BAKEGRUND OF THE QUESTION
The square root of a positive real number S can be calculated iteratively as follows
In this case, we get six correct decimals after five iterations. The sixth iteration confirms that we are done.
QUESTION 1
QUESTION 2
Change your script so that it does not make a huge number of iterations (ten in question 1), but calculate new values until the series converges. Allow the script to print the number of iterations needed. Use the following convergence criteria:
Tips:You need two x-values for the iteration: one variable for the current x value, and one for the previous one. Select their starting values in such a way that the while rate is not interrupted initially.
.........................................................................................................................................................................................................................................................................
Please write a short note that explain the code you write in matlab so I can understand the code also, thanks in advance!
(Tk + S/1*) Tk+1 =Explanation / Answer
a) Check for the stepwise solution and find the explaination in comments:
%Taking the input from the user.
S = input('Enter the number whose square root is to be calculated: ');
%Format command for improving the precision of the data computed
format long;
%Calculating and printing the zeroth approximation by dividing the actual
%number by two
%Data printed with the help of fprintf command
y = S/2;
fprintf('x0 = %d ',y);
%Running a 'for' loop for finding the first ten approximation
for t = 1:10
%Replacing the last value of y over the new value according to the
%algorithm stated.
y = (y + (S/y))/2;
%for printing the result
fprintf('x%d = %d ',t,y);
end
%End of routine
Test run of code:
Enter the number whose square root is to be calculated: 98
x0 = 49
x1 = 2.550000e+01
x2 = 1.467157e+01
x3 = 1.067558e+01
x4 = 9.927704e+00
x5 = 9.899535e+00
x6 = 9.899495e+00
x7 = 9.899495e+00
x8 = 9.899495e+00
x9 = 9.899495e+00
x10 = 9.899495e+00
b)
%Taking the input from the user.
S = input('Enter the number whose square root is to be calculated: ');
%Format command for improving the precision of the data computed
format longg;
%Calculating the zeroth approximation by dividing the actual
%number by two
y = S/2;
%Running a 'for' loop for finding the first ten approximation
for t = 1:100000
%taking the old value in the new variable say dummy
dummy = y;
%Replacing the last value of y over the new value according to the
%algorithm stated.
y = (y + (S/y))/2;
%Applying the condtion of convergence as given in question as condtion of
%the if statement
if abs(dummy-y)< 10^-6
%as soon as condtion is satisfied break from the loop
break
end
end
%for printing the number of iteration required minus one as in the loop
%it was incremented one before the convergence is obtained
fprintf('Number of iterations required: %d ',t-1);
%end of program
OUTPUT:
Enter the number whose square root is to be calculated: 1598
Number of iterations required: 8
Comment in case of any doubts. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.