Você está na página 1de 15

“UNIVERSIDAD ANDINA DEL CUSCO

FACULTAD DE INGENIERIA
ESCUELA PROFESIONAL DE INGENIERIA CIVÍL”

“EJEMPLOS EN MATLAB”

 ASIGNATURA :METODOS NUMERICOS


 DOCENTE : MODROVEJO DELGADO SAMUEL
 ALUMNO :ESPINOZA GARCIA EDSON RICARDO
 CÓDIGO :015100512H

CUSCO-PERU
2017
 Suma de progresión
function y=sumadeprogre(n,a,b);
y=(n*(a+b))/2;

 Ejemplo

sumadeprogre(2,1,1)

ans =

 Función cualquiera

function y=funcion(x);
y=x*x+2*x;

 Ejemplo
>> funcion(8)

ans =

80

 Producto de dos números

function y=producto(a,b);

y=a*b;

 Ejemplo

>> producto(1,2)

ans =

>> producto(5,6)

ans =

30
 Aproximación central
function y=aproxi(x,h);

y=(feval('funcion',x+h)-feval('funcion',x-h))/2*h;

 Ejemplo

>> aproxi(2,0.000001)

ans =

6.0000e-12

 Aproximación hacia atrás


function y=aproxim(x,h);

y=(feval('funcion',x)-feval('funcion',x-h))/h

 Ejemplo
>> aproxim(2,0.001)
y=
5.9990
ans =
5.9990
 Aproximación hacia delante
function y=aproxima(x,h);

y=(feval('funcion',x+h)-feval('funcion',x))/h

 Ejemplo

>> aproxima(1,0.0001)

y=

4.0001

ans =

4.0001
 Aproximación hacia delante, atrás y central

function [y1,y2,y3]=derivadatriple(x,h)

y1=(feval('funcion',x+h)-feval('funcion',x))/h

y2=(feval('funcion',x)-feval('funcion',x-h))/h

y3=(feval('funcion',x+h)-feval('funcion',x-h))/2*h

 Ejemplo
>> derivadatriple(2,0.000001)

y1 =

6.0000

y2 =

6.0000

y3 =

6.0000e-12

ans =

6.0000
 Derivada de orden superior

function [y1,y2]=ordensupe(m,n);

syms x

y1=diff(x*x*x+2*x,m)

y2=subs(y1,n)

end

 Ejemplo

>> ordensupe(2,3)
y1 =
6*x
y2 =
18
ans =
6*x
 Operación de matrices suma,producto,inversa y determinante
function
[sumam,inversa,determinante,diferenciam,multiplicacion1,multiplicacion2]=oper
aciones
syms x
A=input('ingrese matriz A:');
B=input('ingrese matriz B:');
sumam=A+B,
diferenciam=A-B,
multiplicacion1=A*B,
multiplicacion2=2*A+5*B,sd
determinante=det(A),
inversa=inv(A),
end

 Ejemplo

>> operaciones
ingrese matriz A:[1 2 5;2 3 0;1 -2 0]
ingrese matriz B:[1 0 0;4 3 0;-1 -2 -3]

sumam =

2 2 5
6 6 0
0 -4 -3

diferenciam =

0 2 5
-2 0 0
2 0 3
multiplicacion1 =

4 -4 -15
14 9 0
-7 -6 0

multiplicacion2 =

7 4 10
24 21 0
-3 -14 -15

determinante =

-35

inversa =
0 0.2857 0.4286
0 0.1429 -0.2857
0.2000 -0.1143 0.0286
ans =

2 2 5
6 6 0
0 -4 -3
 Método bisección

xai=input('Ingrese el intervalo inferior: ');


xbi=input('Ingrese el intervalo superior: ');
tol=input('Ingrese el porcentaje de error: ');
syms x;
f=input('Ingrese la funciòn: ');
i=1;

f1=subs(f,x,xai);
f2=subs(f,x,xbi);

ea(i)=100;
if f1*f2 < 0
xa(i)=xai; f1=subs(f,x,xa(i));
xb(i)=xbi; f2=subs(f,x,xb(i));
xr(i)=(xa(i)+xb(i))/2; 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=subs(f,x,xa(i+1));
xb(i+1)=xr(i);f2=subs(f,x,xb(i+1));
end
if f1*f3> 0
xa(i+1)=xr(i);f1=subs(f,x,xa(i+1));
xb(i+1)=xb(i);f2=subs(f,x,xb(i+1));
end
xr(i+1)=(xa(i+1)+xb(i+1))/2; 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
 Ejemplo bisección
>> MetodoBiseccion
Ingrese el intervalo inferior: 4
Ingrese el intervalo superior: 8
Ingrese el porcentaje de error: 0.00002
Ingrese la funciòn: log(x)-x+5
It. Xa Xr Xb Error aprox
1 4.0000000 6.0000000 8.0000000
2 6.0000000 7.0000000 8.0000000 14.286
3 6.0000000 6.5000000 7.0000000 7.692
4 6.5000000 6.7500000 7.0000000 3.704
5 6.7500000 6.8750000 7.0000000 1.818
6 6.8750000 6.9375000 7.0000000 0.901
7 6.8750000 6.9062500 6.9375000 0.452
8 6.9062500 6.9218750 6.9375000 0.226
9 6.9218750 6.9296875 6.9375000 0.113
10 6.9296875 6.9335938 6.9375000 0.056
11 6.9335938 6.9355469 6.9375000 0.028
12 6.9355469 6.9365234 6.9375000 0.014
13 6.9365234 6.9370117 6.9375000 0.007
14 6.9365234 6.9367676 6.9370117 0.004
15 6.9367676 6.9368896 6.9370117 0.002
16 6.9367676 6.9368286 6.9368896 0.001
17 6.9368286 6.9368591 6.9368896 0.000
18 6.9368286 6.9368439 6.9368591 0.000
19 6.9368439 6.9368515 6.9368591 0.000
20 6.9368439 6.9368477 6.9368515 0.000
21 6.9368439 6.9368458 6.9368477 0.000
22 6.9368458 6.9368467 6.9368477 0.000
 Raphson
x0=input('Ingrese el valor inicial: ');
tol=input('Ingrese el porcentaje de error: ');
f=input('Ingrese la función: ');
i=1;
fx(i)=x0;
syms x;
f1=subs(f,x,fx(i));
z=diff(f);
d=subs(z,x,fx(i));
ea(1)=100;
while abs(ea(i))>=tol;
fx(i+1)=fx(i)-f1/d; f1=subs(f,x,fx(i+1)); d=subs(z,x,fx(i+1));
ea(i+1)=abs((fx(i+1)-fx(i))/fx(i+1)*100);
i=i+1;
end
fprintf('i fx(i) Error aprox (i) \n');
for j=1:i;
fprintf('%2d \t %11.7f \t %7.3f \n',j-1,fx(j),ea(j));
end

 Ejemplo
>> Newton
Ingrese el valor inicial: 4
Ingrese el porcentaje de error: 0.00002
Ingrese la función: log(x)-x+5
i fx(i) Error aprox (i)
0 4.0000000 100.000
1 7.1817258 44.303
2 6.9375385 3.520
3 6.9368474 0.010
4 6.9368474 0.000
 TRAPECIO GENERALIZADO
function trapecio_generalizado
funcion=input('ingrese la funcion \n f(x)=','s');
b=input('ingrese el limite superior de la integral\n');
a=input('ingrese el limite inferior de la integral\n');
n=input('ingrese el numero de intervalos\n');
h=(b-a)/n;
f=0;
for k=1:n-1
x=a+h*k;
f=f+eval(funcion);
end
f=2*f;
x=a; f=f+eval(funcion); x=b; f=f+eval(funcion);
f=(h/2)*(f);
fprintf('El valor aproximado es: %10.15f\n',f)

EJEMPLO
trapecio_generalizado
ingrese la funcion
f(x)='x*x*x+x*x+x+1'
ingrese el limite superior de la integral
8
ingrese el limite inferior de la integral
1
ingrese el numero de intervalos
5
El valor aproximado es: 840.000000000000000
El valor aproximado es: 294.000000000000000
El valor aproximado es: 840.000000000000000
El valor aproximado es: 294.000000000000000
El valor aproximado es: 840.000000000000000
El valor aproximado es: 301.000000000000000
El valor aproximado es: 840.000000000000000
El valor aproximado es: 294.000000000000000
El valor aproximado es: 840.000000000000000
El valor aproximado es: 301.000000000000000
 SIMPSON GENERALIZADO
fprintf('metodo de simpson generalizado')
clear, clc
f=input('Ingrese funcion a analizar: ');
a=input('Ingrese valor inferior: ');
b=input('Ingrese valor superior: ');
n=input('Ingrese numero de tramos: ');
fa=subs(f,a); fb=subs(f,b);
h=(b-a)/n; si=0; sp=0;
for i=1:n-1
x(i)=a+i*h;
end
for i=1:2:n-1
si=si+subs(f,x(i));
end
for i=2:2:n-2
sp=sp+subs(f,x(i));
end
Rpta=vpa(h/3*(fa+4*si+2*sp+fb))
ezplot(f,a:b)
hold on
grid on

 EJEMPLO
Ingrese funcion a analizar: 'x*x*x+x*x+x+1'
Ingrese valor inferior: 2
Ingrese valor superior: 4
Ingrese numero de tramos: 2

Rpta =

86.666666666666666666666666666667
 TRAPECIO SIMPLE
function tratrage
funcion=input('ingrese funcion \n f(x)=','s');
b=input('ingrese superior\n');
a=input('ingrese inferior\n');
n=input('ingrese numero de intervalos\n');
h=(b-a)/n
x=0;f1=eval(funcion);x=4;f2=eval(funcion);
c=(h/2*n)*(f1+f2)
fprintf('el valor aproximado',c);

 EJEMPLOS
>> tratrage()
ingrese funcion
f(x)=x*x*x+x*x+x+1
ingrese superior
4
ingrese inferior
2
ingrese numero de intervalos
1
h=
2
c=
86

el valor aproximado>>
 SIMPSONSIMPLE
function tratrage
funcion=input('ingrese funcion \n f(x)=','s');
b=input('ingrese superior\n');
a=input('ingrese inferior\n');
n=input('ingrese numero de intervalos\n');
h=(b-a)/n
x=2;f1=eval(funcion);x=6;f2=eval(funcion);f3=eval(funcion);
c=(h/6*n)*(f1+(4*f2)+f3)
fprintf('el valor aproximado',c);

 EJEMPLO

el valor aproximado>> tratrage()


ingrese funcion
f(x)=x*x+2*x+1
ingrese superior
6
ingrese inferior
2
ingrese numero de intervalos
2
h=
2
c=

169.3333

el valor aproximado>>
 SISTEMA LINEAL
function x=sistemalineal(A,B)
disp('********************');
disp('programa que resuelve el sist. lineal');
disp('********************');
A=input('ingrese la matrizA:');
B=input('ingrese la matrizB:');
fprintf('LA SOLUCION DEL SISTEMA ES:');
x=det(A*B);
disp('**************');
disp('hecho por:');
disp('curso:metodos numericos');
disp('prof:......');
disp('*******************');
end

 EJEMPLO
>> sistemalineal
********************
programa que resuelve el sist. lineal
********************
ingrese la matrizA:[3 2 4;2 3 4;2 3 4]
ingrese la matrizB:[3 2 4;2 3 4;2 3 4]
LA SOLUCION DEL SISTEMA ES:**************
hecho por:
curso:metodos numericos
prof:......
*******************

ans =

21 24 36
20 25 36
20 25 36

Você também pode gostar