0% found this document useful (0 votes)
8 views2 pages

Newton's Method MATLAB Code Examples

The document contains two MATLAB scripts for finding roots of functions using the Newton-Raphson method. The first script approximates the root of the function f(x) = x^2 - 11, while the second script finds the root of f(x) = exp(-0.5*x)*(4-x) - 2. Both scripts include user input for initial estimates, tolerances for convergence, and display the iteration results.

Uploaded by

Ege Uygunturk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Newton's Method MATLAB Code Examples

The document contains two MATLAB scripts for finding roots of functions using the Newton-Raphson method. The first script approximates the root of the function f(x) = x^2 - 11, while the second script finds the root of f(x) = exp(-0.5*x)*(4-x) - 2. Both scripts include user input for initial estimates, tolerances for convergence, and display the iteration results.

Uploaded by

Ege Uygunturk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1)

clear all;
close all;
clc;

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


xi = input('Initial Estimate = ');
tol=0.0005;

max_iter=50;
fdiff= @(x) 2*x;

for iter=0:max_iter

xi_old=xi;
xi=xi-f(xi)/fdiff(xi);
e_a=abs((xi-xi_old)/xi)*100;
e_t=abs((sqrt(11)-xi)/sqrt(11))*100;
if e_a<tol
fprintf('%d\t\t%.5f\t\t%.5e\n',iter, xi , e_t);
break;
end

fprintf('%d\t\t%.5f\t\t%.5e\n',iter, xi , e_t);
end

2)
clear all;
close all;
clc;

syms x
f = exp(-0.5*x)*(4-x)-2;
fdiff= diff(f);
xi = input('Initial Estimate = ');
tol=0.005;

max_iter=50;

for iter=0:max_iter

xi_old=xi;
xi=xi-eval(subs(f,xi))/eval(subs(fdiff,xi));
e_a=abs((xi-xi_old)/xi)*100;

if e_a<tol
fprintf('%d\t\t%.5f\t\t%.5e\n',iter, xi , e_a);
break;
end

fprintf('%d\t\t%.5f\t\t%.5e\n',iter, xi , e_a);
end

You might also like