Question
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.
Function 4.3.1 (newton) Newtons method for a scalar rootfinding problem. I function x = newton (f,dfdx.x1) 2 %NEWTON Newton
-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 for a scalar rootfinding problem. I function x = newton (f,dfdx.x1) 2 %NEWTON Newton's method for a scalar equation. 3 % Input: objective function derivative function initial root approximation 5 % dfdx 6%x1 7 % Output vector of root approximations (last one is best) 10 % Operating parameters 12 14 funto1=100*eps ; xto1= 100 * eps; maxiter=40; 15 dx inf; % for initial pass below k=1; 16 17 s while (abs (dx) > xtol) && Cabs (y) >funtol) && Ck
0 0
Add a comment Improve this question Transcribed image text
Answer #1


Matlab code for plotting the function clear all close all %function for which root have to find f-ê (x) x.*-2-sin(x); %functi%Matlab function for Newton Method function [root ]=newton-method (fun,x0,maxit) syms x g1(x) =d if f (fu n , x); %1st DerivaPlotting of x vs. (x) 1.5 0.5 -0.5 0 10 12 14 Published with MATLAB R2018a

%%Matlab code for plotting the function

clear all
close all

%function for which root have to find

f=@(x) x.^-2-sin(x);

%function for which root have to find

fprintf('function for which root have to find f(x)=')
disp(f)

%Interval of x for which root have to find

xx=linspace(1,4*pi,500);
yy=f(xx);

%Plotting of the function
plot(xx,yy)
xlabel('x')
ylabel('f(x)')
title('Plotting of x vs. f(x)')
grid on

  
fprintf('From the plot we can say roots are obtained approximately at\n')
fprintf('\t x=1.07 and function value is %f\n',f(1.07))
fprintf('\t x=3.04 and function value is %f\n',f(3.04))
fprintf('\t x=6.308 and function value is %f\n',f(6.308))
fprintf('\t x=9.414 and function value is %f\n',f(9.414))

%Finding root for initial guess using Newton method

%All initial guess

xx0=[1.5 2 3.2 4 5 2*pi];

for i=1: length(xx0)
  
    x0=xx0(i);
    fprintf('\n\t For initial guess x0=%2.2f\n',x0)
    %root using Newton method
    [root]=newton_method(f,x0,1000);
    fprintf('Root using Newton method is %f\n',root)
  
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Matlab function for Newton Method

function [root]=newton_method(fun,x0,maxit)
syms x
g1(x) =diff(fun,x);   %1st Derivative of this function
xx=x0;            %initial guess]
%Loop for all intial guesses
    n=eps; %error limit for close itteration
    for i=1:maxit
        x2=double(xx-(fun(xx)./g1(xx))); %Newton Raphson Formula
        cc=abs(fun(x2));                 %Error
        err(i)=cc;
        xx=x2;
        if cc<=n
            break
        end
      
    end
    if i==maxit
        warning('Maximum number of iteration reached.\n')
    end
    root=xx;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
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...
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
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