Методы деления пополам с вводом функции в виде строковых и встроенных и оценочных функций в октаве или матричном
Bisection method with function input as string and inline and eval functions in octave or matlab
Autor: Alexander Arias
Fecha: 26 de marzo de 2020
Código:
disp("Métodos de Biseccion con inline");
xai=input('Ingrese el intervalo inferior: ');
xbi=input('Ingrese el intervalo superior: ');
tol=input('Ingrese el porcentaje de error: ');
%syms x;
%ejemplo: limite inferior 12, superior 16
%error: 0.5
%funcion: '(668.06/x)*(1-exp(-0.146843*x))-40'
fxstr=input('Ingrese la funciòn: ');
fx=inline(fxstr);
i=1;
%f1=subs(fx,x,xai);
%f2=subs(fx,x,xbi);
f1=feval(fx,xai);
f2=feval(fx,xbi);
ea(i)=100;
if f1*f2 < 0
xa(i)=xai; f1=feval(fx,xai);%f1=subs(f,x,xa(i));
xb(i)=xbi; f2=feval(fx,xbi);%f2=subs(f,x,xb(i));
xr(i)=(xa(i)+xb(i))/2; f3=feval(fx,xr(i));%f3=subs(f,x,xr(i));
fprintf('It. Xa Xr Xb Error aprox \n');
fprintf('%2d \t %11.7f \t %11.7f \t %11.7f \n',i,xa(i),xr(i),xb(i));
while abs(ea(i)) >= tol,
if f1*f3<0
xa(i+1)=xa(i);f1=feval(fx,xa(i+1));%f1=subs(f,x,xa(i+1));
xb(i+1)=xr(i);f2=feval(fx,xb(i+1));%f2=subs(f,x,xb(i+1));
end
if f1*f3> 0
xa(i+1)=xr(i);f1=feval(fx,xa(i+1));%f1=subs(f,x,xa(i+1));
xb(i+1)=xb(i);f2=feval(fx,xb(i+1));%f2=subs(f,x,xb(i+1));
end
xr(i+1)=(xa(i+1)+xb(i+1))/2; f3=feval(fx,xr(i+1));%f3=subs(f,x,xr(i+1));
ea(i+1)=abs((xr(i+1)-xr(i))/(xr(i+1))*100);
fprintf('%2d \t %11.7f \t %11.7f \t %11.7f \t %7.3f \n',...
i+1,xa(i+1),xr(i+1),xb(i+1),ea(i+1));
i=i+1;
end
else
fprintf('No existe una raíz en ese intervalo');
end
No hay comentarios:
Publicar un comentario