Question

In: Advanced Math

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.

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

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.
PYTHON CODING: In this problem, we will figure out if two balls are colliding. We will...
PYTHON CODING: In this problem, we will figure out if two balls are colliding. We will think in 2D to simplify things, though 3D isn’t different conceptually. For calculating collision, we only care about a ball’s position in space, as well as its size. We can store a ball’s position with the (x, y) coordinates of its center point, and we can calculate its size if we know its radius. Thus, we represent a ball in 2D space as a...
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);...
Looking to see how to figure this out. So far when I break apart the coding...
Looking to see how to figure this out. So far when I break apart the coding it works but when it is all together it doesn't, Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly as difficult as it may appear. The way hangman works (for this assignment - we are doing a simplified game) is as follows: the computer will choose a word. (For this version, a word is selected from a...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT