Question
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 begin

%while loops will execute as long as the condition (in this case "test > tol && k < 1000")
%evaluates true. We have two conditions. "test" is greater than our
%tolerance (tol) and our counter is less than 1000. The purpose of the
%counter is to enure if we screw something up, the while loop will
%terminate eventually, even if we don't get convergence below tol
while test > tol && k < 1000 % this loop will continue until the tolerance is reached or 1000 iterations
    disp(['Iteration Number: ' num2str(k)]) %display current iteration number
    
    %insert code for Newton's Method here
    
    %the below will output information about the interations
    disp(['   Test value: ' num2str(fnew)])
    disp(['   Estimated root: ' num2str(xnew)])
    
    xold = xnew; %updates for the next iteration. By overwriting xold with the new value,
    %we will be able to use the current value when the loop repeats
    k = k+1; %this counts the iterations, and increases k by 1 each iteration
end

R = xnew; %this ensures you output the value R, so that it can be automatically checked

the code above is provided, pls fill in the code to solve the problem below2) Newtons method (also known as Newton-Raphson) is another root-finding algorithm. Starting from an initial guess, we can i

Show transcribed image text2) Newton's method (also known as Newton-Raphson) is another root-finding algorithm. Starting from an initial guess, we can improve our estimate using the following formula: f(xold) Xnew = Xold f'(xold) Where f denotes the derivative. This process can be iterated to achieve progressively better approximations of the root. Using MATLAB (HW_5_P2_partial.m) and Newton's Method, identify where In(x) intersects sin(x) from an initial guess of 0.5. Recall that we can express this as In(x)-sin(x) = 0. Recall that the derivative of In(x)-sin(x) is 1/x- cos(x). Does this work for an initial guess of 4? How about 5? For full credit, find the root of the function within the tolerance of 0.0001, starting from an initial guess of 0.5.

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

clearvars
close all
clc

% create symbolic variables
syms x;
syms f(x);
syms df(x);

% function
f(x)=log(x)-sin(x);

% differentiation of function f
df(x)=diff(f);


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 begin

%while loops will execute as long as the condition (in this case "test > tol && k < 1000")
%evaluates true. We have two conditions. "test" is greater than our
%tolerance (tol) and our counter is less than 1000. The purpose of the
%counter is to enure if we screw something up, the while loop will
%terminate eventually, even if we don't get convergence below tol
while test > tol && k < 1000 % this loop will continue until the tolerance is reached or 1000 iterations
disp(['Iteration Number: ' num2str(k)]) %display current iteration number
  
% calculate new functional value
fnew=eval(f(xold));
  
% calculate differentiation value
f_new=eval(df(xold));
  
% calculate new value for x
xnew=xold-fnew/f_new;
  
% calculate difference between two value of x
test=abs(xold-xnew);
  
%the below will output information about the interations
disp([' Test value: ' num2str(fnew)])
disp([' Estimated root: ' num2str(xnew)])
  
xold = xnew; %updates for the next iteration. By overwriting xold with the new value,
%we will be able to use the current value when the loop repeats
k = k+1; %this counts the iterations, and increases k by 1 each iteration
end

R = xnew; %this ensures you output the value R, so that it can be automatically checked

Iteration Number: 1 Test value: -1.1726 Estimated root: 1.5447 Iteration Number: 2 Test value: -0.56484 Estimated root: 2.453

for initial guess x = 4 , output is

Iteration Number: 1 Test value: 2.1431 Estimated root: 1.6284 Iteration Number: 2 Test value: -0.51076 Estimated root: 2.3888

root is same .

again for x=5 ,

Test value: -0.67097-0.080931 Estimated root: -5.7224-1.90041 Iteration Number: 146 Test value: -0.021696-0.05223i Estimated

root is imaginary, so for x=5 , this method for this function does not work ,

Add a comment
Know the answer?
Add Answer to:
clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold...
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
  • % Bisection.m Lines of code 17-26 and 43-47 are bold % This code finds the root...

    % Bisection.m Lines of code 17-26 and 43-47 are bold % This code finds the root of a function f(x) in the interval [a, b] using the Bisection method % % It uses f.m to define f(x), and assumes f(x) is continuous % It requires specification of a, b and the maximum error % It defines error using |f(xnew)| % Define inputs for problem a=0; %Defines lower limit of initial bracketing interval b=1; %Defines upper limit of initial bracketing interval...

  • MATLAB Given a table like this one: i xLeft xRightyLeftyRight xNew yNew 3 8.75 10.000 173.05 -200.0. 9.3750 -12.7441 4 8.75 9.3750 173.05 -12.74 9.0625 80.4260 On iteration 5 of Bisection, what is xL...

    MATLAB Given a table like this one: i xLeft xRightyLeftyRight xNew yNew 3 8.75 10.000 173.05 -200.0. 9.3750 -12.7441 4 8.75 9.3750 173.05 -12.74 9.0625 80.4260 On iteration 5 of Bisection, what is xLeft? xRight? Complete the Newton's Method Root Finding code below: function [out] = myNewton (p, x0, tol) % p is a function handle to a non-linear function of x % x0 is an initial guess of the root of p % tol is a tolerance around the...

  • c++ Newton method for iteratively finding the root f(x) = 0. The equation is Where f(x)...

    c++ Newton method for iteratively finding the root f(x) = 0. The equation is Where f(x) is the function, f'(x) is the derivative of f9x), Write a C++ program to find root for the function of f(x). The function is on your C++ homework 2 for F(x) = x + 2x -10 You may have two functions, for example, float f(float x) float f=x*x-4; //any function equation return f; float prime(float x) float prime = 2 * x; //derivative of...

  • Programming Language: MATLAB Problem 3: (5 Points) Write a function with the header: function [R] -myNewtonRaphson...

    Programming Language: MATLAB Problem 3: (5 Points) Write a function with the header: function [R] -myNewtonRaphson (f, fp, x0, tol) which takes as input f: a function handle fp: a function handle to the derivative of f (note how I do it in the test case). x0: the initial guess of the root tol: a tolerance above which the algorithm will keep iterating. Tips: . Be sure to include an iteration counter which will stop the while-loop if the number...

  • 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)...

  • Matlab problem using newton raphson to find square root of number

    Need help modifying my Matlab script below (myscript calculates the square root of a number. using a Newton-Raphson method with 1 as the initial guess, calculates true and estimated error, and shows each iteration).-I need to create three new functions each of which should be called in the main script. These functions are needed to replace code that is currently in my script shown below.-I need to create these functions:A function to find f(x)A function to find f '(x) ?A...

  • MATLAB Write a function with the header function [s, count] = myMonteCarlo(f, xLeft, xRight, tol) which uses bracketing...

    MATLAB Write a function with the header function [s, count] = myMonteCarlo(f, xLeft, xRight, tol) which uses bracketing logic and random numbers to solve for the root of f. Start from your code for Problem 1, then modify the update equation to randomly choose a number between xLeft and xRight. That is your xNew. Note your code will take a different number of iterations to find the root every time you run it, even for the exact same initial bracket,...

  • 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...

  • 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...

  • Newton's Method in MATLAB During this module, we are going to use Newton's method to compute...

    Newton's Method in MATLAB During this module, we are going to use Newton's method to compute the root(s) of the function f(x) = x° + 3x² – 2x – 4 Since we need an initial approximation ('guess') of each root to use in Newton's method, let's plot the function f(x) to see many roots there are, and approximately where they lie. Exercise 1 Use MATLAB to create a plot of the function f(x) that clearly shows the locations of its...

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