Programas de Matlab

8
Método de la bieccion % Este algoritmo resuelve ecuaciones por el metodo de la biseccion % a y b es el intervalo de la solución % d y er son los errores a=input('ingrese el límite inferior a='); b=input('ingrese el límite superior b='); M=input('numero de iteraciones M='); d=input('d='); er=input('er='); u=f(a); v=f(b); c=b-a; %fprintf('%f\t%f\t%f\t%f\t%f\n',a,b,u,v); if sign(u)==sign(v) disp('esta mal') else for k=1:M e=c/2; c=a+e; w=f(c); fprintf('%d\t%f\t%f\t%f\t%f\t%f\n',k,a,b,c,w,e) if abs(e)<d || abs(w)<er break end if sign(w)~=sign(u) b=c; v=w; else a=c; u=w; end c=b-a; end end c Método del puinto fijo % algorimo del metodo de punto fijo x0=input('ingrese un punto de inicio x0='); m=input(' ingrese el numero de iteraciones m='); d=input('ingrese un munero muy pequeño d='); for k=1:m x1=x0; x0=f(x0); fprintf('%d \t %f \t %f \n',k,x1,x0); if abs(x1-x0)<d break; end end x0

Transcript of Programas de Matlab

Page 1: Programas de Matlab

Método de la bieccion

% Este algoritmo resuelve ecuaciones por el metodo de la biseccion% a y b es el intervalo de la solución% d y er son los erroresa=input('ingrese el límite inferior a=');b=input('ingrese el límite superior b=');M=input('numero de iteraciones M=');d=input('d=');er=input('er=');u=f(a);v=f(b);c=b-a;%fprintf('%f\t%f\t%f\t%f\t%f\n',a,b,u,v);if sign(u)==sign(v) disp('esta mal')else for k=1:M e=c/2; c=a+e; w=f(c); fprintf('%d\t%f\t%f\t%f\t%f\t%f\n',k,a,b,c,w,e) if abs(e)<d || abs(w)<er break end if sign(w)~=sign(u) b=c; v=w; else a=c; u=w; end c=b-a; endendc

Método del puinto fijo

% algorimo del metodo de punto fijox0=input('ingrese un punto de inicio x0=');m=input(' ingrese el numero de iteraciones m=');d=input('ingrese un munero muy pequeño d=');for k=1:m x1=x0; x0=f(x0); fprintf('%d \t %f \t %f \n',k,x1,x0); if abs(x1-x0)<d break; endendx0

METODO DE NEWTON

% Este programa halla raíces con el algoritmo de newtonx=input('ingrese el punto inicial x=');m=input('ingrese el numero de iteraciones m=');df=input('df=');y=f(x);e=input('ingrese un numero pequeño e=');fprintf('%d\t%f\t%f\n',0,x,y)for k=1:m x=x-y/df; y=f(x); fprintf('%d\t%f\t%f\n',k,x,y) if abs(y)<e break endend

METODO DE LA SECANTE

% Este algoritmo halla las raíces de las ecuaciones por el método de la secantea=input('ingrese el primer punto de inicio a =');b=input('ingrese el segundo punto b =');m=input('ingrese el numero de iteraciones m =');d=input('ingrese un numero muy pequeño d =');e=input('ingrese un numero muy pequeño e =');u=f(a);v=f(b);fprintf('%f\t% f\t%f\n',0,a,u)fprintf('%f\t%f\t%f\n',1,b,v)for k=1:m if abs(u)<abs(v) s=b; b=a; a=s; p=v; v=u; u=p; end s=(b-a)/(v-u); a=b; u=v; b=b-(v*s); v=f(b); fprintf('%f\t%f\t%f\n',k,b,v) if abs(v)<e || abs(b-a)<d break endendb

Page 2: Programas de Matlab

A.- El de sustitución progresiva y sustitución regresiva para sistemas triangulares

PROGRESIVA REGRESIVA%Este algoritmo resuelve sistemas triangulares inferiores%metodo empleado SUSTITUCION PROGRESIVAclearA=input('ingrese la matriz A =');b=input('ingrese la matriz b =');n=length(A);x=zeros(n);d=n; for d=1:n m=0; for i=1:n m=m+A(d,i)*x(i); end x(d)=(b(d)-m)/A(d,d); end x(1:i)'

%Esten algritmo resulve sistemas triangulares superiores%Metodo empleado SUSTITUCION REGRESIVAA=input('ingrese la matriz A=');b=input('ingrese la matriz b=');n=length(b);x=zeros(n);d=n;while d>=1 m=0; for i=d:n m=m+A(d,i)*x(i); end x(d)=(b(d)-m)/A(d,d); d=d-1;endx(1:i)'

B.- El de eliminación de Gauss simple y el de eliminación de Gauss con filas permutadas

Page 3: Programas de Matlab

GAUUS SIMPLE GAUS CON FLAS PERMUTADAS%Este algoritmpo resulve sistemas de ecuaciones%Usando el metodo de GAUSS BASICOA=input('ingrese la matriz A =');n=length(A);b=input('ingrese la atriz b=');for k=1:n-1 for i=k+1:n z=A(i,k)/A(k,k); A(i,k)=0; for j=k+1:n A(i,j)=A(i,j)-z*A(k,j); end b(i)=b(i)-z*b(k); endenddisp('nueva A');disp(A); disp('nueva b');disp(b);f=length(A);d=f; x=zeros(f);while d>=1 v=0; for i=d:f v=v+A(d,i)*x(i); end x(d)=(b(d)-v)/A(d,d); d=d-1;endx(1:i)'

%GAUSS CON FILAS PERMUTADASA=input('inrese la matriz A=');b=input('inrese la matriz b=');p=input('orden de permutacio de las filas p=');n=length(A);for k=i:n-1 for i=k+1:n z=A(p(i),k)/A(p(k),k); A(p(i),k)=0; for m=k+1:n A(p(i),m)=A(p(i),m)-z*A(p(k),m); end b(p(i))=b(p(i)-z*b(p(k))); endendfor w=1:n F(n,:)=A(p(n),:); bb(n)=b(p(n));enddisp('la nuea matriz');disp(F);disp('la nueva igualdad');disp(bb);n=length(A);k=n;x=zeros(n);while k>=1 m=0; for i=k:n m=m+F(k,i)*x(i); end x(k)=(bb(k)-m)/F(k,k); k=k-1;end x(1:i)'

c.- El de eliminacioinde gauus con pivoteo de filas escaladas

%ELIMINACION DE GAUSS CON FILAS ESCALADASclearA=input('ingrese la matiz A=');b=input('ingrese la matiz b=');n=length(A);

for i=1:n p(i)=i; s(i)=max(abs(A(i,:)));endfor k=1:n-1 for j=k:n if abs(A(p(j),k))/s(p(j))>=abs(A(p(k),k))/s(p(k)) a=p(k); p(k)=p(j); p(j)=a; end end for i=k+1:n z=A(p(i),k)/A(p(k),k); A(p(i),k)=z;

Page 4: Programas de Matlab

for j=k+1:n A(p(i),j)=A(p(i),j)-z*A(p(k),j); end endendfor i=1:n u(i,:)=A(p(i),:); bb(i)=b(p(i));enddisp('La nueva matriz es');disp(u);for i=1:n-1 for j=i+1:n bb(j)=bb(j)-bb(i)*u(j,i); endenddisp('La neva matriz b es');disp(bb);r=length(u);k=r; x=zeros(r);while k>=1 a=0; for c=k:r a=a+u(k,c)*x(c); end x(k)=(bb(k)-a)/u(k,k); k=k-1;ends=x(:,1);disp('Las solucioies son');disp(s);

D.- Los métodos iterativos de Richarsom, Jacobi y Gauss seidel

METODO DE RICHARSON

%metodo de richarsomclearA=input('ingrese la matriz A=');b=input('ingrese la matriz b=');x=input('ingrese los puntos iniciales x=');M=input('ingrese el numero de iteraciones M=');n=length(A);for k=1:M for i=1:n r(i)= b(i)-(A(i,1:n)*x(1:n)); end for i=1:n x(i)=x(i)+r(i); end fprintf('%d\t%f\t%f\t%f\t%f\t%f\t%f\n',k,x')end

METODO DE JACOBI

Page 5: Programas de Matlab

%metodo de jacobi%La matriz A debe de ser diagonalmente dominanteA=input('ingrese la matriz A=');b=input('ingrese la matriz b=');x=input('ingrese los puntos iniciales x=');M=input('ingrese el numero de iteraciones M=');n=length(A);Z=0;for k=1:M for i=1:n p=0; for j=1:n m=A(i,j)*x(j); if j==i m=0; end p=p+m; end u(i)=(b(i)-p)/A(i,i); end for i=1:n x(i)=u(i); end if max(abs(x-Z))<0.000001 break; end v(:,k)=x; z=x; fprintf('%d\t%f\t',k,x); fprintf('\n');end

METODO DE GAUSS-SEIDEL

%gauss seidelclearA=input('ingrese la matriz A=');b=input('ingrese la matriz b=');x=input('ingrese los puntos iniciales x=');M=input('ingrese el numero de iteraciones M=');n=length(A);z=0;for k=1:M for i=1:n p=0; for j=i:n m=A(i,j)*x(j); if j==i m=0; end p=p+m; end x(i)=(b(i)-p)/A(i,i); end if max(abs(x-z))<0.00001 break; end v(:,k)=x; z=x; fprintf('%d\t%f\t',k,x);

Page 6: Programas de Matlab

fprintf('\n');end

METODO DE LA POTENCIA

%CALCULA LOS VALORES Y VECTORES PROPIOS DE UNA MATRIZ POR EL METODO DE LA%POTENCIAA=input('ingrese la matriz A=');x=input('ingrese los vectores propios iniciales x=');M=input('Ingrese el numero de ieteraciones M=');fprintf('%d\t%f\t',0,x);a=0; b=0;fprintf('\n');for k=1:M y=A*x; r=y(1)/y(2); x=y/norm(y); fprintf('%d\t%f\t%f\f',k,x,r); fprintf('\n');if max(abs(x-a))<0.000001 || abs(r-b)<0.000001 break;enda=x;b=r;end

3.- IMPLEMENTE N MATLAB EL ALGORTM DE DIFERENCIAS DIVIDIDAS, APLIQU PARACALCULAR EL POLINOIO DE INTERPOLACION P(X) PARA

F= x5 sin xex+ tanh x

en [-5,5]

a) Usando 5 y después 25 nodos igualmente espaciados grafique f(x) y p(x). comente.b) Hale el error delta aproximado p(x) de f(x) en algunos puntos x E [-5,5].

%DIFERENCIAS DIVIDIDAScleara=input('ingrese la cota inferior a= ');b= input('ingrese la cota superior b= ');n=input('ingrese el numero de nodos n=');l=(b-a)/(n-1);syms x real;f=a;F=@(x) ((x^5)*sin(x))/(exp(x)+tanh(x));for k=1:n u(k)=f; y(k)=F(f); f=f+l;endfor i=1:length(u) c(i,1)=y(i);endfor j=2:length(u) for i=1:length(u)-j+1 c(i,j)=(c(i+1,j-1)-c(i,j-1))/(u(i+j-1)-u(i)); endend

Page 7: Programas de Matlab

v=c(1,:);disp('los coeficientes son');disp(v);pol=0;for p=1:n q=1; for z=1:p-1 q=q*(x-u(z)); end pol=pol +v(p)*q;enddisp('el polinomio de interpolacion es pol');disp(pol);