function [root,iter] = Newton(f, fp, fpp, a, b, tol)
global x k
xx = a : 0.01 : b;
ff = f(xx);
plot(xx,ff,'b','LineWidth',3)
hold on
grid on
set(gca, 'FontName', 'Times', 'FontSize', 11)
axis([a,b,min(ff),max(ff)])
xlabel('\it{x}')
ylabel('\it{y}')
title('Метод на Нютон')
if (f(a)*fpp(a)>0)
x(1) = a;
end
if (f(b)*fpp(b)>0)
x(1) = b;
%else x(1) = b;
end
x(2) = x(1) - f(x(1))/fp(x(1));
k = 1;
while (abs(x(k+1)-x(k)) > tol)
x(k+2) = x(k+1) - f(x(k+1))/fp(x(k+1));
k = k + 1;
end
root = x(end);
iter = k;
end
clear all
clc
%===================================
% Using strings for inline()
%===================================
syms y
fun = sin(y) + exp(-y) + y^3 - 5;
funp = diff(fun,y);
funpp = diff(funp,y);
%===================================
% Inline functions for Newton method
%===================================
f = inline(fun);
fp = inline(funp);
fpp = inline(funpp);
%===================================
% Interval for the 1-st root
%===================================
a = 1.55;
b = 1.60;
%===================================
% Tolerance
%===================================
tol = 1.0e-04;
%===================================
% Calling the function Newton(args)
%===================================
figure(1)
[r,it] = Newton(f,fp,fpp,a,b,tol);
display('=================================')
display('FIRST ROOT')
display('=================================')
display('The root: ')
display(r)
display('Number of iterations: ')
display(it)
%===================================
% Interval for the 2-nd root
%===================================
a = -4.8;
b = -4.6;
tol = 1.0e-04;
figure(2)
[r,it] = Newton(f,fp,fpp,a,b,tol);
display('=================================')
display('SECOND ROOT')
display('=================================')
display('The root: ')
display(r)
display('Number of iterations: ')
display(it)
Регистрирани потребители: Google [Bot]