In matlab 1) Write a function to use the Newton method (with analytic derivative
ID: 3792742 • Letter: I
Question
In matlab 1) Write a function to use the Newton method (with analytic derivative) to obtain the root for the following two functions:f(t) = tan (t) t [with x0 = 7] g(t) = exp (t) sqrt(t + 9) [with x0 = 2]
Where x0 is the starting guess. YOU MUST FIRST ATTEMPT THE STARTING GUESSES I PROVIDE. Print each iterate (xi) and its accompanying function value (f(xi)), then print the converged root (x) value. Require an error of less than 1e-6.
If the starting guess I give diverges (wink, wink), choose a new starting guess (x0 = 7.65 for the first one) and find the true root near the starting point. Explain what happened (why it diverged).
2) Rewrite your Newton code to use the Secant method. Use this method to find the roots again, using starting values of your choice.
In matlab 1) Write a function to use the Newton method (with analytic derivative) to obtain the root for the following two functions:
f(t) = tan (t) t [with x0 = 7] g(t) = exp (t) sqrt(t + 9) [with x0 = 2]
Where x0 is the starting guess. YOU MUST FIRST ATTEMPT THE STARTING GUESSES I PROVIDE. Print each iterate (xi) and its accompanying function value (f(xi)), then print the converged root (x) value. Require an error of less than 1e-6.
If the starting guess I give diverges (wink, wink), choose a new starting guess (x0 = 7.65 for the first one) and find the true root near the starting point. Explain what happened (why it diverged).
2) Rewrite your Newton code to use the Secant method. Use this method to find the roots again, using starting values of your choice.
In matlab 1) Write a function to use the Newton method (with analytic derivative) to obtain the root for the following two functions:
f(t) = tan (t) t [with x0 = 7] g(t) = exp (t) sqrt(t + 9) [with x0 = 2]
Where x0 is the starting guess. YOU MUST FIRST ATTEMPT THE STARTING GUESSES I PROVIDE. Print each iterate (xi) and its accompanying function value (f(xi)), then print the converged root (x) value. Require an error of less than 1e-6.
If the starting guess I give diverges (wink, wink), choose a new starting guess (x0 = 7.65 for the first one) and find the true root near the starting point. Explain what happened (why it diverged).
2) Rewrite your Newton code to use the Secant method. Use this method to find the roots again, using starting values of your choice.
Explanation / Answer
Answer of 1)
Part ONE (Eqn 1):
f = @(x) tan(x) - x; % Equation definition
fp = @(x) sec^(x) - 1; % First-order derivative of f
x0 = 7; % 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) = tan(x) - x$','FontSize', 20,'Interpreter','latex')
xlabel('Iteration','FontSize',16)
ylabel('$x$','FontSize',16,'Interpreter','latex')
Part TWO (Eqn 2)
f = @(x) exp(x) - sqrt(x-9); % Equation definition
fp = @(x) exp(x) - 1/(2*sqrt(x+9)); % 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) = tan(x) - x$','FontSize', 20,'Interpreter','latex')
xlabel('Iteration','FontSize',16)
ylabel('$x$','FontSize',16,'Interpreter','latex')
The codes are properly indented for understanding. If you need to print the internediate values of f(x) or f'(x) use the disp() within the loop to print the value of fe and fpe.
There is more than one question, so i had solved only the first question.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.