%Doolitle sistemas especiales% % metodo de newton raphson modificado
A=input('ingrese la matriz : '); SENL% clc b=input('ingrese la matriz de terminos clc disp('metodo de Gauss-seidel'); independientes : '); x0=[0;0;0]; disp('para un sistema de 3 ecuaciones cpn 3 n=input('ingrese el orden de la matriz : incognitas'); '); syms x y z disp('17X -2Y -3Z = 500'); n=length(b); fname= [3*x-cos(y*z)-0.5;4*x^2- disp('-5X -21Y -2Z = 200'); L=zeros(n,n); 625*y^2+2*y-1;exp(-x*y)+20*z- disp('-5X -5Y +22Z = 30'); U=zeros(n,n); 1+10*pi/3]; %LECTURA DE LAS ECUACIONES REPRESENTADAS EN C=zeros(n,1); fprima=jacobian(fname); MATRICES % X=zeros(n,1); tolerancia=1.e-6; A=input('digite la matriz de la ecuacion 1: for i=1:n maxiter=30; \nEjemplo\n[1 2 3 4]\n'); L(i,i)=1; iter=1; B=input('digite la matriz de la ecuacion 2: end \nEjemplo\n[1 2 3 4]\n'); for j=1:n f=inline(fname); C=input('digite la matriz de la ecuacion 3: for i=1:j jf=inline(fprima); \nEjemplo\n[1 2 3 4]\n'); suma=A(i,j); error=norm(f(x0(1),x0(2),x0(3)),2); %Terminos para evaluar% for k=1:(i-1) fprintf('error=%12.8f\n',error); Y=input('DIGITE LA ESTIMACION PARA Y\n'); suma=suma-L(i,k)*U(k,j); while error >= tolerancia Z=input('DIGITE LA ESTIMACION PARA Z\n'); end fx0=f(x0(1),x0(2),x0(3)); T=input('La tolerancia\n'); U(i,j)=suma; fpx0=jf(x0(1),x0(2),x0(3)); %despejar las ecuaciones y evaluar en X Y end x1=x0-(inv(fpx0))*fx0; Z% for i=(j+1):n X1=(A(1,4)-A(1,2)*Y-A(1,3)*Z)/A(1,1); suma=A(i,j); fx1=f(x1(1),x1(2),x1(3)); Y1=(B(1,4)-B(1,1)*X1-B(1,3)*Z)/B(1,2); for k=1:(j-1) error=norm((fx1),2); Z1=(C(1,4)-C(1,1)*X1-C(1,2)*Y1)/C(1,3); suma=suma-L(i,k)*U(k,j); fprintf('Iter %2d raiz eaX=100; end x=(%14.9f,%14.9f,%14.9f) eaY=100; L(i,j)=suma./U(j,j); f(x)=(%14.9f,%14.9f,%14.9f)\n',iter,x1 eaZ=100; end (1),x1(2),x1(3),fx1(1),fx1(2),fx1(3)); i=1; end if iter > maxiter while (eaX>T && eaY>T && eaZ>T) A fprintf('numero maximo de X=X1; L Y=Y1; U iteraciones muy excedido \n'); Z=Z1; B=b'; return; X1=(A(1,4)-A(1,2)*Y-A(1,3)*Z)/A(1,1); C=inv(L)*B end Y1=(B(1,4)-B(1,1)*X1-B(1,3)*Z)/B(1,2); X=inv(U)*C x0=x1; Z1=(C(1,4)-C(1,1)*X1-C(1,2)*Y1)/C(1,3); iter=iter+1; eaX=abs((X1-X)/X1)*100; end eaY=abs((Y1-Y)/Y1)*100; eaZ=abs((Z1-Z)/Z1)*100; i=i+1; fprintf('\t%i\t %11.7f\t %11.7f\t %11.7f\t %11.7f\t %11.7f\t %11.7f\n' ,i,X1,eaX,Y1,eaY,Z1,eaZ); end %newton raphon SENL end f2=[3*x-cos(y*z)-0.5;4*x^2-625*y^2+2*y- clear all; clear all 1;exp(-x*y)+20*z-1+10*pi/3]; clc; %metodo de broyden SENL% dx=x2-x1; clc; df=f2-f1; syms x y format long A1=A0+((dx- format long A0*df)*transp(dx)*A0)/(transp(dx)*A0*df); disp('------------------------------') f1=x^2-10*x+y^2+8; disp(' i xi yi zi ') x3=x2-A1*f2; f2=x*y^2+x-10*y+8; disp('------------------------------') x=x3(1); f1da=diff(f1,x); i=0; y=x3(2); f1db=diff(f1,y); x=0; z=x3(3); f2da=diff(f2,x); y=0; i=i+1; f2db=diff(f2,y); z=0; t=x2; x0=[x;y;z]; fprintf('%2.6g %2.6f %2.6f %2.6f\n',i, x, x=0; fprintf('%2.6g %2.6f %2.6f %2.6f\n',i, x, y, z') y=0; x2=x3; y, z') x1(1)=1; f0=[3*x-cos(y*z)-0.5;4*x^2-625*y^2+2*y- end y1(1)=1; 1;exp(-x*y)+20*z-1+10*pi/3]; m=1; J=[ 3, z*sin(y*z), y*sin(y*z) INTERPOLACION d(m)=1; ; 8*x, 2 - 1250*y, 0 ;-y*exp(- clear all while d(m)>0.00001 x*y), -x*exp(-x*y), 20]; invJ=inv(J); syms x v1da=eval(f1da); v1db=eval(f1db); x1=x0-invJ*f0; clc x=x1(1); format long v2da=eval(f2da); y=x1(2); v2db=eval(f2db); z=x1(3); x1=[400 600 800 1000]; vf1=eval(f1); i=1; y=[42 39.1 34.5 27.4]; vf2=eval(f1); fprintf('%2.6g %2.6f %2.6f %2.6f\n',i, x, xi=750; A=[v1da v1db;v2da v2db]; y,z') yi = interp1(x1,y,xi,'cubic'); B=[-vf1;-vf2]; f1=[3*x-cos(y*z)-0.5;4*x^2-625*y^2+2*y- C=inv(A)*B; 1;exp(-x*y)+20*z-1+10*pi/3]; disp('Interpolacion por Matlab'); h=C(1); dx=x1-x0; disp(yi); df=f1-f0; j=C(2); %----------------------------Interpolacion Cubica------------------ A0=invJ+((dx- m=m+1; invJ*df)*transp(dx)*invJ)/(transp(dx)*invJ* -------- x1(m)=x+h; df); %y=a0+a1*x+a2*x^2+a3*x^3 y1(m)=y+j; x2=x1-(A0)*f1; %42=a0+a1*(400)+a2*(400)^2+a3*(400)^3 d(m)=((x1(m)-x)^2+(y1(m)-y)^2)^0.5; x=x2(1); x=x1(m); y=x2(2); %39.1=a0+a1*(600)+a2*(600)^2+a3*(600)^3 y=y1(m); z=x2(3); %34.5=a0+a1*(800)+a2*(800)^2+a3*(800)^3 end i=2; %27.4=a0+a1*(1000)+a2*(1000)^2+a3*(1000)^3 fprintf('%2.6g %2.6f %2.6f %2.6f\n',i, x, disp(' k x^k y^k A=[1 x1(1) x1(1)^2 x1(1)^3;1 x1(2) x1(2)^2 x1(2)^3;1 y, z') d') e=0.0001; x1(3) x1(3)^2 x1(3)^3;1 x1(4) x1(4)^2 x1(4)^3]; for p=1:m i=2; b=transp(y); fprintf('%2.0f %2.7f %2.7f t=x1; %2.7f\n',p-1,x1(p),y1(p),d(p)) while e<0.001 && i<10 a=inv(A)*b; a0=a(1); %INTERPOLACION DE NEWTON% %integracion de regla de Simpson% a1=a(2); function[yi, p, b]=pol_newton(x,y,xi) function Regla_de_simpson 1/3 a2=a(3); n=length(x); g=input('ingrese la funcion:','s'); b=zeros(n); f=inline(g); a3=a(4); b(:,1)=y(:); a=input ('ingrese el extremo p3=a0+a1*x+a2*x^2+a3*x^3; for j=2:n inferior'); disp('Polinomio de Grado 3'); for i=1:n-j+1 b=input('ingrese el estremo disp(simplify(p3)); b(i,j)=(b(i+1,j-1)-b(i,j- superior'); %------------Interpolacion Cubica con el Polinomio de 1))/(x(i+j-1)-x(i)); k=input('ingrese la cantidad de veces Lagrange------------- end a aplicar'); end n=2*k; en 3/8 se usa 3*k L0=(x-x1(2))*(x-x1(3))*(x-x1(4))/((x1(1)-x1(2))*(x1(1)- %calculo del dato interpolado% s=0; x1(3))*(x1(1)-x1(4))); xt=1; h=(b-a)/n; L1=(x-x1(1))*(x-x1(3))*(x-x1(4))/((x1(2)-x1(1))*(x1(2)- yi=b(1,1); for i=1:k x1(3))*(x1(2)-x1(4))); for j=1:n-1 L2=(x-x1(1))*(x-x1(2))*(x-x1(4))/((x1(3)-x1(1))*(x1(3)- xt=xt.*(xi-x(j)); s=s+(h/3)*(f(a)+4*f(a+h)+f(a+2*h)); x1(2))*(x1(3)-x1(4))); yi=yi+b(1,j+i)*xt; a=a+2*h; end cuando es de 3/8 L3=(x-x1(1))*(x-x1(2))*(x-x1(3))/((x1(4)-x1(1))*(x1(4)- %construir el polinomio% A=A+(3*h/8)*(f(a)+3*f(a+h)+3*f(a+2*h)+ x1(2))*(x1(4)-x1(3))); p=num2str(b(1,1)); f(a+3*h)); p3l=L0*y(1)+L1*y(2)+L2*y(3)+L3*y(4); xx=x*-1; A=a+3*h; disp('Polinomio de Lagrange de Grado 3'); for j=2:n end disp(simplify(p3l)); signo=''; fprintf(' El valor aproximado de la x=xi; if b(1,j)>=0 integral es:%f',s,A); signo='+'; end vp=eval(p3); end vpl=eval(p3l); xt=''; %REGLA DE SIMPSON 1-3TABLA disp('Interpolacion por Polinomio de Grado 3'); for i=1:j-1 function ReglasSimpson_tabla disp(vp); signo2=''; fprintf('regla de simpson'); x=input('ingrese el ve tor horizontal x'), disp('Interpolacion por Polinomio de Lagrange de Grado if xx(i)>=0 n=lenngth(x); 3'); signo2='+'; k=(n-1)/2;en 3/8 k=(n-1)/3; end if (mod(n-1,2)==0) disp(vpl); xt=strcat(xt,'*(x',signo2,num2str(xx(i h=x(2)-x(1); )),')'); y=input('ingrese el vector horizontal y:'); end A=0; p=strcat(p,signo,num2str(b(1,j)),xt); j=1; for i=1:k end S=(h/3)*(y(j)+4*y(j+1)+y(j+2)); A=A+S; j=j+2; para 3/8 A=A+(3*h/8)*(y(j)+3*y(j+1)+3*y(j+2)+y(j+3)) disp(yo) ; j=j+3; %Programa taylor para EDO %Programa de Runge Kutta para EDO end clc clc fprintf('El valor aproximado de la integral es: %f \nSe aplico %d veces',A,k); clear clear else disp(' ') disp(' ') fprintf('La cantidad de datos no disp(' Método Numérico de Taylor disp(' Método Numérico de Runge Kutta permiete este metodo'); para EDO ') ') end disp(' disp(' end ********************************** ************************************** ') * ') %Programa euler para EDO disp(' ') disp(' ') clc syms x y syms x y clear disp(' De la forma dy/dx = f(x,y) disp(' De la forma dy/dx = f(x,y) ') disp(' ') ') f=input(' Ingrese la función f(x,y): disp(' Método Numérico De Euler para f=input(' Ingrese la función '); EDO ') f(x,y): '); xo=input(' Ingrese el valor inicial disp(' df=simplify(diff(f,sym('x'))+diff(f x(0): '); ********************************* ') ,sym('y'))*f); yo=input(' Ingrese el valor inicial disp(' ') xo=input(' Ingrese el valor inicial y(0): '); syms x y x(0): '); in=input(' Ingrese el valor inicial disp(' De la forma dy/dx = f(x,y) ') yo=input(' Ingrese el valor inicial x(i) por evaluar: '); f=input(' Ingrese la función f(x,y): y(0): '); n=input(' Ingrese el número de '); in=input(' Ingrese el valor inicial subintervalos: '); xo=input(' Ingrese el valor inicial x(i) por evaluar: '); h=(in-xo)/n; x(0): '); n=input(' Ingrese el número de I=1; yo=input(' Ingrese el valor inicial subintervalos: '); disp(' ') y(0): '); h=(in-xo)/n; disp(' Iteración(i) in=input(' Ingrese el valor inicial I=1; y(i) ') x(i) por evaluar: '); while I<=n disp(' n=input(' Ingrese el número de ===================================') subintervalos: '); yo=yo+h*subs(f,{x,y},{xo,yo})+h^2*s format long h=(in-xo)/n; ubs(df,{x,y},{xo,yo})/2; while I<=n; I=1; xo=xo+h; k1=single(subs(f,{x,y},{xo,yo})); while I<=n I=I+1; k2=single(subs(f,{x,y},{xo+h/2,yo+h*k1 yo=yo+h*subs(f,{x,y},{xo,yo}); end /2})); xo=xo+h; disp(' ') k3=single(subs(f,{x,y},{xo+h/2,yo+h*k2 I=I+1; disp(' Solución:' ) /2})); end disp(yo) k4=single(subs(f,{x,y},{xo+h,yo+h*k3}) disp(' ') ); disp(' Solución:' ) yo=yo+h*(k1+2*k2+2*k3+k4)/6; xo=xo+h; y0=input('ingrese el valor inicial de disp([I yo]) y0: '); METODO IMPLICITO. I=I+1; a=rrkk(r,s,x0,y0,t,h); clc end tab=[t,a]; %METODO IMPLICITO% disp(' ') g=a; disp('solucion de ecuaciones disp(' Solución:' ) while (t<tf) diferenciales parciales'), fprintf(' El valor aproximado es: x0=a(1); %valor de x en t=0 alfa=1; %f\n',yo) y0=a(2); %valor de y en t=o x0=0; t=t+h; xL=1; PROGRAMA DE RUNGE KATTA EXAMEN tab=[tab;t,x0,y0]; t0=0; a=rrkk(r,s,x0,yo,t,h); PRIMERO CREAR rrkk tf=1; g=[g;a]; function q=rrkk(xx,yy,x,y,t,h) ax=4; a=(xL-x0)/ax; end u1=h*eval('xx(t,x,y)'); by=10; u=0:h:tf; %dominio pARA GRAFICAR% v1=h*eval('yy(t,x,y)'); b=(tf-t0)/by; hold on u2=h*eval('xx(t+h/2,x+u1/2,y+v1/2)'); for i=1:ax; plot(u',g) %grafica de ambas especies v2=h*eval('yy(t+h/2,x+u1/2,y+v1/2)'); x(i)=x0+1*a; un(i)=20; disp('Resultados del calculo con el u3=h*eval('xx(t+h/2,x+u2/2,y+v2/2)'); end metodo de Runge'); v3=h*eval('yy(t+h/2,x+u2/2,y+v2/2)'); f0(1)=0; fL(1)=60; disp('columna1: t columan2: u4=h*eval('xx(t+h,x+u3,y+v3)'); for i=1:by+1; x(t) columna3:y(t)') v4=h*eval('yy(t+h,x+u3,y+v3)'); t(i)=t0+i*b; tab xx1=x+(1/6)*(u1+2*u2+2*u3+u4); f0(i+1)=f0(1)+i*b; title(['solucion del sistema (x(t)) Vs yy1=y+(1/6)*(v1+2*v2+2*v3+v4); fL(i+1)=80; (y(t)) con h',h]); q=[xx1,yy1]; end xlabel('Tiempo (t)'); s=[f0(1) un fL(1)]; ylabel('corrientes'); segundo programar A=zeros (ax); d=zeros(ax,1); clc disp('solucion de runge kutta dos TERCERO UNIR for i=1:ax-1; ecuaciones'); function dy=dpl(t,y) A(i,i)=-2; A(i,i+1)=1; A(i+1,i)=1; disp(''); dy=[4*y(1)+2*y(2); -y(1)+y(2); end r=inline('4*x+2*y','t','x','y'); A(ax,ax)=-2; r=alfa*b/a^2; s=inline('-x+y','t','x','y'); for i=1:by t0=input('ingrese el valor del tiempo d(1) = f0(i+1); d(ax)=fL(i+1); inicial,t0;'); un=(inv(eye(ax)- tf=input('ingrese el valor del tiempo r*A)*(un'+r*d))'; final, tf'); s=[s;f0(i+1) un fL(i+1)]; h=input('ingrese el valor del paso end h'); disp(s) t=t0; x0=input('ingrese el valor inicial de x0: ');