Beruflich Dokumente
Kultur Dokumente
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
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
valor de a = ');
valor de b = ');
function y= mi_funcion1(x)
y=(x^4)-(2*(x^2)) ;
return
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)
function y= mi_funcion3(x)
y= 3*(x^2) ;
return
function y= mi_funcion3(x)
y= 3*(x^2) ;
return
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
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
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 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)
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