Está en la página 1de 17

UNIVERSIDAD ANDINA DEL CUSCO

FACULTAD DE INGENIERÍA
ESCUELA PROFESIONAL DE INGENIERÍA CIVIL

APLICACIONES EN MATLAB

CURSO : Métodos Numéricos

DOCENTE : Samuel Mogrovejo Delgado

ALUMNO : Roy Franz Yucra Cardeña

CUSCO-PERU
2018 - I
METODO DE BISECCION
Algoritmo en Matlab:

%Metodo de Biseccion - Metodos Numericos Matlab


%Roy Franz Yucra Cardeña 014200660 J
clc;
Fx=input('Ingrese la funcion: ','s');
a=input('Ingrese intervalo menor : ');
c=input('Ingrese intervalo mayor: ');
e=input('Ingrese el error : ');
x=a;
Fa=eval(Fx);
x=c;
Fc=eval(Fx);
fprintf('\n %6s %7s %8s %10s %8s %8s %8s \n ', 'A', 'B',
'C', 'F(a)', 'F(b)', 'F(c)', '|c-a|');
while abs(c-a)>e
b=(a+c)/2;
x=b;
Fb=eval(Fx);

fprintf('\n %11.7f %11.7f %11.7f %11.7f %11.7f %11.7f %11.7f


\n',a,b,c,Fa,Fb,Fc,abs(c-a));
if Fa*Fb<=0
c=b;
Fc=Fb;
else
a=b;
Fa=Fb;
end

end
fprintf('\nEl resultado sera %11.7f\n',b);
ezplot(Fx);%graficamos la funcion
grid on;
Ejemplo:

1) Sea F(x)=x^2-4 Hallar la raíz utilizando el método de bisección y que el resultado sea
correcto en al menos 4 cifras decimales en el intervalo [-30;10].

Solucion:

Ingrese la funcion: x^2-4

Ingrese intervalo menor : -30

Ingrese intervalo mayor: 10

Ingrese el error : 0.005

A B C F(a) F(b) F(c) |c-a|

-30.0000000 -10.0000000 10.0000000 896.0000000 96.0000000 96.0000000 40.0000000

-10.0000000 0.0000000 10.0000000 96.0000000 -4.0000000 96.0000000 20.0000000

-10.0000000 -5.0000000 0.0000000 96.0000000 21.0000000 -4.0000000 10.0000000

-5.0000000 -2.5000000 0.0000000 21.0000000 2.2500000 -4.0000000 5.0000000

-2.5000000 -1.2500000 0.0000000 2.2500000 -2.4375000 -4.0000000 2.5000000

-2.5000000 -1.8750000 -1.2500000 2.2500000 -0.4843750 -2.4375000 1.2500000

-2.5000000 -2.1875000 -1.8750000 2.2500000 0.7851563 -0.4843750 0.6250000

-2.1875000 -2.0312500 -1.8750000 0.7851563 0.1259766 -0.4843750 0.3125000

-2.0312500 -1.9531250 -1.8750000 0.1259766 -0.1853027 -0.4843750 0.1562500

-2.0312500 -1.9921875 -1.9531250 0.1259766 -0.0311890 -0.1853027 0.0781250


-2.0312500 -2.0117188 -1.9921875 0.1259766 0.0470123 -0.0311890 0.0390625

-2.0117188 -2.0019531 -1.9921875 0.0470123 0.0078163 -0.0311890 0.0195313

-2.0019531 -1.9970703 -1.9921875 0.0078163 -0.0117102 -0.0311890 0.0097656

El resultado sera -1.9970703


2) Sea F(x)=x^2-5 Hallar la raíz utilizando el método de bisección y que el resultado sea
correcto en al menos 4 cifras decimales en el intervalo [2.20;2.30].

Solución:

Tolerancia = 0.005

Ingrese el intervalo inferior: 2.20

Ingrese el intervalo superior: 2.30

Ingrese el porcentaje de error: 0.005

Ingrese la funciòn: x^2-5

Ingrese la funcion: x^2-5

Ingrese intervalo menor : 2.20

Ingrese intervalo mayor: 2.30

Ingrese el error : 0.005

A B C F(a) F(b) F(c) |c-a|

2.2000000 2.2500000 2.3000000 -0.1600000 0.0625000 0.2900000 0.1000000

2.2000000 2.2250000 2.2500000 -0.1600000 -0.0493750 0.0625000 0.0500000

2.2250000 2.2375000 2.2500000 -0.0493750 0.0064062 0.0625000 0.0250000

2.2250000 2.2312500 2.2375000 -0.0493750 -0.0215234 0.0064062 0.0125000

2.2312500 2.2343750 2.2375000 -0.0215234 -0.0075684 0.0064062 0.0062500


El resultado sera 2.2343750
METODO DE NEWTON RAPHSON
Algoritmo en Matlab:
%METODO DE NEWTON RAPHSON - Metodos Numericos Matlab
%Roy Franz Yucra Cardeña 014200660 J
clear,clc
cf=input('ingrese la funcion \n f(x)=','s');
syms x
f=inline(cf);
derivada=diff(cf,x);
df=inline(derivada);
tol=input('ingrese tolerancia:');
error=100;
x=input('ingrese valor inicial:');
n=1;
disp(' n xi error')
while(error>tol)
fprintf('\t%i\t%10.8f\t%f\n',n,x,error);
n=n+1;
x=x- f(x)/df(x);
error=abs(f(x));
end

Ejemplo:

1) Hallar la raíz de F(x)= x^2-4 utilizando el método de Newton Raphson para X0=0.9 y que el
resultado sea correcto en al menos 4 cifras decimales.

Solución:

ingrese la funcion

f(x)=x^2-4

ingrese tolerancia:0.005

ingrese valor inicial:0.9

n xi error

1 0.90000000 100.000000

2 2.67222222 3.140772
3 2.08455186 0.345356

4 2.00171476 0.006862

Rep. La buena aproximación es de 2.00171476.

2) Hallar la raíz de F(x)=senx+x-7 utilizando el método de Newton Raphson para X0=1 y que el
resultado sea correcto en al menos 4 cifras decimales.

solución:

ingrese la funcion

f(x)=sin(x)+x-7

ingrese tolerancia:0.005

ingrese valor inicial:1

n xi error

1 1.00000000 100.000000

2 4.34903674 3.585674

3 9.91175363 2.443798

4 -11.11055613 17.117159

5 4.24489320 3.647806

6 10.88514242 2.891234

7 7.63580734 1.612102

8 6.31055367 0.662081

9 6.64165636 0.007501

Rep. La buena aproximación es de 6.64165636.


METODO DE NEWTON RAPHSON MODIFICADO
Algoritmo en Matlab:
% Metodo de Newton Raphson modificado.
clc,clear
syms x
func=input('Ingrese funcion: ');
f=inline(func);
derivada=diff(func,x); df=inline(derivada);
derivadas=diff(derivada,x); ddf=inline(derivadas);
x=input('Ingrese un valor inicial: ');
tol=input('Ingrese tolerancia: ');
error=100; n=1; er=x-((f(x)*df(x))/((df(x))^2-(f(x)*ddf(x))));
disp(' n xk xk+1 error')
while(error>tol)
n=n+1;
xi=x-((f(x)*df(x))/((df(x))^2-(f(x)*ddf(x))));
x=xi;
er=x-(f(x)/df(x));
error=abs(((er-x)/er)*100);
fprintf('\t %i\t %11.7f\t %11.7f %11.7f\n',n,x,er,error);
end
Ejemplo:

1) Hallar la raíz de F(x)= x^2-4 utilizando el método de Newton Raphson Modificado para X0=1
y que el resultado sea correcto en al menos 4 cifras decimales.

Solución:

Ingrese funcion: x^2-4

Ingrese un valor inicial: 1

Ingrese tolerancia: 0.005

n xk xk+1 error

2 1.6000000 2.0500000 21.9512195

3 1.9512195 2.0006098 2.4687595

4 1.9993904 2.0000001 0.0304832

5 1.9999999 2.0000000 0.0000046

Rep. La buena aproximación es de 1.9999999 con un error de 0.0000046%.

2) Hallar la raíz de F(x)= cos(x)+2*x-4 utilizando el método de Newton Raphson Modificado


para X0=1 y que el resultado sea correcto en al menos 4 cifras decimales.

Solución:

Ingrese funcion:

Ingrese un valor inicial: 1

Ingrese tolerancia: 0.005


n xk xk+1 error

2 4.0552252 2.8016731 44.7429848

3 2.3278631 2.3522642 1.0373455

4 2.3519472 2.3521044 0.0066825

5 2.3521044 2.3521044 0.0000003

Rep. La buena aproximación es de 2.3521044 con un error de 0.0000003%.

DERIVADA DE ORDEN N(x,h)


% DERIVADA DE ORDEN N(x,h)
function [y1d,y1a,y1c,y2,y3,y4] = derivadadeordenN(x,h)

y1d = (feval('funccion',x+h)- feval('funccion',x))/h;


y1a = (feval('funccion',x) - feval('funccion',x-h))/h;
y1c = (feval('funccion',x+h) - feval('funccion',x-h))/(2*h);
y2 = (feval('funccion',x+2*h)+ feval('funccion',x-2*h)-
2*feval('funccion',x))/(4*(h^2));
y3 = (feval('funccion',(x+(3*h))) - 3*feval('funccion',(x+2*h)) +
3*feval('funccion',(x+h)) - feval('funccion',x))/(h^3);
y4 = ((feval('funccion',x+2*h)) - 4*(feval('funccion',x+h)) +
6*(feval('funccion',x)) - 4*(feval('funccion',x-h)) +
feval('funccion',x-(2*h)))/(h^4);

if (y4<1)
y4=0;
end
if (y4>1)
y4=0;
end
DERIVADA DE ORDEN N
Algoritmo en Matlab
%DERIVADA DE ORDEN SUPERIOR - Metodos Numericos Matlab
%Roy Franz Yucra Cardeña 014200660 J
format long
syms x
fx = input('Ingrese la función = ');
f = inline(fx);
n = input('Ingrese el orden de la derivada = ');
h = input('Ingrese el valor de h = ');
i=n;
x = input('Ingrese el valor de x = ');
disp('N° It. Derivada');
k = 0;
Fx = 0;
while i>=0
signo = mod(k,2);
if(signo>=1)
Fx =Fx-(factorial(n)/(factorial(n-k)*factorial(k)))* f(x+i*h);
i = i-1;
k=k+1;
Gx = Fx/(h^n);
else
Fx =Fx + (factorial(n)/(factorial(n-k)*factorial(k)))*
f(x+i*h);
i = i-1;
k=k+1;
Gx = Fx/(h^n);
end
fprintf('%d %10.6f \n',k,Gx);
end

EJEMPLO
MATRIZ INVERSA
Algoritmo en Matlab
%CALCULO DE MATRIZ INVERSA - Metodos Numericos Matlab
%Roy Franz Yucra Cardeña 014200660 J
function
[determinante,inversa,transpuesta,rangodeA,adjunta]=matrizresultante
A=input('ingrese la matriz:');
determinante=A,
inversa=inv(A),
transpuesta=A',
rangodeA=rank(A),
adjunta=(det(A))*A,
end

EJEMPLO
ECUACION MATRICIAL
Algoritmo en Matlab
% PROGRAMA PARA RESOLVER LA ECUACION MATRICIAL
function y = solucion (A,B)
clc,clear
A=input ('ingrese la matriz A = ')
B=input ('ingrese la matriz B = ')
y= inv(A)*B;
end

EJEMPLO
METODO DE TRAPECIO SIMPLE
Algoritmo en Matlab
%METODO DEL TRAPECIO SIMPLE
function [ y1,y2,e ] = trapesiosimple( fun,a,b )
f=inline(fun);
y1=(b-a)/2*(f(a)+f(b));

end

EJEMPLO
METODO DE TRAPECIO GENERALIZADO
Algoritmo en Matlab
% METODO DEL TRAPECIO GENERALIZADO
g=input('Ingrese la función: ');
b=input('Ingrese el limite superior: ');
a=input('Ingrese el limite inferior: ');
n=input('Ingrese la cantidad de divisones: ');
h=(b-a)/n; f=inline(g);
s=0
hold on
for i=1:n
s=h/2*(f(a+(i-1)*h)+f(a+(i)*h))+s;

fprintf('el area aproximada es: %10.15f\n\n',s);


end
%grafica
x=linspace(a,b,n);
y=subs(g,x);
bar(x,y)
xlabel('Eje X')
ylabel('Eje y')
title('metodo del trapecio','color','b')
plot(x,y,'red','LineWidth',3)

EJEMPLO

También podría gustarte