Você está na página 1de 24

CDIGOS UTILIZADOS EN MATLAB

BISECCIN (CDIGO)

clc;
Fx=input('Ingrese la funcin: ','s');
a=input('Ingrese lmite inferior: ');
b=input('Ingrese lmite superior: ');
e=input('Ingrese el error: ');

n=0;
x=a;
Fa=eval(Fx);
x=b;
Fb=eval(Fx);
fprintf('\nF(a)= %8.6f \nF(b)= %8.6f \n',Fa,Fb);

fprintf('\n n an pn bn F(pn) |(bn-


an)|\n');

while abs((b-a))>=e
P=(a+b)/2;
x=P;
Fp=eval(Fx);
n=n+1;
fprintf('%2d \t %.6f \t %.6f \t %.6f \t %.6f \t
%.6f\n',n,a,P,b,Fp,abs(b-a)/2);
if Fa*Fp<=0
b=P;
Fb=Fp;
else
a=P;
Fa=Fp;
end
end
fprintf('\nEl resultado ser %.6f\n',P);
ezplot(Fx);
grid on;
NEWTON (CDIGO)

clc
syms x;
Fx=input('Ingrese la funcin: ');
inf=input('Ingrese lmite inferior: ');
sup=input('Ingrese lmite superior: ');
ezplot (Fx,[inf,sup]);
grid on;

P0=input('Ingrese el valor inicial: ');


exact=input('Ingrese la exactitud: ');
n=0;
derivF1=diff(Fx);
dF1=double(subs(derivF1,x,P0));
F1=double(subs(Fx,x,P0));

fprintf('\n n \t Pn \tF(Pn)\n');
fprintf('%2d \t %5.7f \t %.7f\n',n,P0,F1);
while abs(F1)>=exact
P0=double((P0-(F1/dF1)));
F1=double(subs(Fx,x,P0));
dF1=double(subs(derivF1,x,P0));
n=n+1;
fprintf('%2d \t %5.7f \t %.7f\n',n,P0,F1);
end
if abs(F1)<1
fprintf('\nEl resultado es: %.7f\n',P0);
else
disp('La funcin NO converge');
end
SECANTE (CDIGO)

clc
syms x;
Fx=input('Ingrese la funcin: ');
inf=input('ingrese lmite inferior: ');
sup=input('Ingrese lmite superior: ');
ezplot(Fx,[inf,sup]);
grid

P0=input('Ingrese P0: ');


P1=input('Ingrese P1: ');
exact=input('Ingrese la exactitud: ');

F0=double(subs(Fx,x,P0));
F1=double(subs(Fx,x,P1));
Psig= P1 - ((P1-P0) *F1)/(F1-F0);
Fsig=double(subs(Fx,x,Psig));
n= 0;

fprintf('\nP0=%4.2f \t P1=%4.2f\n\n n \t Pn+1 \t F(Pn+1)\n',P0,P1);

while abs(Fsig)>=exact
Psig= P1 - ((P1-P0) *F1)/(F1-F0);
Fsig=double(subs(Fx,x,Psig));

P0=P1;
F0=double(subs(Fx,x,P0));

P1=Psig;
F1=double(subs(Fx,x,P1));
n=n+1;
fprintf('%2d \t %.7f \t %.7f \n',n,Psig,Fsig);
end
if abs(Fsig)<1
fprintf('\nEl resultado es: P%2d= %.7f\n',n+1,Psig);
else
fprintf('\nLa funcin NO converge\n');
end
SECANTE 2 (CDIGO SEGN LA EXACTITUD):

clc
syms x;
Fx=input('Ingrese la funcin: ');
ezplot(Fx);
grid

P0=input('Ingrese P0: ');


P1=input('Ingrese P1: ');
i=input('Ingrese el nmero de iteraciones: ');

F0=double(subs(Fx,x,P0));
F1=double(subs(Fx,x,P1));
n=0;
if i>n
fprintf('\nP0=%4.2f \t P1=%4.2f\n\n n \t Pn+1 \t F(Pn+1)\n',P0,P1);

while n<i
Psig= P1 - ((P1-P0) *F1)/(F1-F0);
Fsig=double(subs(Fx,x,Psig));

P0=P1;
F0=double(subs(Fx,x,P0));

P1=Psig;
F1=double(subs(Fx,x,P1));

n=n+1;
fprintf('%3d \t %4.7f \t %4.7f \n',n,Psig,Fsig);
end
if abs(Fsig)<1
fprintf('\nLuego de%3d iteraciones P%3d es igual a %.7f\n',n,n+1,Psig);
else
fprintf('\nLuego de%3d iteraciones la funcin NO converge\n',n);
end
else
disp('Error al ingresar el nmero de iteraciones');
end
PUNTO FIJO (CDIGO):

clc
syms x;
Gx=input('Ingrese g(x): ');
inf=input('ingrese lmite inferior: ');
sup=input('Ingrese lmite superior: ');
ezplot(Gx,[inf,sup]);
grid

P0=input('Ingrese valor inicial: ');


exact=input('Ingrese la exactitud: ');

Pn=double(subs(Gx,x,P0));
n=1;

fprintf('\n P0=%.3f \n n \t Pn \t|P(n)-P(n-1)|\n',P0);


fprintf('%2d \t %4.7f \t %1.7f\n',n,Pn,abs(Pn-P0));
while abs(Pn-P0)>=exact
P0=Pn;
Pn=double(subs(Gx,x,P0));
n=n+1;
fprintf('%2d \t %4.7f \t %1.7f\n',n,Pn,abs(Pn-P0));
end
if abs(Pn-P0)>=1
disp('La funcin NO converge');
else
fprintf('\nEl resultado ser %.7f\n',Pn);
end
DIFERENCIAS DIVIDIDAS DE NEWTON (CDIGO)

clc;
n=input('Polinomio grado: ');
fprintf('\nNecesita %.0f puntos\n',n+1);

for i=1:n+1
fprintf('x%.0f=',i-1);
X(i)=input(' ');
fprintf('F(x%.0f)=',i-1);
F(i)=input(' ');
fprintf('\n');
end
bi=zeros(n+1);
bi(:,1)=F;
for k=2:n+1
for J=k:n+1
bi(J,k)=[bi(J,k-1)-bi(J-1,k-1)]/[X(J)-X(J-k+1)];
end
end
disp('El polinomio es:');
syms x;
polinomio=bi(1,1);
P=1;
for i=1:n
P=P*(x-X(i));
polinomio=polinomio+P*bi(i+1,i+1);
end
polinomio=expand(polinomio);
pretty(polinomio);
hold on;
ezplot(polinomio,[X(1) X(n+1)]);
x=input('x=');
fx=eval(polinomio);
fprintf('F(x)= %.6f\n',fx);
plot(x,fx,'r+');
Con=input('\n\nDesea interpolar otro valor?\n','s');

while Con=='si'
x=input('x=');
fx=eval(polinomio);
fprintf('F(x)= %.6f\n',fx);
hold on;
plot(x,fx,'r+');
Con=input('\n\nDesea interpolar otro valor?\n','s');
end
INTERPOLACIN DE LAGRANGE (CDIGO)

clc;
n=input('Polinomio grado: ');
fprintf('\nNecesita %.0f puntos\n',n+1);

for i=1:n+1 fprintf('x


%.0f=',i-1);
X(i)=input(' ');
fprintf('F(x%.0f)= ',i-1);
Fx(i)=input(' ');
fprintf('\n');
end
syms x;
polinomio=0;
i=1;
while i<=n+1
L=1;
J=0;
while J<=n
if i~=J+1
L=L*(x-X(J+1))/(X(i)-X(J+1));
end
J=J+1;
end
polinomio=polinomio+L*Fx(i);
i=i+1;
end
polinomio=expand(polinomio);
pretty(polinomio);
hold on;
ezplot(polinomio,[X(1) X(n+1)]);
xlabel('DAS');ylabel('Peso promedio (mg)')%Para el ejercicio 19 se
cambia el nombre de las axisas asi: xlabel('DAS');ylabel('Peso promedio
(mg)')
x=input('x= ');
fx=eval(polinomio);
fprintf('F(x)= %.6f',fx');
plot(x,fx,'r+');
Con=input('\n\nDesea interpolar otro valor?\n','s');
while Con=='si'
x=input('x= ');
fx=eval(polinomio);
fprintf('F(x)= %.6f',fx');
hold on;
plot(x,fx,'r+');
Con=input('\n\nDesea interpolar otro valor?\n','s');
end
REGLA DEL TRAPECIO COMPUESTA (CDIGO)

clc;
syms x;
Fx=input('Ingrese F(x)= ');
a=input('Ingrese lmite inferior: ');
b=input('Ingrese lmite superior: ');
n=input('Ingrese #divisiones (n): ');
fprintf('Ingrese un nmero entre (%.2f,%.2f): ',a,b);
u=input(' ');
h=(b-a)/n;

Fa=double(subs(Fx,x,a));
Fb=double(subs(Fx,x,b));

Fxm(2)=0;
Fxi=zeros(n+1);
for j=2:n
xi(j)=(a+((j-1)*h));
Fxi(j)=double(subs(Fx,x,xi(j)));
Fxe(j)=Fxi(j);
Fxi(j)=Fxi(j)+Fxm(j);
Fxm(j+1)=Fxi(j);
end

Px=((h/2)*(Fa+2*Fxi(n)+Fb));

derivFx=diff(Fx);
deriv2Fx=diff(derivFx);
Deriv2Fx=double(subs(deriv2Fx,x,u));

Rx=-((b-a)/12)*(h^2)*Deriv2Fx;

inteFx=Px+Rx;
if Rx<0
fprintf('\nEl resultado es: %.5f%.1d',Px,Rx);
else
fprintf('\nEl resultado es: %.5f+%.1d',Px,Rx);
end

xi(1)=a;
Fxe(1)=Fa;
xi(n+1)=b;
Fxe(n+1)=Fb;
hold on;
plot(xi,Fxe,'r');
ezplot(Fx,[a,b]);
REGLA DEL TRAPECIO COMPUESTA 2 (CDIGO SEGN
EXACTTUD)

clc;
syms x;
Fx=input('Ingrese F(x)= ');
a=input('Ingrese lmite inferior: ');
b=input('Ingrese lmite superior: ');
Rx=input('Ingrese la exactitud: ');

derivFx=diff(Fx);
deriv2Fx=diff(derivFx);
if deriv2Fx~=0
aa=a+0.1;
bb=b-0.1;
iterador=aa;
i=1;
while iterador<=bb
i=i+1;
iterador=iterador+0.1;
end
u=zeros(i);
u(:,1)=100000000000000;
iterador=aa;
i=1;
while iterador<=bb
while abs(double(subs(deriv2Fx,x,iterador)))==0
iterador=iterador+0.1;
end
u(i,1)=abs(double(subs(deriv4Fx,x,iterador)));
iterador=iterador+0.1;
i=i+1;
end
miu=min(u(:,1));

w=(((b-a)/12)*((b-a)^2)*miu)/Rx;
n=nthroot(w,2);

o=floor(n);
l=n-o;
if l<0.5
n=floor(n);
else
n=ceil(n);
end
fprintf('\nn debe ser mayor o igual a: %d\n',n);

h=((b-a)/n);

fprintf('h debe ser menor o igual a %.4f\n',h);


fprintf('\nSegn lo anterior\n');
n=input('Ingrese un n par: ');
h=((b-a)/n);

Fa=double(subs(Fx,x,a));
Fb=double(subs(Fx,x,b));

Fxm(2)=0;
Fxi=zeros(n+1);
for j=2:n
xi(j)=(a+((j-1)*h));
Fxi(j)=double(subs(Fx,x,xi(j)));
Fxe(j)=Fxi(j);
Fxi(j)=Fxi(j)+Fxm(j);
Fxm(j+1)=Fxi(j);
end

Px=((h/2)*(Fa+2*Fxi(n)+Fb));

fprintf('\nCon exactitud de %.1d, el resultado es: %.5f',Rx,Px);

xi(1)=a;
Fxe(1)=Fa;
xi(n+1)=b;
Fxe(n+1)=Fb;
hold on;
plot(xi,Fxe,'r');
ezplot(Fx,[a,b]);
else
fprintf('\nLa funcin es muy sencilla para utilizar este mtodo\nPor
favor realice la integracin');
end
REGLA DE SIMPSON COMPUESTA PAR (CDIGO)

clc;
syms x;
Fx=input('Ingrese F(x)= ');
a=input('Ingrese lmite inferior: ');
b=input('Ingrese lmite superior: ');
n=input('Ingrese #divisiones (n): ');
fprintf('Ingrese un nmero entre (%.2f,%.2f): ',a,b);
u=input(' ');
h=((b-a)/n);

Fa=double(subs(Fx,x,a));
Fb=double(subs(Fx,x,b));

Fxi=zeros(n+1);
Fxp=zeros(n+1);
Fxm(3)=0;
Fxm(2)=0;
for j=2:((n/2)+1)
xi((2*j)-2)=(a+(((2*j)-3)*h));
Fxi((2*j)-2)=double(subs(Fx,x,xi((2*j)-2)));
Fxe((2*j)-2)=Fxi((2*j)-2);
Fxi((2*j)-2)=Fxi((2*j)-2)+Fxm((2*j)-2);
Fxm(2*j)=Fxi((2*j)-2);
end

for j=2:n/2
xi((2*j)-1)=(a+(((2*j)-2)*h));
Fxp((2*j)-1)=double(subs(Fx,x,xi((2*j)-1)));
Fxe((2*j)-1)=Fxp((2*j)-1);
Fxp((2*j)-1)=Fxp((2*j)-1)+Fxm((2*j)-1);
Fxm((2*j)+1)=Fxp((2*j)-1);
end
Px=((h/3)*(Fa+(2*Fxp(n-1))+(4*Fxi(n))+Fb));

derivFx=diff(Fx);
deriv2Fx=diff(derivFx);
deriv3Fx=diff(deriv2Fx);
deriv4Fx=diff(deriv3Fx)

Deriv4Fx=double(subs(deriv4Fx,x,u));

Rx=-((b-a)/180)*(h^4)*Deriv4Fx;

inteFx=Px+Rx;
if Rx<0
fprintf('\nEl resultado es: %.5f%.1d',Px,Rx);
else
fprintf('\nEl resultado es: %.5f+%.1d',Px,Rx);
end

xi(1)=a;
Fxe(1)=Fa;
xi(n+1)=b;
Fxe(n+1)=Fb;
polinomio=0;
j=1;
while j<=n+1
L=1;
J=0;
while J<=n
if j~=J+1
L=L*(x-xi(J+1))/(xi(j)-xi(J+1));
end
J=J+1;
end
polinomio=polinomio+L*Fxe(j);
j=j+1;
end
hold on;
hL=ezplot(polinomio,[a,b]);
set(hL,'Color','green');
hold on;
ezplot(Fx,[a,b]);
REGLA DE SIMPSON COMPUESTA PAR 2(CDIGO SEGN
EXACTITUD)

clc;
syms x;
Fx=input('Ingrese F(x)= ');
a=input('Ingrese lmite inferior: ');
b=input('Ingrese lmite superior: ');
Rx=input('Ingrese la exactitud: ');

derivFx=diff(Fx);
deriv2Fx=diff(derivFx);
deriv3Fx=diff(deriv2Fx);
deriv4Fx=diff(deriv3Fx);
if deriv4Fx~=0
aa=a+0.1;
bb=b-0.1;
iterador=aa;
i=1;
while iterador<=bb
i=i+1;
iterador=iterador+0.1;
end
u=zeros(i);
u(:,1)=100000000000000000;
iterador=aa;
i=1;
while iterador<=bb
while abs(double(subs(deriv4Fx,x,iterador)))==0
iterador=iterador+0.1;
end
u(i,1)=abs(double(subs(deriv4Fx,x,iterador)));
iterador=iterador+0.1;
i=i+1;
end
miu=min(u(:,1));

w=(((b-a)/180)*((b-a)^4)*miu)/Rx;

n=nthroot(w,4);
o=floor(n);
l=n-o;
if l<0.1
n=floor(n);
else
n=ceil(n);
end
fprintf('\nn debe ser mayor o igual a: %d\n',n);

h=((b-a)/n);
fprintf('h debe ser menor o igual a %.4f\n',h);
fprintf('\nSegn lo anterior\n');
n=input('Ingrese un n par: ');
h=((b-a)/n);
Fa=double(subs(Fx,x,a));
Fb=double(subs(Fx,x,b));

Fxi=zeros(n+1);
Fxp=zeros(n+1);
Fxm(3)=0;
Fxm(2)=0;
for j=2:((n/2)+1)
xi((2*j)-2)=(a+(((2*j)-3)*h));
Fxi((2*j)-2)=double(subs(Fx,x,xi((2*j)-2)));
Fxe((2*j)-2)=Fxi((2*j)-2);
Fxi((2*j)-2)=Fxi((2*j)-2)+Fxm((2*j)-2);
Fxm(2*j)=Fxi((2*j)-2);
end

for j=2:n/2
xi((2*j)-1)=(a+(((2*j)-2)*h));
Fxp((2*j)-1)=double(subs(Fx,x,xi((2*j)-1)));
Fxe((2*j)-1)=Fxp((2*j)-1);
Fxp((2*j)-1)=Fxp((2*j)-1)+Fxm((2*j)-1);
Fxm((2*j)+1)=Fxp((2*j)-1);
end

Px=((h/3)*(Fa+(2*Fxp(n-1))+(4*Fxi(n)+Fb)));

fprintf('\nCon exactitud de %.1d, el resultado es: %.5f',Rx,Px);

xi(1)=a;
Fxe(1)=Fa;
xi(n+1)=b;
Fxe(n+1)=Fb;

polinomio=0;
j=1;
while j<=n+1
L=1;
J=0;
while J<=n
if j~=J+1
L=L*(x-xi(J+1))/(xi(j)-xi(J+1));
end
J=J+1;
end
polinomio=polinomio+L*Fxe(j);
j=j+1;
end
hold on;
hL=ezplot(polinomio,[a,b]);
set(hL,'Color','green');
hold on;
ezplot(Fx,[a,b]);
else
fprintf('\nLa funcin es muy sencilla para utilizar este mtodo\nPor
favor realice la integracin');
end
MTODO DE EULER
(CDIGO)

clc;
syms x y;
fprintf('Exprese la ecuacion diferencial como\ny''=f(x,y)\n');
Fx=input('y''=');
x0=input('Ingrese el lmite inferior: ');
xs=input('Ingrese el lmite superior: ');
fprintf('Ingrese y(%d)=',x0);
y0=input(' ');
h=input('Ingrese h: ');
n=(xs-x0)/h;
Cond1=input('Desea realizar una comparacin con el valor real?: ','s');

if Cond1=='si'
Yreal=input('Ingrese la ecuacin solucin y(x)= ');
fprintf('\n\ni \t xi \t yi(xi) \ty(xi) \t error(real)');
y1=y0;
x1=zeros(n+1);
x1(1)=x0;
it=0;
hold on;
plot(x1(1),y1,'.r');
error2=abs(y1-y0);
fprintf('\n%d\t%.3f \t %.6f \t %.6f \t %.6f',it,x1(1),y1,y0,error2);
i=1;
while i<=n
it=i;
x1(i+1)=double(x0+(i*h));
yreal=double(subs(Yreal,x,x1(i+1)));
px=subs(Fx,x,x1(i));
py=double(subs(px,y,y1)); y1=(y0+
(h*py));
error2=abs(y1-yreal);
plot(x1(i+1),y1,'.r');
fprintf('\n%d\t%.3f \t %.6f \t %.6f \t %.6f',it,x1(i+1),y1,yreal,error2);
y0=y1;
i=i+1;
end
ezplot(Yreal,[x0,xs]);

else

fprintf('\nSoluciones aproximadas para y''');


fprintf('\n\ni \t xi \t yi(xi)');
y1=y0;
x1=zeros(n+1);
x1(1)=x0;
it=0;
fprintf('\n%d\t%.3f \t %.6f',it,x1(1),y1);
i=1;
while i<=n
it=i;
x1(i+1)=double(x0+(i*h));
px=subs(Fx,x,x1(i));
py=double(subs(px,y,y1));
y1=(y0+(h*py));
fprintf('\n%d\t%.3f \t %.6f',it,x1(i+1),y1);
y0=y1;
i=i+1;
end
end
EULER MEJORADO
(CDIGO)

clc;
syms x y;
fprintf('\nExprese la ecuacion diferencial como\ny''=f(x,y)\n');
Fx=input('y''=');
x0=input('Ingrese el lmite inferior: ');
xs=input('Ingrese el lmite superior: ');
fprintf('Ingrese y(%d)=',x0);
y0=input(' ');
h=input('Ingrese h: ');
n=(xs-x0)/h;
Cond1=input('Desea realizar una comparacin con el valor real?: ','s');

if Cond1=='si'
Yreal=input('Ingrese la ecuacin solucin y(x)= ');
fprintf('\n\ni \t xi \t yi(xi) \ty(xi) \t error(real)');
y1=y0;
y2=y0;
x1=zeros(n+1);
x1(1)=x0;
it=0;
hold on;
plot(x1(1),y2,'.r');
error2=abs(y2-y0);
fprintf('\n%d\t%.3f \t %.6f \t %.6f \t %.6f',it,x1(1),y2,y0,error2);
i=1;
while i<=n
it=i;
x1(i+1)=double(x0+(i*h));
yreal=double(subs(Yreal,x,x1(i+1)));
px=subs(Fx,x,x1(i));
py2=double(subs(px,y,y2)); y1=(y0+
(h*py2));
px=subs(Fx,x,x1(i+1));
py1=double(subs(px,y,y1));
y2=(y0+((h/2)*(py2+py1)));
error2=abs(y2-yreal);
plot(x1(i+1),y2,'.r');
fprintf('\n%d\t%.3f \t %.6f \t %.6f \t %.6f',it,x1(i+1),y2,yreal,error2);
y0=y2;
i=i+1;
end
ezplot(Yreal,[x0,xs]);

else

fprintf('\nSoluciones aproximadas para y''');


fprintf('\n\ni \t xi \t yi(xi)');
y1=y0;
y2=y0;
x1=zeros(n+1);
x1(1)=x0;
it=0;
fprintf('\n%d\t%.3f \t %.6f',it,x1(1),y2);
i=1;
while i<=n
it=i;
x1(i+1)=double(x0+(i*h));

px=subs(Fx,x,x1(i));
py2=double(subs(px,y,y2));
y1=(y0+(h*py2));
px=subs(Fx,x,x1(i+1));
py1=double(subs(px,y,y1)); y2=(y0+
((h/2)*(py2+py1))); fprintf('\n%d\t%.3f \t
%.6f',it,x1(i+1),y2); y0=y2;
i=i+1;
end
end

Você também pode gostar