Question

In: Advanced Math

Use MATLAB to figure out the following problem, if you do not know how to use...

Use MATLAB to figure out the following problem, if you do not know how to use MATLAB then please do not answer. Coding is required for the exercise.

For f(x) = arctan(x), find its zeros by implimenting Newtons method and the Secant method in Matlab. (Hint: Use Newtons method to calculate x1 for Secant method)

Comment all code please since I would like to learn how to do this correctly in MATLAB. Thank you.

Solutions

Expert Solution

MATLAB Script:

close all
clear
clc

syms x
tol = 1e-3; % Error tolerance

f = atan(x); % Given function f(x)
df = diff(f); % f'(x)

% Plot the function to get initial estimates of the roots
x_vals = -5:0.001:5; % Range [-5, 5] with step size of 0.001
f_vals = subs(f, x_vals); % Evaluate f(x) at x = x_vals
plot(x_vals, f_vals), grid on % Plot f(x) and turn on the gridlines
xlabel('x'), ylabel('f(x)') % Label the axes
title('f(x) = atan(x)') % Add a title

% Newton's method
x0 = -1; % Initial guess
result = newton(f, df, x0, tol);
fprintf('Root (using Newton''s method), x = %.3f\n', result)

% Secant method
x1 = double(x0 - subs(f, x0)/subs(df, x0)); % Apply Newton update rule to find x1
result = secant(f, x0, x1, tol);
fprintf('Root (using secant method), x = %.3f\n', result)

function x = newton(f, df, x0, tol)
x = x0;
while true
x_ = x; % Save previous iteration's result
x = double(x - subs(f, x)/subs(df, x)); % Newton Update Rule
if abs(x_ - x) < tol % Termination condition
break;
end
end
end

function x = secant(f, a, b, tol)
x = [a b];
while true
xl = x(end-1); xu = x(end);
y = ( xl*subs(f, xu) - xu*subs(f, xl) ) / (subs(f, xu) - subs(f, xl)); % Secant method's root update
x = [x y];
if abs(x(end-1) - x(end)) < tol % Termination condition
break
end
end
x = x(end);
end

Plot:

Output:

Root (using Newton's method), x = 0.000
Root (using secant method), x = 0.000


Related Solutions

Coding: Use MATLAB to figure out the following problem, if you do not know how to...
Coding: Use MATLAB to figure out the following problem, if you do not know how to use MATLAB then please do not answer. Coding is required for the exercise. For f(x) = arctan(x), find its zeros by implimenting Newtons method and the Secant method in Matlab. (Hint: Use Newtons method to calculate x1 for Secant method) Comment all code please since I would like to learn how to do this correctly in MATLAB. Thank you.
This is a Matlab Exercise problem. Please create the Matlab code and figure for the following...
This is a Matlab Exercise problem. Please create the Matlab code and figure for the following problem using problem specifications: Plot x vs y when y=sin(x), y=cos(x), y=sin (2*x), and y=2*sin(x) when x = 1:0.1:10. Use 2 by 2 subplot, sin(x) is in location 1, cos(x) is in location 2, sin(2*x) is in location 3 and 2*sin(x) is in location 4. The plot should have: (1) x label = ‘x value’, y label = ‘y value’, legend ‘y=sin(x)’,’ y=cos(x)’,’ y=sin...
Use Matlab to do the following (please) : 1. Plot the following signals in one figure...
Use Matlab to do the following (please) : 1. Plot the following signals in one figure window using Matlab commands. Given x (n) = {5,2,1,3,1,4,2} plot x(3n) 2. Plot all transformations of a Gaussian signal in one figure window 3. Plot r(-3t-9).
I am supposed to map out the following and can't figure out how to do it!...
I am supposed to map out the following and can't figure out how to do it! Can somebody help? The experiment has to do with determining the simplest formula of potassium chlorate and to determine the original amount of potassium chlorate in a potassium chlorate-potassium chloride mixture by measuring the oxygen lost from decomposition. The chemical reaction is 2KClO3(s) ------> 2KCL(s) + 3 O2(g) I am supposed to map out 1. Mass of oxygen lost in the first part 2....
Blackbody radiation: what is it, and how do we use its properties to figure out the...
Blackbody radiation: what is it, and how do we use its properties to figure out the temperature of an object?
How do I use the data analysis in Excel to figure these questions out? Suppose a...
How do I use the data analysis in Excel to figure these questions out? Suppose a researcher gathered survey data from 19 employees and asked the employees to rate their job satisfaction on a scale from 0 to 100 (with 100 being perfectly satisfied). Suppose the following data represent the results of this survey. Assume that relationship with their supervisor is rated on a scale from 0 to 50 (0 represents a poor relationship and 50 represents an excellent relationship);...
1. If you are given an equipotential diagram, how could you use it to figure out...
1. If you are given an equipotential diagram, how could you use it to figure out the direction of the electric field at a specific location? Explain your thinking in detail, using both words and a picture. 2. If you are given an equipotential diagram, how could you use it to figure out which regions have a stronger electric field and which regions have a weaker electric field? Explain your thinking in detail, using both words and a picture.
How do you figure out the ideal gas equation in a lab experiment?
How do you figure out the ideal gas equation in a lab experiment?
Who do you figure out alkalinity in chemisty?
Who do you figure out alkalinity in chemisty?
How do you figure out the adjusted trial balance if this is the journal entry: Bank...
How do you figure out the adjusted trial balance if this is the journal entry: Bank account Dr. $ 7,500 To common stock a/c $ 7,500 Rent a/c Dr. $     650 to banck a/c $      650 Insurance Premium a/c Dr. $     840 To bank a/c $      840 Equipment a/c DR. $ 3,600 To cash a/c $ 3,600 Office supplies a/c Dr. $     300 To XYZ company a/c $      300 Cash account Dr. $ 6,000 To Sales a/c $ 6,000 ABC...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT