Escolar Documentos
Profissional Documentos
Cultura Documentos
FACULTAD DE INGENIERIA
ESCUELA PROFESIONAL DE INGENIERIA CIVÍL”
“EJEMPLOS EN MATLAB”
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
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
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
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));
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
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