Sie sind auf Seite 1von 15

Codigos Matlab

Mtodo de biseccin:
clc
clear all
a = input('ingrese el valor de a = ');
b = input('ingrese el valor de b = ');
fa= mi_funcion1(a);
fb= mi_funcion1(b);
while fa*fb>=0
disp('sus valores no encierran una solucion')
a = input('ingrese el valor de a = ');
b = input('ingrese el valor de b = ');
fa= mi_funcion1(a);
fb= mi_funcion1(b);
end
error = input('ingrese el valor de su error');
error= abs(error);
nmax = input('ingrese el numero maximo de iteraciones');
dif= 2*error;
n=0;
while dif>=error && nmax>=n
m= (a+b)/2;
fm= mi_funcion1(m);
if fa*fm<0
b= m;
fb= fm;
else
a= m;
fa= fm;
end
dif = abs(fm);
n= n+1;
end
if n>nmax
disp('el programa ha superado el numero maximo de iteraciones')
else
tmp= sprintf('la respuesta obtenda entre %d y %d es %f',a,b,m);
disp(tmp)
end

function y= mi_funcion1(x)
y=(x^4)-(2*(x^2)) ;
return

Mtodo de la secante:
clc
clear all
x1 = input('ingrese el valor de
x2 = input('ingrese el valor de
error = input('ingrese el valor
error= abs(error);
nmax = input('ingrese el numero
dif= 2*error;
n=0;

x1 = ');
x2 = ');
de su error');
maximo de iteraciones');

fx1= mi_funcion2(x1);
fx2= mi_funcion2(x2);
while dif>=error && nmax>=n
x3=x2-(fx2*((x2-x1)/(fx2-fx1)));
fx3= mi_funcion2(x3);
x1= x2;
fx1= fx2;
x2= x3;
fx2= fx3;
dif = abs(x1-x2);
n= n+1;
end
if n>nmax
disp('el programa ha superado el numero maximo de iteraciones')
else
tmp= sprintf('la respuesta obtenda es %f con un error de
%f',x3,error);
disp(tmp)
end
function y= mi_funcion2(x)
y= log(x)-x+2 ;
return

Mtodo de Newton-Raphson:
clc
clear all
x1 = input('ingrese el valor de x1 = ');
error = input('ingrese el valor de su error');
error= abs(error);
nmax = input('ingrese el numero maximo de iteraciones');
dif= 2*error;
n=0;
fx1= mi_funcion2(x1);
fdx1= mi_funciond2(x1);
while dif>=error && nmax>=n
x2=x1-(fx1/fdx1);
fx2= mi_funcion2(x2);
fdx2= mi_funciond2(x2);
x1= x2;
fx1= fx2;
fdx1= fdx2;
dif = abs(x1-x2);
n= n+1;
end
if n>nmax
disp('el programa ha superado el numero maximo de iteraciones')
else
tmp= sprintf('la respuesta obtenda es %f con un error de
%f',x2,error);
disp(tmp)
end

function y= mi_funcion2(x)
y= log(x)-x+2 ;

return

function y= mi_funciond2(x)
y= (1/x)-1 ;

return

Mtodo de Regula Falsi:

clc
clear all
a = input('ingrese el valor de a = ');
b = input('ingrese el valor de b = ');
fa= mi_funcion1(a);
fb= mi_funcion1(b);
while fa*fb>=0
disp('sus valores no encierran una solucion')
a = input('ingrese el valor de a = ');
b = input('ingrese el valor de b = ');
fa= mi_funcion1(a);
fb= mi_funcion1(b);
end
error = input('ingrese el valor de su error');
error= abs(error);
nmax = input('ingrese el numero maximo de iteraciones');
dif= 2*error;
n=0;
while dif>=error && nmax>=n
m= ((a*fb)-(b*fa))/(fb-fa);
fm= mi_funcion1(m);
if fa*fm<0
b= m;
fb= fm;
else
a= m;
fa= fm;
end
dif = abs(fm);
n= n+1;
end
if n>nmax
disp('el programa ha superado el numero maximo de iteraciones')
else
tmp= sprintf('la respuesta obtenda entre %d y %d es %f',a,b,m);
disp(tmp)
end

function y= mi_funcion1(x)
y=(x^4)-(2*(x^2)) ;
return

Mtodo de Regula Falsi modificado:


clc
clear all
a = input('ingrese el
b = input('ingrese el
fa= mi_funcion1(a);
fb= mi_funcion1(b);
while fa*fb>=0
disp('sus valores
a = input('ingrese el

valor de a = ');
valor de b = ');

no encierran una solucion')


valor de a = ');

b = input('ingrese el valor de b = ');


fa= mi_funcion1(a);
fb= mi_funcion1(b);
end
error = input('ingrese el valor de su error');
error= abs(error);
nmax = input('ingrese el numero maximo de iteraciones');
dif= 2*error;
n=0;
na=0;
nb=0;
while dif>=error && nmax>=n
m= ((a*fb)-(b*fa))/(fb-fa);
fm= mi_funcion1(m);
if fa*fm<0
if nb==2
fb= fb/2;
nb= 0;
else
b= m;
fb= fm;
nb= nb+1;
n= n+1;
end
else
if na==2
fa= fa/2;
na= 0;
else
a= m;
fa= fm;
na= na+1;
n= n+1;
end
end
dif = abs(fm);
end
if n>nmax
disp('el programa ha superado el numero maximo de iteraciones')
else
tmp= sprintf('la respuesta obtenda entre %d y %d es %f',a,b,m);
disp(tmp)
end

function y= mi_funcion1(x)
y=(x^4)-(2*(x^2)) ;
return

Mtodo de Diferencias Divididas:


clc
clear
n=input('ingrese el numero de puntos');
for i=1:1:n

a= sprintf('ingrese el valor de X%d = ',i);


x(i)=input(a);
b= sprintf('ingrese el valor de Y%d = ',i);
y(i)=input(b);
end
c=input('ingrese el valor a evaluar');
b= y;
k=1;
for i=2:1:n
for j=i:1:n
b(j)= (b(j)-y(j-1))/(x(j)-x(j-k));
end
y=b;
k=k+1;
end
s=b(1);
mult=1;
for i=1:1:n-1
for j=1:1:i
mult = mult*(c-x(j));
end
s = s+ (mult*b(i+1));
mult=1;
end
tmp= sprintf('interpolando el valor %f su respuesta es %f',c,s);
disp(tmp)

Mtodo de Lagrange:
clc
clear
n=input('ingrese el numero de puntos');
for i=1:1:n
a= sprintf('ingrese el valor de X%d = ',i);
x(i)=input(a);
b= sprintf('ingrese el valor de Y%d = ',i);
y(i)=input(b);
end
c=input('ingrese el valor a evaluar');
mult1= 1;
mult2= 1;
s=0;
for i=1:1:n
for j=1:1:n
if i==j
else
mult1= mult1*(c-x(j));
mult2= mult2*(x(i)-x(j));
end
end
s= s+(y(i)*(mult1/mult2));
mult1= 1;
mult2= 1;
end
disp(s)

Integracin numrica trapecio:


clc
clear all
a=input('ingrese el valor de a');
b=input('ingrese el valor de b');
n=input('ingrese el numero de divisiones');
h=(b-a)/n;
suma= 0;
for i=a+h:h:b-h
suma= suma + mi_funcion3(i);
end
i=(h/2)*(mi_funcion3(a) + mi_funcion3(b) + 2*suma);
tmp= sprintf('el valor de la iontegral es %f',i);
disp(tmp)

function y= mi_funcion3(x)
y= 3*(x^2) ;
return

Integracin numrica Simpson 1/3:


clc
clear all
a=input('ingrese el valor de a');
b=input('ingrese el valor de b');
n=input('ingrese el numero PAR de divisiones');
while mod(n,2)~=0
disp('ingrese un numero PAR de divisiones')
n=input('ingrese el numero PAR de divisiones');
end
h=(b-a)/n;
suma1= 0;
suma2= 0;
p=1;
for i=a+h:h:b-h
x(p)= i;
p= p+1;
end
for k=0:1:(n-2)/2
suma1 = suma1 + mi_funcion3(x(2*k+1));
end
for k=1:1:(n-2)/2
suma2 = suma2 + mi_funcion3(x(2*k));
end
i=(h/3)*(mi_funcion3(a) + mi_funcion3(b) + 4*suma1 + 2*suma2);
tmp= sprintf('el valor de la iontegral es %f',i);
disp(tmp)

function y= mi_funcion3(x)
y= 3*(x^2) ;
return

Integracin numrica Simpson 3/8:


clc
clear all
a=input('ingrese el valor de a');
b=input('ingrese el valor de b');
n=input('ingrese un numero de divisiones multplo de 3');
while mod(n,3)~=0
disp('ingrese un numero de divisiones multplo de 3')
n=input('ingrese el numero de divisiones multiplo de 3');
end
h=(b-a)/n;
suma1= 0;
suma2= 0;
suma3= 0;
p=1;
for i=a+h:h:b-h
x(p)= i;
p= p+1;
end
for k=0:1:(n-3)/3
suma1 = suma1 + mi_funcion3(x(3*k+1));
end
for k=0:1:(n-3)/3
suma2 = suma2 + mi_funcion3(x(3*k+2));
end
for k=1:1:(n-3)/3
suma3 = suma3 + mi_funcion3(x(3*k));
end
i=(3*h/8)*(mi_funcion3(a) + mi_funcion3(b) + 3*suma1 + 3*suma2 +
2*suma3);
tmp= sprintf('el valor de la iontegral es %f',i);
disp(tmp)

function y= mi_funcion3(x)
y= 3*(x^2) ;
return

Cuadratura de Gauss:
clc
clear all
a= input('ingrese el limite de integracion inferior');
b= input('ingrese el limite de integracion superior');
x(1)= -0.861136312;
x(2)= -0.339981043;
x(3)= x(2)*-1;
x(4)= x(3)*-1;
a(1)= 0.347854845;
a(2)= 0.347854845;
a(3)= a(2);
a(4)= a(3);
if a==-1 && b==1
s= 0;

for i=1:1:4
y= mi_funcion4(x(i));
r= a(1)*y;
s= s+r;
end
else
c=(b+a)/2;
d=(b-a)/2;
s= 0;
for i=1:1:4
y= mi_funcion5(x(i),c,d);
r= a(1)*y;
s= s+r;
end
end
tmp= sprintf('el valor de la integral entre %d y %d es %f',a,b,s);
disp(tmp)

function y= mi_funcion4(x)
y= sin(1+x)/(x^2) ;
return

function y= mi_funcion5(x,c,d)
y= (sin(1+(c+d*x))/
((c+d*x)^2))*d ;
return

Integracin doble x trapecio:


clc
clear all
a= input('ingrese el limite de integracion inferior');
b= input('ingrese el limite de integracion superior');
n=input('ingrese el numero de divisiones');
h=(b-a)/n;
p= 1;
for k=a:h:b
c= funcion1(k);
d= funcion2(k);
h1= (d-c)/n;
suma= 0;
for i=c+h1:h1:d-h1
suma= suma + mi_funcion6(k,i);
end
i(p)=(h/2)*(mi_funcion6(k,c) + mi_funcion6(k,d) + 2*suma);
p= p+1;
end
suma= 0;
for m=1:1:n-1
suma= suma + i(m+1);
end
id= (h/2)*(i(1)+i(n)+2*suma);
disp(id)
function y= mi_funcion6(x,y)
y= sin(x+y) ;
return

function y= funcion1(x)
y= log(x) ;
return
function y= funcion2(x)
y= 3 + (2.71828182^(x/5));
return

Metodo de Euler:
clc
clear all
h= input('ingrese el valor del paso');
x(1)= input('ingrese el valor de X1');
y(1)= input('ingrese el valor de Y1');
xf= input('ingrese el valor final de X');
n= (xf-x(1))/h;
n= ceil(n);
for i=2:1:n
x(i)=x(i-1)+h;
y(i)=y(i-1)+ h*mi_funcion7(x(i-1),y(i-1));
end
plot(x,y)
function y= mi_funcion7(x,z)
y= x*(2.71828182^x)+z ;
return

Metodo de Euler Modificado:


clc
clear all
h= input('ingrese el valor del paso');
x(1)= input('ingrese el valor de X1');
y(1)= input('ingrese el valor de Y1');
xf= input('ingrese el valor final de X');
n= (xf-x(1))/h;
n= ceil(n);
for i=2:1:n
x(i)=x(i-1)+h;
z=y(i-1)+ h*mi_funcion7(x(i-1),y(i-1));
y(i)= y(i-1)+(h/2)*(mi_funcion7(x(i-1),y(i-1))+ mi_funcion7(x(i),z));
end
plot(x,y)
function y= mi_funcion7(x,z)
y= x*(2.71828182^x)+z ;
return

Metodo Runge kutta 4to Orden:


clc
clear all
h= input('ingrese el valor del paso');
x(1)= input('ingrese el valor de X1');

y(1)= input('ingrese el valor de Y1');


xf= input('ingrese el valor final de X');
p= 2;
for i=x(1)+h:h:xf
x(p)= i;
k1= h*mi_funcion7(x(p-1),y(p-1));
k2= h*mi_funcion7(x(p-1)+(h/2),y(p-1)+(k1/2));
k3= h*mi_funcion7(x(p-1)+(h/2),y(p-1)+(k1/2));
k4= h*mi_funcion7(x(p-1)+ h,y(p-1)+ k3);
y(p)= y(p-1)+((k1+2*k2+2*k3+k4)/6);
p= p+1;
end
plot(x,y)
function y= mi_funcion7(x,z)
y= x*(2.71828182^x)+z ;
return

Metodo Adams 3er Orden:


clc
clear all
h= input('ingrese el valor del paso');
x(1)= input('ingrese el valor de X1');
y(1)= input('ingrese el valor de Y1');
xf= input('ingrese el valor final de X');
p= 2;
for i=x(1)+h:h:x(1)+2*h
x(p)= i;
k1= h*mi_funcion7(x(p-1),y(p-1));
k2= h*mi_funcion7(x(p-1)+(h/2),y(p-1)+(k1/2));
k3= h*mi_funcion7(x(p-1)+(h/2),y(p-1)+(k1/2));
k4= h*mi_funcion7(x(p-1)+ h,y(p-1)+ k3);
y(p)= y(p-1)+((k1+2*k2+2*k3+k4)/6);
p= p+1;
end
p= 4;
for i=x(1)+h:h:xf
x(p)= i;
z=y(p-1)+(h/12)*(23*mi_funcion7(x(p-1),y(p-1))-16*mi_funcion7(x(p2),y(p-2))+5*mi_funcion7(x(p-3),y(p-3)));
y(p)= y(p-1)+(h/12)*(5*mi_funcion7(x(p),z)+8*mi_funcion7(x(p-1),y(p1))-mi_funcion7(x(p-2),y(p-2)));
end
plot(x,y)

function y= mi_funcion7(x,z)
y= x*(2.71828182^x)+z ;
return

Suma Matrices:
clc
clear all
p=input('ingrese el numero de filas de la matriz');
q=input('ingrese el numero de columnas de la matriz');
for i=1:1:p
for j=1:1:q
tmp= sprintf('A(%d,%d)= ',i,j);
A(i,j)= input(tmp);
end
end
for i=1:1:p
for j=1:1:q
tmp= sprintf('B(%d,%d)= ',i,j);
B(i,j)= input(tmp);
end
end
for i=1:1:p
for j=1:1:q
C(i,j)= A(i,j)+B(i,j);
end
end
disp(A)
disp(B)
disp(C)

Producto Matrices cuadradas:


clc
clear all
n=input('ingrese el tamao de la matriz');
for i=1:1:n
for j=1:1:n
tmp= sprintf('A(%d,%d)= ',i,j);
A(i,j)= input(tmp);
end
end
for i=1:1:n
for j=1:1:n
tmp= sprintf('B(%d,%d)= ',i,j);
B(i,j)= input(tmp);
end
end
for i=1:1:n
for j=1:1:n
C(i,j)= 0;
for k=1:1:n
C(i,j)= C(i,j)+A(i,k)*B(k,j);
end
end
end
disp(C)

Producto de Matrices:
clc
clear all
p=input('ingrese el numero de filas de la matriz A');
q=input('ingrese el numero de columnas de la matriz A');
r=input('ingrese el numero de columnas de la matriz B');
for i=1:1:p
for j=1:1:q
tmp= sprintf('A(%d,%d)= ',i,j);
A(i,j)= input(tmp);
end
end
for i=1:1:q
for j=1:1:r
tmp= sprintf('B(%d,%d)= ',i,j);
B(i,j)= input(tmp);
end
end
for i=1:1:p
for j=1:1:r
C(i,j)= 0;
for k=1:1:q
C(i,j)= C(i,j)+A(i,k)*B(k,j);
end
end
end
disp(C)

Escalonada una Matriz:


clc
clear all
n=input('ingrese el tamao de la matriz = ');
for i=1:1:n
for j=1:1:n
tmp= sprintf('A(%d,%d)= ',i,j);
A(i,j)= input(tmp);
end
end
for i=1:1:n-1
for j=i+1:1:n
m=-A(j,i)/A(i,i);
for k=i:1:n
A(j,k)= A(j,k)+m*A(i,k);
end
end
end
disp(A)

Determinante de una Matriz:


clc
clear all
n=input('ingrese el tamao de la matriz');
for i=1:1:n
for j=1:1:n
tmp= sprintf('A(%d,%d)= ',i,j);
A(i,j)= input(tmp);
end
end
for i=1:1:n-1
for j=i+1:1:n
if A(i,i)==0
for j=i+1:1:n
if A(j,i)~=0
m=p(i);
p(i)=p(j);
p(j)=m;
break
end
end
else
m=-A(j,i)/A(i,i);
for k=i:1:n
A(j,k)= A(j,k)+m*A(i,k);
end
end
end
end
d=1;
for i=1:1:n
d= d*A(i,i);
end
disp(A)
tmp= sprintf('el valor del determinante es %f',d);
disp(tmp)

Para sacar el mayor nmero de una matriz y su posicin


clc
clear all
m=input('ingrese el numero de filas: ');
n=input('ingrese el numero de columnas: ');
for i=1:m
for j=1:n
A(i,j)=input(sprintf('Ingrese A(%d,%d)= ',i,j));
if i==1 & j==1
max=A(1,1);
min=A(1,1);
end
if A(i,j)>max
max=A(i,j);
fmax=i;
cmax=j;
end
if A(i,j)<min
min=A(i,j);
fmin=i;
cmin=j;
end
end
end
A
disp(sprintf('Datos del mximo: max=%d, fila=%d, columna=
%d',max,fmax,cmax));
disp(sprintf('Datos del mnimo: min=%d, fila=%d, columna=
%d',min,fmin,cmin));

NUMEROS PRIMOS
clc
clear all
n=input('Nmero natural que deseas saber si es primo ');
i=2;
primo=1;
while i<=sqrt(n)
if rem(n,i)==0 % Resto de dividir n entre i
primo=0;
break
end
i=i+1;
end
if primo
disp('El nmero dado es primo.')
else
disp('El nmero dado no es primo.')
disp('De hecho, es divisible por:')
disp(i)
end

Das könnte Ihnen auch gefallen