Question

Newtons Method in MATLAB During this module, we are going to use Newtons method to compute the root(s) of the function f(x)Download the following M-file, then open and view its contents. This M-file contains the function newton to help us get startnewton.m x + function X = newton(x) %NEWTON Performs a single iteration of Newtons method. % To use Newtons method to compuExercise 3 Add a while loop to newton.m that performs additional iterations of Newtons method. 1. Your loop should run whileNewtons method is not a fail-safe way to find roots, since the sequence of approximations may not converge for a bad initiSometimes we want to visualise how quickly Newtons method is converging (or not converging), we can visualise the convergencExercise 6 For which of the following initial guesses, X1, use your function newton to find if Newtons method converges to a

0 0
Add a comment Improve this question Transcribed image text
Answer #1

clc;
clear all;
close all;
n=10;
f=@(x)x.^3+3*x.^2-2*x-4;
fprime=@(x) 3*x.^2+6*x-2;
x=linspace(-4,2,100);
fx=f(x);
plot(x,fx);
grid on;
xlabel('x');
ylabel('function');

function -3 -2 -1 0 1 2

%%% Matlab function

function [] = newton (x)
f=@(x)x.^3+3*x.^2-2*x-4;
fprime=@(x) 3*x.^2+6*x-2;
xp=x;
x=xp-f(xp)/fprime(xp);
n=1;
while ( abs(x-xp) > 0.001 )
xp=x;
x=xp-f(xp)/fprime(xp);
n=n+1;
end
fprintf('Newton Approximates root at %1.10f after %d iteration \n',x,n);
fprintf('f(x) at approximate root x is %1.10f \n',f(x));
fprintf('|x-xp| is %1.10f \n',abs(x-xp));

end

OUTPUT:

>> newton(10)

Newton Approximates root at 1.2360679797 after 8 iteration
f(x) at approximate root x is 0.0000000221
|x-xp| is 0.0000574524

%%%

function [] = newton (x)
f=@(x)x.^3+3*x.^2-2*x-4;
fprime=@(x) 3*x.^2+6*x-2;
xp=x;
x=xp-f(xp)/fprime(xp);
n=1;
while ( abs(x-xp) > 0.001 )
xp=x;
x=xp-f(xp)/fprime(xp);
n=n+1;
if (n == 100 )
fprintf('Roots diverges \n');
break;
end
end
if (n<100)
fprintf('Newton Approximates root at %1.10f after %d iteration \n',x,n);
fprintf('f(x) at approximate root x is %1.10f \n',f(x));
fprintf('|x-xp| is %1.10f \n',abs(x-xp));
end

end

OUTPUT:

>> newton(-3)
Newton Approximates root at -3.2360679775 after 4 iteration
f(x) at approximate root x is -0.0000000000
|x-xp| is 0.0000016550
>> newton(-1)
Newton Approximates root at -1.0000000000 after 1 iteration
f(x) at approximate root x is 0.0000000000
|x-xp| is 0.0000000000
>> newton(0)
Roots diverges

Add a comment
Know the answer?
Add Answer to:
Newton's Method in MATLAB During this module, we are going to use Newton's method to compute...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is Matlab Problem and I'll attach problem1 and its answer for reference. We were unable...

    This is Matlab Problem and I'll attach problem1 and its answer for reference. We were unable to transcribe this imageNewton's Method We have already seen the bisection method, which is an iterative root-finding method. The Newton Rhapson method (Newton's method) is another iterative root-finding method. The method is geometrically motivated and uses the derivative to find roots. It has the advantage that it is very fast (generally faster than bisection) and works on problems with double (repeated) roots, where the...

  • Calculate two iterations of Newton's Method to approximate a zero of the function using the given...

    Calculate two iterations of Newton's Method to approximate a zero of the function using the given initial guess. (Round your answers to three decimal places.) f(x) = x7 − 7,    x1 = 1.2 Calculate two iterations of Newton's Method to approximate a zero of the function using the given initial guess. (Round your answers to three decimal places.) f(x) = x? - 7, x1 = 1.2 n X f(xn) f'(x) 1 2

  • in matlab -Consider the equation f(x) = x-2-sin x = 0 on the interval x E [0.1,4 π] Use a plot to approximately locate the roots of f. To which roots do the fol- owing initial guesses converge wh...

    in matlab -Consider the equation f(x) = x-2-sin x = 0 on the interval x E [0.1,4 π] Use a plot to approximately locate the roots of f. To which roots do the fol- owing initial guesses converge when using Function 4.3.1? Is the root obtained the one that is closest to that guess? )xo = 1.5, (b) x0 = 2, (c) x.-3.2, (d) xo = 4, (e) xo = 5, (f) xo = 27. Function 4.3.1 (newton) Newton's method...

  • B. Implement the Newton-Raphson (NR) method for solving nonlinear equations in one dimension. The...

    B. Implement the Newton-Raphson (NR) method for solving nonlinear equations in one dimension. The program should be started from a script M-file. Prompt the user to enter an initial guess for the root. -Use an error tolerance of 107, -Allow at most 1000 iterations. .The code should be fully commented and clear 2. a) Use your NR code to find the positive root of the equation given below using the following points as initial guesses: xo = 4, 0 and-1...

  • clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold...

    clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold = 0.5; % this is the initial guess test = 1; % this simply ensures we have a test value to enter the loop below. %If we don't preallocate this, MATLAB will error when it trys to start the %while loop below k = 1; %this is the iteration counter. Similar to "test" we need to preallocate it %to allow the while loop to...

  • Problem 1 (Matlab): One of the most fundamental root finding algorithms is Newton's Method. Given a real-valued, di...

    Problem 1 (Matlab): One of the most fundamental root finding algorithms is Newton's Method. Given a real-valued, differentiable function f, Newton's method is given by 1. Initialization: Pick a point xo which is near the root of f Iteratively define points rn+1 for n = 0,1,2,..., by 2. Iteration: f(xn) nt1 In 3. Termination: Stop when some stopping criterion occurs said in the literature). For the purposes of this problem, the stopping criterion will be 100 iterations (This sounds vague,...

  • Consider the following function with a real variable, x: ?(?) = ?3 - 3?2 + 6?...

    Consider the following function with a real variable, x: ?(?) = ?3 - 3?2 + 6? + 10 a. Write a Python function for the derivative of f(x) that takes x and returns the derivative of f(x). Take the derivative of f(x) analytically with respect to x before writing the function. b. Write a Python code that approximately finds the real root, x0, of f(x) such that f(x0)~0 using the Newton-Raphson method. The code is expected to get an initial...

  • Only the matlab nlinear equations x 0.75 Determine the roots of these equations using: a) The...

    Only the matlab nlinear equations x 0.75 Determine the roots of these equations using: a) The Fixed-point iteration method. b) The Newton Raphson method. Employ initial guesses of x y 1.2 and perform the iterations until E.<10%. Note: You can use to solve the problems, but you should sol at least two full iterations manually. AB bl Du Thursd 30/3/ 1. For the displacement in Q3 y 10 e cos at 0 St S 4. a) Plot the displacement y...

  • LAB 2 APROXIMATING ZEROS OF FUNCTIONS USING NEWTON'S METHOD (Refer to section 3.8 of your textbook...

    LAB 2 APROXIMATING ZEROS OF FUNCTIONS USING NEWTON'S METHOD (Refer to section 3.8 of your textbook for details in the derivation of the method and sample problems) (NOTE: You can use Derive, MicrosoftMathematics or Mathematica or any other Computer Algebra System of your choice. Your final report must be clear and concise. You must also provide sufficient comments on your approach and the final results in a manner that will make your report clear and accessible to anyone who is...

  • I'm working on the newton's method on matlab, could someone help me and show what two...

    I'm working on the newton's method on matlab, could someone help me and show what two lines are needed to be added in the codes in order to make this function work? function sample_newton(f, xc, tol, max_iter) % sample_newton(f, xc, tol, max_iter) % finds a root of the given function f over the interval [a, b] using Newton-Raphson method % IN: % f - the target function to find a root of % function handle with two outputs, f(x), f'(x)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT