Plotting a graphical converge inside a While - Newton Raphson numerical method

조회 수: 1 (최근 30일)
Hi, I'm trying to get a command inside a while so as the Newton Rapshon code shows a graphical converge. If the values of the aproximations are connected between a line, it shows something like a spider web. Can anyone help me out, please?
Here is my code:
clear
clc
syms x
f = input('Introducir la función: '); %function
p0 = input ('Introducir valor semilla: '); %first aproximation
TOL = input ('Introducir la tolerancia de error: '); %error
fplot (f)
grid on
hold on
derivada = diff(f);
derivada = inline (derivada);
f = inline (f);
eabs = 100;
i =0;
while eabs>TOL
p = p0 - (f(p0))/(derivada(p0));
eabs = abs(((p-p0)/p)*100);
p0 = p;
i = i+1;
end
fprintf('\n Valor= %8.3f ',p0)
plot (p)
fplot (0)
hold off
  댓글 수: 7
David Wilson
David Wilson 2019년 8월 27일
Another solution assuming you are looking for an iterative numerial stragegy that stays strictly within the given bounds is to use bisection. Sure it is slow, but it will stay within the -pi/2 to +pi/2 bound.
F = @(x) -x +cos(x)+tan(x); % function of interest
eps = 0.0; tol = 1e-6; % assume some stopping tolerance, set eps to 0.1 if nervous
x = [-pi/2+eps, pi/2-eps];
Fx = F(x); % should be vectorised
assert(prod(sign(Fx)) < 0,'x does not bracket root')
while abs(diff(x)) > tol % Now start bisection routine
m = mean(x); % mid point, m = (a + b)/2
fm = F(m); % find f(m)
if Fx(1)*fm > 0
x(1)=m; Fx(1)=fm; % discard left
else
x(2)=m; % discard right
end % if
end % while
Either value of x is a reasonable solution.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by