Problem #2 A numerical algorithm is used to solve the following equation The alg
ID: 3282490 • Letter: P
Question
Problem #2 A numerical algorithm is used to solve the following equation The algorithm starts with an initial guess x(1)-1 It can be seen that this is not the solution (f(1)--I # 0). An update term is then used to improve the solution. x(2) = x(1) + ??(1) v here ??(1) = and f'(x) = 3x2-2x This process will be repeated until If (x(N)ls 0.0001 (the solution has been found) or 1) Ax(N)I s 0.0001 (the solution cannot be improved). f (x(k)) x(k + 1) x(k) + ??(k) Write a Matlab script file for the following tasks (I) Use "while-end" loop to find the solution of the equation and the number of iteration required for finding the solution. (To make sure the program can stop even a s olution cannot be found please ensure the number of iteration is restricted to 50). (2) Find the solution using the command "izero" and compare the result with the result obtainedExplanation / Answer
MATLAB code
close all
clear
clc
f = @(x) (x^3 - x^2 - 1);
fd = @(x) (3*x^2 - 2*x);
max_iter = 50;
x = 1; % initial guess
iter = 0;
while 1
x_prev = x;
deltax = -double(f(x))/double(fd(x));
x = x + deltax;
iter = iter + 1;
if abs(double(f(x))) <= 0.0001 || abs(deltax) <= 0.0001 || iter == max_iter
break
end
end
fprintf('Number of iterations: %d ', iter);
fprintf('Solution: %.8f ', x);
fprintf('f(Solution): %.8f ', double(f(x)));
x = fzero(f,1);
fprintf(' Using ''fzero'': ');
fprintf('Solution: %.8f ', x);
fprintf('f(Solution): %.8f ', double(f(x)));
Output
Number of iterations: 5
Solution: 1.46557137
f(Solution): 0.00000050
Using 'fzero':
Solution: 1.46557123
f(Solution): 0.00000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.