Question

f(x) = exp(sin(x)^3) + x^6 −2*x^4 −x^3 −1 Plot the function on the interval [−1.3,1.6] and...

f(x) = exp(sin(x)^3) + x^6 −2*x^4 −x^3 −1

Plot the function on the interval [−1.3,1.6] and verify by eye that the location of the three roots are approximately [−1.2,0.1,1.5]. Use these approximations as initial guesses and perform 10 steps of Newton’s method to find better approximations.

In each case print xk at each iteration, underlining the correct number of digits. Determine which roots converge quadratically, and which converge linearly.

I need to code this in matlab

Homework Answers

Answer #1

Matlab function Newton_method.m

function x = Newton_method(f,df,x0,tol,n) %Newton's method to find the root of f(x)
iter=0; % intial iteration value
y = f(x0); % function value at x0
dy = df(x0); % function derivative at x0
err=abs(y/dy); % error
% displaying the result in tabular form
disp(' iter      f(x)            x')
disp('___________________________________________________')
fprintf('%2.0f %12.6f %12.6f\n',iter,y,x0); %result to the screen
x(iter+1) = x0;
while (err>tol)&&(iter<=n)&&(dy~=0) % loop to until converg or reach N iter
      x1=x0-y/dy; % new approximation of root
      err=abs(x1-x0); % estimating the difference b/w two approximations
      x0=x1; % replace the old value in x0 with new value
      y = f(x0); % function evaluating at new x value
      dy = df(x0); % function derivative evaluating at new x value
      iter=iter+1; % increase number of iteration
      fprintf('%2.0f %12.6f %12.6f \n',iter,y,x0); % result to the screen
      x(iter+1) = x0;
end
if (dy==0) % Checking devision by zero occured or not
   disp('division by zero'); % if yes display error message
end
end

Matlab code to call the function Newton_method.m for the various x0 values

f = @(x) exp(sin(x).^3)+x.^6-2*x.^4-x.^3-1; % The function f(x)
df = @(x) 3*exp(sin(x).^3).*sin(x).^2.*cos(x)+6*x.^5-8*x.^3-3*x.^2; % derivative of f(x)
x = -1.3:0.01:1.6;% x vector to plot the the function
plot(x,f(x),x,zeros(size(x))); % function plotting alog with a line in zero
xlabel('x');% labeling x axis
ylabel('f(x)');% labeling y axis
x0 = [-1.2,0.1,1.5]; % initial guesses
tol = 10^-6;% tolerence
n = 10;% number of iterations
for j = 1:3 % loop to get three root for different initial guess
    fprintf('\n\nInitial guess x0 = %f\n',x0(j)); % the initial guess taken is printing in the screen
    xk = Newton_method(f,df,x0(j),tol,n); % Calling the function to compute the root for x0
    figure % opnen new figure window to plot the iteration VS xk plot
    plot(xk);% ploting the values of xk VS iterations
    xlabel('iterations');% labeling the x axis as iterations
    ylabel('xk');% labeling the y axis as xk
    title(x0(j));% titile of the plot as the initial guess
    clear xk; % clearing the variable xk from the workspace
end% end of loop

OUTPUT


Initial guess x0 = -1.200000
iter      f(x)            x
___________________________________________________
0      0.011794     -1.200000
1      0.000100     -1.197644
2      0.000000     -1.197624
3     -0.000000     -1.197624


Initial guess x0 = 0.100000
iter      f(x)            x
___________________________________________________
0     -0.000203      0.100000
1     -0.000064      0.075061
2     -0.000020      0.056343
3     -0.000006      0.042290
4     -0.000002      0.031738
5     -0.000001      0.023816
6     -0.000000      0.017870
7     -0.000000      0.013407
8     -0.000000      0.010057
9     -0.000000      0.007545
10     -0.000000      0.005659
11     -0.000000      0.004245


Initial guess x0 = 1.500000
iter      f(x)            x
___________________________________________________
0     -0.411394      1.500000
1      0.046720      1.533225
2      0.000430      1.530162
3      0.000000      1.530134
4     -0.000000      1.530134

x0 = -1.2 Converge linearly

x0 = 0.1 converge quadratically

x0 = 1.5 converge linearly.

Know the answer?
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
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT