Several Numerical methods have been developed to deal with problems that may not
ID: 1818885 • Letter: S
Question
Several Numerical methods have been developed to deal with problems that may not have analytical solutions, or when such solutions become difficult to obtain. Among these methods are Newton-Raphson and Secant methods. In this homework, you will have to find the solution (root) for the given function using both methods. The function is:f(x)=10+e^(-x)-5x+sin?(x?^2)
Newton-Raphson method:
1. Start with an initial guess x0
2. Find x1 as follows: x_1= x_0-(f(x_0))/(f ´(x_0))
where (f(x)) ´ is the derivative of f(x) with respect to x
3. Keep finding new values for x using the previous value of x until the difference between any two successive values is negligible:
x_(n+1)= x_n-(f(x_n))/(f ´(x_n))
tolerance= |x_(n+1)-x_n |
4.Choose tolerance =10-7 and catch all the values the method generates for x
Secant method:
1. This method doesn’t require knowledge of the derivative, instead it requires two guess points x0 and x1
2. Find x2 as follows: x_2= x_1-f(x_1)(((x_1-x_0 ))/((f(x_1 )-f(x_0)) ))
3. Keep finding new values for x using the last two generated values of x until the difference between any two successive values is negligible:
x_(n+1)= x_n-f(x_n)(((x_n-x_(n-1) ))/((f(x_n )-f(x_(n-1))) ))
tolerance= |x_(n+1)-x_n |
4. Choose tolerance =10-7 and catch all the values the method generates for x
Finally, compare between the two methods by comparing the number of iterations each method requires before it converges. For example, start from a far guess point (say 100) and see how fast the method finds the solution.
Please turn in your homework printed in a report format (i.e: cover page, introduction, codes, figures of the function and the x values and comments). You can use the publish html files feature but you need to add introduction and comments.
Explanation / Answer
Here are the basic codes for the two methods. The way you wrote the function seems odd, my assumption is you meant to write sin(x^2) and randomly threw question marks in for some reason. If I am wrong it doesn't matter, just plug the correct equation into the codes below:
% Basic Newton-Raphson code
clear
clc
format long
%function:
f=inline('10+exp(-x)-5*x+sin(x^2)')
%derivative of function:
fp=inline('-exp(-x)-5+2*x*cos(x^2)');
i=1;
x(i)=100;
test=1;
while test
x(i+1)=x(i)-f(x(i))/fp(x(i));
test=abs(x(i+1)-x(i))>10^(-7);
i=i+1;
end
fprintf('The root is %2.7f after %.0f iterations ',x(i),i)
fprintf('Examine the vector x from the workspace ')
fprintf('or command window to track the iterations. ')
%Basic secant method code:
clear
clc
format long
f=inline('10+exp(-x)-5*x+sin(x^2)')
i=2;
x(i-1)=0;
x(i)=100;
test=1;
while test
x(i+1)=x(i)-(f(x(i))*(x(i)-x(i-1)))/(f(x(i))-f(x(i-1)));
test=abs(x(i+1)-x(i))>10^(-7 );
i=i+1;
end
fprintf('The root is %2.7f after %.0f iterations ',x(i),i)
fprintf('Examine the vector x from the workspace ')
fprintf('or command window to track the iterations. ')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.