Ejemplo Matlab
Ejemplo Matlab
MATLAB
f=(667.38./x).*(1-exp(-0.146843.*x))-40
MTODO GRFICO
Grfico: f=(667.38./x).*(1-exp(-0.146843.*x))-40
60
%grafica.m
function grafica
x=[13:0.05:16];
f=(667.38./x).*(1-exp(-0.146843.*x))-40;
plot(x,f,'r*');
hold on;
z=0;
plot(x,z,'b*');
xlabel('Eje X');
ylabel('Eje Y');
title('Grfico: f=(667.38./x).*(1-exp(-0.146843.*x))-40');
text(13.5,-2,'f=(667.38./x).*(1-exp(-0.146843.*x))-40');
grid on;
end
50
40
Eje Y
30
20
10
0
f=(667.38./x).*(1-exp(-0.146843.*x))-40
-10
10
Eje X
12
14
16
18
20
Grfico: f=(667.38./x).*(1-exp(-0.146843.*x))-40
4
Eje Y
-1
-2
-3
13
f=(667.38./x).*(1-exp(-0.146843.*x))-40
13.5
14
14.5
Eje X
15
15.5
16
MTODO BISECCIN
%f.m
function y=f(x)
y=(667.38./x).*(1-exp(-0.146843.*x))-40;
Mtodos Numricos
%biseccion.m
function biseccion
fprintf('\t\t MTODO DE LA BISECCIN \n');
fprintf('\t\t=========================\n\n');
fprintf('\t f(x) = y=(667.38./x).*(1-exp(-0.146843.*x))-40 \n\n');
a = input('ingrese valor inferior del intervalo a:');
b = input('ingrese valor superior del intervalo b:');
tol = input('ingrese la tolerancia:');
x = a;
f_a = F(x);
fprintf('f(a) = %10.9f\n',f_a);
x = b;
f_b = F(x);
fprintf('f(b) = %10.9f\n',f_b);
fprintf('f(a) * f(b) = %10.9f\n', f_a * f_b);
if ((f_a * f_b) > 0)
fprintf('\t\tEn este intervalo no hay raices\n');
fprintf('\t\tBuscar otro intervalo\n');
return
else
n = 100;
fprintf('tope para el numero de iteraciones = %8d\n',n);
it = 0;
cAnterior =0;
fprintf('\n -----------------------------------------------------------------------------------------------------');
fprintf('\nNro.Iteracion\t\ta\t\t\t\tc\t\t\t\tb\t\t\t\tf(a)\tf(c)\tf(b)\t\tEa\t ');
fprintf('\n -----------------------------------------------------------------------------------------------------');
while 1
it = it + 1;
if (it > n)
fprintf('numero de iteraciones en exceso');
break;
end
c = (a + b)/2;
x = c;
f_c = F(x);
Ea = abs(( c - cAnterior) / c) * 100 ;
% fprintf('\n %8d \t %10.9f \t %10.9f \t %10.9f \t %10.9f \t %10.9f \t %10.9f ',it,a,c,b,f_a, f_b, f_c);
fprintf('\n%7d\t\t%10.9f \t%10.9f \t%10.9f ',it,a,c,b);
if (f_a > 0)
fprintf('\t\t+');
else
fprintf('\t\t-');
end
if (f_c > 0)
fprintf('\t\t+');
else
fprintf('\t\t-');
end
if (f_b > 0)
fprintf('\t\t+');
else
fprintf('\t\t-');
end
fprintf('\t\t%8.7f%%',Ea);
if (Ea <= tol)
break;
end
if (f_c > 0 && f_a < 0 )
b = c;
f_b = f_c;
elseif (f_c > 0 && f_b < 0)
a = c;
f_a = f_c;
elseif (f_c < 0 && f_a > 0)
Mtodos Numricos
b = c;
f_b = f_c;
else
a = c;
f_a = f_c;
end
cAnterior = c;
end
fprintf('\n -----------------------------------------------------------------------------------------------------');
fprintf('\n Ea < Tolerancia : %5.4f%% <%5.7f%% ==> la raz aproximada es = %10.9f',Ea,tol,c);
fprintf('\n -----------------------------------------------------------------------------------------------------\n');
end
Mtodos Numricos
Mtodos Numricos
end
end
fprintf('\n\t ---------------------------------------------------------------------------------------------------------------------------');
fprintf('\n\n\tLA RAIZ DE LA FUNCION EN EL INTERVALO DADO ES: %.5f\n\n',Z(J,3));
end
Mtodos Numricos
MTODO SECANTE
%secante.m
function secante
global fun
fprintf('\t\t MTODO DE LA SECANTE:\n');
fprintf('\t\t========================\n');
x0=input('Ingrese valor inferior del intervalo : ');
x1=input('Ingrese valor superior del intervalo : ');
tol=input('Ingrese la tolerancia: ');
it=0;
fprintf('\n\t|\tit\t\t | a\t\t\t\t| b\t\t\t\t| f(a)\t\t\t| f(b)\t\t\t| abs\t\t|\n');
fprintf('\t|-------------|-----------------|---------------|---------------|---------------|-----------|\n');
while(it<50)
it=it+1;
x=x0;
f0=F(x0);
x=x1;
f1=F(x1);
x2=(x0*f1-x1*f0)/(f1-f0);
fprintf('\n\t|\t%5f | %5f\t\t| %5f\t\t| %5f\t\t| %5f\t\t| %5f\t|\n',it,x0,x1,f0,f1,abs((x1-x0)/x1)*100);
if((abs(x1-x0)/x1)*100<=tol)
break
end
x0=x1;
x1=x2;
end
fprintf('\n\t -------------------------------------------------------------------------------------------\n');
fprintf('la raiz buscada es=%15.9f\n',x2);
end
Mtodos Numricos
MTODO NEWTON-RAPHSON
%df.m
function y=df(x)
y=(-667.38./(x^2)).*(1-exp(-0.146843.*x))+(667.38./x)*(0.146843.*exp(-0.146843.*x));
% mnewtonraphson.m
function mnewtonraphson
fprintf('\t\tmetodo de newton-raphson\n');
fprintf('\t\t========================\n');
xi = input('ingrese el punto inicial:');
tol = input('ingrese la tolerancia:');
n = 100;
fprintf('tope para el numero de iteraciones = %8d\n',n);
it = 0;
cAnterior =0;
fprintf('\n -----------------------------------------------------------------------------------------------------------------------------------');
fprintf('\nNro.Iteracion \t\txn\t\t\t\t\txi\t\t\t\t\tf(xi)\t\t\t\tdf(xi)\t\t\tf(xi)/df(xi)\t\t\tEa\t\t');
fprintf('\n -----------------------------------------------------------------------------------------------------------------------------------');
while 1
it = it + 1;
if (it > n)
fprintf('numero de iteraciones en exceso');
break;
end
xn = xi - (f(xi) / df(xi));
cActual = xn;
Ea = abs(( cActual - cAnterior) / cActual) * 100 ;
fprintf('\n%8d\t\t\t%10.9f\t\t%12.9f\t\t%12.9f\t\t%10.9f\t\t%12.9f\t\t%8.4f%%\t',it,xn,xi,f(xi),df(xi),(f(xi)/df(xi)),Ea);
if (Ea<= tol)
break;
end
xi = xn;
cAnterior = cActual;
end
fprintf('\n -----------------------------------------------------------------------------------------------------------------------------------');
fprintf('\nlaraizaproximadaes = %10.9f\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t',cActual);
fprintf('\n -----------------------------------------------------------------------------------------------------------------------------------\n');
end