(MATLAB) The following question needs to be programmed in MATLAB . Please help i
ID: 3792149 • Letter: #
Question
(MATLAB) The following question needs to be programmed in MATLAB.
Please help in both 2 and 3. I think 3 shouldn't be hard once 2 is done. I am just not sure how to do double precision on Matlab. Thank you.
I AM IN NEED OF HELP FOR NUMBER 3 IN DOUBLE PRECISION. PLEASE DON'T DISREGARD THAT QUESTION.
Second picture contains part of #2.
MATLAB
2. Write a simple, self-contained program to apply Newton's method to the equation x 2 x 10x 20, starting with xo 2. Evaluate the appropriate f (x) and f'(x),Explanation / Answer
Answer of Part 2)
the Matlab code is...
f = x^3 + 2*x^2 + 10*x -20; % Equation definition
fp = 3*x^2 + 4*x +10; % First-order derivative of f
x0 = 2; % Initial guess
N = 50; % Maximum number of iterations
tol = 1E-6; % Convergence tolerance
x = zeros(N + 1,1); % Preallocate solution vector where row => iteration
x(1) = x0; % Set initial guess
% Newton's Method algorithm
n = 2;
nfinal = N + 1; % Store final iteration if tol is reached before N iterations
while (n <= N + 1)
fe = f(x(n - 1));
fpe = fp(x(n - 1));
x(n) = x(n - 1) - fe/fpe;
if (abs(fe) <= tol)
nfinal = n; % Store final iteration
break;
end
n = n + 1;
end
% Plot evolution of the solution
figure('Color','White')
plot(0:nfinal - 1,x(1:nfinal),'o-')
title('Newton''s Method Solution: $f(x) = x^3 + 2*x^2 + 10*x -20$','FontSize', 20,'Interpreter','latex')
xlabel('Iteration','FontSize',16)
ylabel('$x$','FontSize',16,'Interpreter','latex')
The above code is commented for understanding the steps. I shows the output as graph using plot function.
For using double precision use decimal point in the initial value selection
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.