Question

Let the mathematical function f(x) be defined as: f(x) = exp(-0.5x) cos(5x)-0.5 , x 〉 0 Write a Matlab function called NewtonTest the algorithm to find the zero as follows. >> [x,f,k-Newton! (O) → x-1.1641, f=-1.6653e-16, k = 7 >> [x,f,k-Newton! (0.1

Let the mathematical function f(x) be defined as: f(x) = exp(-0.5x) cos(5x)-0.5 , x 〉 0 Write a Matlab function called Newton1 that would find the zero based on a passing initial guess as an input argument x0. The function returns the estimated zero location x, the function value at the zero location (f) and the number of iteration k. The iteration function converges if f(%) 10000. When it diverges, it should state so using disp() command. The function should starts as: function [x,fs,k]-Newton1(xo) % enter your code here end
Test the algorithm to find the zero as follows. >> [x,f,k-Newton! (O) → x-1.1641, f=-1.6653e-16, k = 7 >> [x,f,k-Newton! (0.1) → x = 0.1972, f= 1.1102e-16, k :5 >> [x,f,k]-Newton1(1.5) → x= 1.3111, f=-1.1102e-16, k = 6 >> [x,f,k-Newton! (2) → Warning iteration diverged, x-2.8808, f=-0.5624, k = 10001
0 0
Add a comment Improve this question Transcribed image text
Answer #1

MATLAB CODE;

clc;

clear all;

close all;

function [ xz fs k ]=Newton1(x0)
format long
tol=5*eps;
c(1)=x0;
syms x;
f=exp(-0.5*x)*cos(5*x)-0.5 ;
for k=1:1000
l1=subs(f,c(k));
l2=subs(diff(f),c(k));
c(k+1)=c(k)-l1/l2;
l3=(subs(f,c(k+1)));

fs=l3;
xz=c(k+1);
if (abs(l3) < tol)

break;
end
end
end

%%%%%%% TEST %%%% %%%

[x fs k]=Newton1(0);
fprintf(' x= %f \n',x);
fprintf(' fx= %1.32f \n',fs);
fprintf(' k= %f \n',k);
if k>1000
fprintf('Iteration diverged');
end

MATLAB O/P::

x= 1.164097
fx= -0.00000000000000006861264023336157
k= 7.000000
>>

%%%%%%%%TEST%%%%%%

clc;
close all;
clear all;
format long;

[x fs k]=Newton1(1.5);
fprintf(' x= %f \n',x);
fprintf(' fx= %1.32f \n',fs);
fprintf(' k= %f \n',k);
if k>1000
fprintf('Iteration diverged');
end

MATLAB O/P:

x= 1.311127
fx= -0.00000000000000002916434802119459
k= 6.000000

%%%%%%%%%%%%%TEST%%%%%

clc;
close all;
clear all;
format long;

[x fs k]=Newton1(2);
fprintf(' x= %f \n',x);
fprintf(' fx= %1.32f \n',fs);
fprintf(' k= %f \n',k);
if k>1000
fprintf('Iteration diverged');
end

MATLAB O/P:

x= 2.399596
fx= -0.24611144691801684070142641758139
k= 1000.000000
>>>>>>>>>>>>>Iteration diverged

Add a comment
Know the answer?
Add Answer to:
Let the mathematical function f(x) be defined as: f(x) = exp(-0.5x) cos(5x)-0.5 , x 〉 0 Write a ...
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