Write a script in Matlab (or your favorite language) for computing the cube root
ID: 2264718 • Letter: W
Question
Write a script in Matlab (or your favorite language) for computing the cube root of a number, x=(a)-with only basic arithmetic operations using Newton's method, by finding the root of the function f(x)-z?-a. Run your program for a = 0,210. For each of these cases start with an initial guess reasonably close to the solution. stopping criterion, require the function value whose root you are searching to be smaller than E = 10-8. Print out the values ofXy and f(%) in each iteration. Comment on the convergence rates and explain how they match your expectations. As aExplanation / Answer
MATLAB script I've developed to implement the function f(x) using Newton's method:
clc;clear all;close all;
a=10;x(1)=2.5;
f=@(x) ((x.^3)-a);
g=@(x) (3.*(x.^2));
x(2)=(x(1))-(f(x(1))/g(x(1)));
e(1)=(f(x(2))-f(x(1)));
i=1;
while f(x(i))>(10^(-8))
x(i+1)=(x(i))-(f(x(i))/g(x(i)));
i=i+1;
end
The 'a' value and 'x(1)' (initial guess) value, can be changed for each case in the line-2 of given code, and initial guess is started with index of 1, as 0 is not a valid index in MATLAB and all the indices thus obtained from the results must be subtracted by 1, in order to obtain xk 's, ( which is however, the user's wish to change or not in the answer)
For a=0, I've selected x0 as x(1)=0.1, gives results of
xk ---------------------------------> f(xk)
0.100000000000000 -------->0.00100000000000000
0.0666666666666667------->0.000296296296296296
0.0444444444444445------->8.77914951989027e-05
0.0296296296296296------->2.60122948737489e-05
0.0197530864197531------->7.70734662925895e-06
0.0131687242798354------->2.28365826052117e-06
0.00877914951989026----->6.76639484598865e-07
0.00585276634659351----->2.00485773214478e-07
0.00390184423106234----->5.94031920635491e-08
0.00260122948737489----->1.76009457966072e-08
0.00173415299158326----->5.21509505084657e-09
Thus, after 11 iterations, we obtain as x10=0.00173415299158326=0.0017(approx) , as the solution to cube root of
0, accurate upto a E value of 10-8
For a=2, I've selected x0 as x(1)=1.5, gives results of
xk ---------------------------------> f(xk)
1.50000000000000---------->1.37500000000000
1.29629629629630---------->0.178275669359345
1.26093222474175---------->0.00481928579256641
1.25992186056593---------->3.86058273083023e-06
1.25992104989539---------->2.48379095069140e-12
Thus, after 5 iterations, we obtain as x4=1.25992104989539=1.2599(approx) , as the solution to cube root of 2, accurate upto a E value of 10-8
For a=10, I've selected x0 as x(1)=2.5, gives results of
xk ---------------------------------> f(xk)
2.50000000000000---------->5.62500000000000
2.20000000000000---------->0.648000000000003
2.15537190082645---------->0.0130561194336490
2.15443509749603---------->5.67384410210536e-06
2.15443469003196---------->1.07114317415835e-12
Thus, after 5 iterations, we obtain as x4=2.15443469003196=2.1544(approx) , as the solution to cube root of 10, accurate upto a E value of 10-8
The method converges fast, if you guess the initial value closer to the answer, If you select an initial guess which is far away from the answer, it take more iterations for the Newton's method to compute the result, For example, if I consider x0=20 for a=10, it would take 11 iterations to derive to the answer.
Thus, choosing the inital value as close to the answer is appreciable.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.