r 1mn

11
%HORNER p=input('[anxn+…+ao]: '); %[-2 4x 0x^2 -2 3 4 1] [1 4 3 -2 0 4 -2] x=input('punto a evaluar: '); %2 n=length(p)-1; %n=6 o=p'; b=o(length(p):-1:1); %1era iteración, 1 valor b(n+2,1)=b(n+1,1)*x+b(n,1); %b=(8,1)=b(7,1)2+b(6,1)=1*2+4=6 %1era interacción (desde el 2 valor hasta n-valor) for i=n+3:2*n+1; %)=9:13 b(i,1)=b(2*(n+1)-i,1)+b(i-1,1)*x; %b(9,1)=b(14-9,1)+b( 8,1)2=15 %b(13,1)=b(14-13,1)+b(12,1)2=230 end %Coeficiente for k=2*n:-1:n+2; %k=12:-1:8; for i=n+2:k; %i=8:12; b(i,1)=b(i,1)+b(i-1,1)*x; end end %Residuos for i=1:n+1 %i=1:7 R(i)=b(2*n+2-i,1); %R(1)=b(13,1) %R(7)=b( 7,1) end %Factoriales de los residuos for i=0:n %i=0:6 px0(i+1)=factorial(i)*R(i+1); %px0(1)=factorial(0)*R(1)=0!*230 %px0(2)=factorial(1)*R(2)=1!*588 end disp('La evaluacion del polinomio: ') disp(p) disp('En el punto: ') disp(x) disp('Es igual: ') disp(px0) %BISECCION Fx=input('Ingrese la funcion: ','s'); %Ej.: x.^3+4*x.^2-10 disp(' Ingreso del intervalo') a=input('Ingrese a : '); %Ej.:1 b=input('Ingrese b : '); %Ej.:2 e=input('Ingrese el error: '); %Ej.:10^(-4) x=a; Fa=eval(Fx); x=b; Fb=eval(Fx); if Fa*Fb>=0 disp('No existe solución en el intervalo') break; end c=(a+b)/2; x=c; Fc=eval(Fx); k=(1/log(2))*(log((b-a)/e)); fprintf('\n número de iteraciones: k>= \n'); disp(k)

Upload: xavier-fuentes

Post on 08-Dec-2015

212 views

Category:

Documents


0 download

DESCRIPTION

metodos númericos

TRANSCRIPT

Page 1: R 1MN

%HORNERp=input('[anxn+…+ao]: '); %[-2 4x 0x^2 -2 3 4 1] [1 4 3 -2 0 4 -2]x=input('punto a evaluar: '); %2n=length(p)-1; %n=6o=p';b=o(length(p):-1:1); %1era iteración, 1 valorb(n+2,1)=b(n+1,1)*x+b(n,1); %b=(8,1)=b(7,1)2+b(6,1)=1*2+4=6%1era interacción (desde el 2 valor hasta n-valor)for i=n+3:2*n+1; %)=9:13 b(i,1)=b(2*(n+1)-i,1)+b(i-1,1)*x; %b(9,1)=b(14-9,1)+b( 8,1)2=15 %b(13,1)=b(14-13,1)+b(12,1)2=230 end%Coeficientefor k=2*n:-1:n+2; %k=12:-1:8; for i=n+2:k; %i=8:12; b(i,1)=b(i,1)+b(i-1,1)*x; endend%Residuos for i=1:n+1 %i=1:7R(i)=b(2*n+2-i,1); %R(1)=b(13,1) %R(7)=b( 7,1) end%Factoriales de los residuosfor i=0:n %i=0:6 px0(i+1)=factorial(i)*R(i+1); %px0(1)=factorial(0)*R(1)=0!*230

%px0(2)=factorial(1)*R(2)=1!*588 enddisp('La evaluacion del polinomio: ')disp(p)disp('En el punto: ')disp(x)disp('Es igual: ')disp(px0)

%BISECCIONFx=input('Ingrese la funcion: ','s'); %Ej.: x.^3+4*x.^2-10disp(' Ingreso del intervalo')a=input('Ingrese a : '); %Ej.:1b=input('Ingrese b : '); %Ej.:2e=input('Ingrese el error: '); %Ej.:10^(-4)x=a;Fa=eval(Fx);x=b;Fb=eval(Fx);if Fa*Fb>=0 disp('No existe solución en el intervalo') break;endc=(a+b)/2;x=c;Fc=eval(Fx);k=(1/log(2))*(log((b-a)/e));fprintf('\n número de iteraciones: k>= \n');disp(k)fprintf('\n %8s %8s %8s %8s %8s %8s %8s %8s\n ','xi-1','xi-2','xi','F(a)','F(b)','F(xi)','Er','item');i=1;fprintf('\n %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.0f\n',a,b,c,Fa,Fb,Fc,abs((c-a)/c),i);while abs((c-a)/c)>=e c=(a+b)/2; x=c; Fc=eval(Fx); if Fa*Fc<0 a=a;

Page 2: R 1MN

Fa=Fa; b=c; Fc=Fb; c=(a+b)/2; x=c; Fc=eval(Fx); else b=b; Fb=Fb; a=c; Fa=Fc; c=(a+b)/2; x=c; Fc=eval(Fx); end i=i+1; fprintf('\n %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.0f\n',a,b,c,Fa,Fb,Fc,abs((c-a)/c),i); endfprintf('\nEl resultado es: %.5f\n',c);ezplot(Fx);grid on;

FALSA POSICIONc=(((a*Fb)-(b*Fa))/(Fb-Fa));while abs(Fc)>eeliminar E de la visualización

disp ('1. Derivada')disp ('2. Integral')disp ('3. Taylor')disp ('4. Calculo del factorial de un número')disp ('5. Graficar función, derivada, integral, Taylor')n=input('Escoja un numero: ')switch (n) case(1)syms('x') f=input('Ingrese la función: ') b=input('Ingrese el grado de la derivada: ')y=diff(f,b)figure(1), ezplot(f)figure(2), ezplot(y) case(2) syms('x') f=input('Ingrese la función: ')b=input('Ingrese el limite inferior')n=input('Ingrese el limite superior') y=int(f,b,n)figure(1), ezplot(f)figure(2), ezplot(y) case(3) syms('x') f=input('Ingrese la función: ') b=input('Ingrese el grado del polinomio')n=input('Ingrese el punto a calcular de la serie') y=taylor(f,b,n) figure(1), ezplot(f)figure(2), ezplot(y)case(4) n=input('Ingrese el número: ');fact=1; %nombre de mi varibalefor i=1:n %desde 1 hasta n fact= fact*i; enddisp(fact) case(5) syms('x')

Page 3: R 1MN

f=input('Ingrese la función: ')v=diff(f) y=int(f) x=taylor(f)subplot(2,2,1), ezplot(f)subplot(2,2,2), ezplot(v)subplot(2,2,3), ezplot(y)subplot(2,2,4), ezplot(x) otherwise fprintf('Error') end

%clc [borra]%clear [borra las variables]%MATRICES>> A=[1 3 5 7 9];>> B=[1,3,5,7,9];>> C=A+B%TraspuestaA'>> M=A';>> N=A*M>> A=[1 3 5 7 9; 1 -1 2 3 7]A = 1 3 5 7 9 1 -1 2 3 7>> A=[1 3 5 7 9; 1 -1 2 3 7; -1 0 3 4 2];>> zeros(2,3)ans = 0 0 0 0 0 0>> zeros(3)>> %Identidad>> eye(3)ans = 1 0 0 0 1 0 0 0 1>> eye(2,3)>> %PolinomioA=[1 2 1 1 1];>> %=x^4 x^3 x^2 x^1 x^0 = x^4 +2x^3 +x^2 +x^1 +1>> % help eval>> polyval(A,2)%Evalúa el polinomio en el valor dado>>polyval(A,0)ans = 1>> %Ingreso de una función con variables simbólicas>> y=x^3+2*x^2+x+1; %??? Undefined function or variable 'x'.>> syms x>> % Declara la variable x como simbólica>> y=x^3+2*x^2+x+1;>> y='c^3+2*c^2+c+1'>> y=x.^3+2*x.^2+x+1>> A=[1 2 3 4];>> roots(A)ans = -1.6506 -0.1747 + 1.5469i -0.1747 - 1.5469i>> %Raicez reales e imaginarias>> %Multiplicación de polinomios>> A=[5 6 7 8 9];>> B=[5 6 7 8 9];

Page 4: R 1MN

>> r=conv(A,B)r = 25 60 106 164 235 220 190 144 81>> %25x^9+...144x+81>> r=deconv(A,B)%divider = 1>> A=[1 3];>> b=[1 2];>> r=conv(A,b)r = 1 5 6>> length(r)%Número de columnas del vectorans = 3>> size(r)%Número de columnas y filas ans = 1 3>> [m,n]=size(r)%m y n variables; size: orden de la matrizm = 1n = 3>> %m: número de filas y n: número de columnas>> linspace(1,100,10)%genera un vector interespaciado: (V inicial,V final,número de elementos)ans = 1 12 23 34 45 56 67 78 89 100>> logspace(1,100,12)ans =

1.0e+100 * 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1.0000>> rand(m)%Matriz 3x3ans = 0.8147>> rand(2,5) %Matriz 2x5ans = 0.9058 0.9134 0.0975 0.5469 0.9649 0.1270 0.6324 0.2785 0.9575 0.1576>> x=pi*[-10:0.1:10]; %[desde: espaciado: hasta]>> y=cos(x);>> plot(x,y)>> %grafica x-y>> subplot(2,2,1), plot(x,y)%subplot(filas, columnas, posición)>> %divide la pantalla>> z=sin(x);>> w=-cos(x);>> q=-sin(x);>> subplot(2,2,1), plot(x,y)>> subplot(2,2,2), plot(x,z)>> subplot(2,2,3), plot(x,w)>> subplot(2,2,4), plot(x,q)>> title('cos(x)')>> xlabel('t')>> ylabel('cos(x)')>> subplot(2,2,2), plot(x,z)>> legend('pos 2')>> subplot(1,2,2), plot(x,z,'--')

>> [x,y]=meshgrid(-3:0.1:3);>> z=x.^2+y.^2-2*x*y+3*x.^2-y+1; >> surf(x,y,z)%Gráfica en 3D>> figure(1)%Se graba en la figura 1>> surf(x,y,z)>> figure(2)>> contour(x,y,z,25)%contour: gráfico de nivel, profundidad; 25:numero de líneas

>> y='x^3+x'y =x^3+x>> diff(y) ans =3*x^2 + 1>> syms('x')

Page 5: R 1MN

>> taylor(cos(x))ans =x^4/24 - x^2/2 + 1

EN EL EDITOR (New scrib)function y = funcua(a,dx,b)a=input('Ingrese el límite inferior del intervalo: ')%Se guarda lo que el usuario ingresadx=input('Ingrese los pasos de los valores de x: ')b=input('Ingrese el límite superior del intervalo: ')x=(a:dx:b);%x:vectory=x.^2+1;disp('La grafica de la función se encuentra en otra ventana')plot(x,y)

En el comand Windows>> funcuaIngrese el límite inferior del intervalo: -10a =-10Ingrese los pasos de los valores de x: 1dx = 1Ingrese el límite superior del intervalo: 10b =10La grafica de la función se encuentra en otra ventanaans = 101 82 65 50 37 26 17 10 5 2 1 2 5 10 17 26 37 50 65 82 101%(y se grafica una función parabolica)>> syms('x')>> f='x^3+x'>> ezplot(f) %grafica la función, no necesita de valores, Matlab le da los convenientes

function y = trabajo(f)syms('x')f=input('Ingrese su funcion; ');%Ej cos(v)y=diff(f)subplot(1,2,1), ezplot(f)subplot(1,2,2), ezplot(y)

syms('x')>> f=x^3+2*x^2+2*x+1; >> a=limit(f,x,0)a =1>> b=diff(f)b =3*x^2 + 4*x + 2>> c=diff(f,2)c =6*x + 4>> f=x^3+2*x^2+2*x+1>> d=diff(f,3)d =6>> e=taylor(f,3,2)e =22*x + 8*(x - 2)^2 - 23>> y=cos(x) >> g=taylor(y,7,0)g =- x^6/720 + x^4/24 - x^2/2 + 1>> h=subs(y,x,0)h = 1>> z=diff(f,2)z =6*x + 4>> i=solve(z)i =-2/3>> j=solve(f)j = -1 - 1/2 + (3^(1/2)*i)/2 - 1/2 - (3^(1/2)*i)/2

Page 6: R 1MN

>> i=real(j)i = -1 -1/2 -1/2>> subplot(2,2,1),ezplot(f)>> subplot(2,2,2),ezplot(b)>> subplot(2,2,3),ezplot(g)>> subplot(2,2,4),ezplot(y)

Ingrese la función: x.^3+4*x.^2-10Ingrese a : 1Ingrese b : 2Ingrese el error: 10^(-4) número de iteraciones: k>= 13.2877 xi-1 xi-2 xi F(a) F(b) F(xi) Er item 1.00000 2.00000 1.50000 -5.00000 14.00000 2.37500 0.33333 1 V 1.00000 1.50000 1.25000 -5.00000 14.00000 -1.79688 0.20000 2 F 1.25000 1.50000 1.37500 -1.79688 14.00000 0.16211 0.09091 3 V 1.25000 1.37500 1.31250 -1.79688 14.00000 -0.84839 0.04762 4 F 1.31250 1.37500 1.34375 -0.84839 14.00000 -0.35098 0.02326 5 F 1.34375 1.37500 1.35938 -0.35098 14.00000 -0.09641 0.01149 6 F 1.35938 1.37500 1.36719 -0.09641 14.00000 0.03236 0.00571 7 V 1.35938 1.36719 1.36328 -0.09641 14.00000 -0.03215 0.00287 8 F 1.36328 1.36719 1.36523 -0.03215 14.00000 0.00007 0.00143 9 V 1.36328 1.36523 1.36426 -0.03215 14.00000 -0.01605 0.00072 10 F 1.36426 1.36523 1.36475 -0.01605 14.00000 -0.00799 0.00036 11 F 1.36475 1.36523 1.36499 -0.00799 14.00000 -0.00396 0.00018 12 F 1.36499 1.36523 1.36511 -0.00396 14.00000 -0.00194 0.00009 13 FEl resultado es: 1.36511

MÉTODO DE LA FALSA POSICIÓN CLASEIngrese la funcion: cos(x)-xIngrese a : 0Ingrese b : 1Ingrese el error: 0.00001 i a b xi F(a) F(b) F(xi) 1 0.00000 1.00000 0.68507 1.00000 -0.45970 0.08930 2 0.68507 1.00000 0.73630 0.08930 -0.45970 0.00466 3 0.73630 1.00000 0.73895 0.00466 -0.45970 0.00023 4 0.73895 1.00000 0.73908 0.00023 -0.45970 0.00001 5 0.73908 1.00000 0.73908 0.00001 -0.45970 0.00000 La raiz es 0.739085

funcion:[anxn+…+ao]: [1 4 3 -2 0 4 -2]punto a evaluar: 2La evaluacion del polinomio: 1 4 3 -2 0 4 -2En el punto: 2Es igual: 230 588 1240 2052 2472 1920 720

function [y,z]= suma (a,b)a=input('Ingrese un numero: ')b=input('Ingrese el 2do numero: ')c=a+b;d=a*b;if a>b c=a/belse if a==10 d=-(a+b) else d=(a+b) endend

%En el Editorfunction [y,z]= suma (a,b)a=input('Ingrese un numero: ')b=input('Ingrese el 2do numero: ')c=a+bd=a*b

Page 7: R 1MN

New scriba=input('Ingrese un numero: ')b=input('Ingrese el 2do numero: ')c=a+b;d=a*b;

function [y,z]= suma (a,b)%[Salidas]=Archivo(Entradas)a=input('Ingrese un numero: ')b=input('Ingrese el 2do numero: ')c=a+b;d=a*b;if a>b c=a+bend

COMPUESTOfunction [y,z]= suma (a,b)a=input('Ingrese un numero: ')b=input('Ingrese el 2do numero: ')c=a+b;d=a*b;if a>b c=a+belse d=a*bend***

**function [y,z]= suma (a,b,n)fprintf('1. Suma de dos numeros')fprintf('2. Multiplicación de dos numeros')fprintf('3. División de dos numeros')n=input('Escoja un numero: ')if n==1 a=input('Ingrese un numero: ')b=input('Ingrese el 2do numero: ')c=a+belse if n==2 a=input('Ingrese un numero: ')b=input('Ingrese el 2do numero: ')c=a*b else if n==2 a=input('Ingrese un numero: ')b=input('Ingrese el 2do numero: ')c=a/b else if n>3 fprintf('No es una opción') endendendend****function [c,d]= suma3 (a,b,n)fprintf('1. Suma de dos numeros')fprintf('2. Multiplicación de dos numeros')fprintf('3. División de dos numeros')n=input('Escoja un numero: ')switch (n) case(1) a=input('Ingrese un numero: ')b=input('Ingrese el 2do numero: ') c=a+b case(2)

Page 8: R 1MN

a=input('Ingrese un numero: ')b=input('Ingrese el 2do numero: ')c=a*b case(3) a=input('Ingrese un numero: ')b=input('Ingrese el 2do numero: ')c=a/b otherwise fprintf('Error') end

A=5;B=8;if A==B disp('A=B')elseif A>B disp('A es mayor que B')elseif A<B disp('A es menor que B')end switch A case 0 disp('A es 0') case 1 disp('A es 1') otherwise disp('A distinto de 0 y 1')end v=[];for n=3:1:7 v=[v n^2];endv% concatenando los numeros cuadrados en la matriz vacia v

f=input('Ingrese la funcion f(x) : ','s'); x0=input('Ingrese el valor del intervalo inferior de x : '); x1=input('Ingrese el valor del intervalo superior de x : '); e=input('Ingrese el error : '); syms ('x'); a=subs(f,x,x0); b=subs(f,x,x1); xi=((x1*b)-(x0*a))/b-a; fxi=subs(f,x,xi); i=1; while fxi>e x=x0; g=eval(f); x=x1; gg=eval(f); xi=x1-((gg*(x0-x1))/(g-gg)); fxi=abs((xi-x1)/xi)*100; x0=x1; x1=xi; i=i+1; fprintf('\n %8.5f %8.5f %8.5f \n',x1,x0,xi); end disp('El resulatado es:') disp(xi)

disp(' MÉTODO DE LA FALSA POSICIÓN CLASE')disp('Por: Xavier Fuentes y Saul Mendez')Fx=input('Ingrese la funcion: ','s'); %Ej.: cos(x)-xa=input('Ingrese a : '); %Ej.:0b=input('Ingrese b : '); %Ej.:1

Page 9: R 1MN

e=input('Ingrese el error: '); %Ej.:0.00001syms('x');Fa=subs(Fx,x,a);%Evaluacion en el punto aFb=subs(Fx,x,b);%Evaluacion en el punto bfprintf('\n %8s %8s %8s %8s %8s %8s %8s\n ','i','a','b','xi','F(a)','F(b)','F(xi)');if Fa*Fb<0 Fc=1; i=1;while abs(Fc)>e %Error de la funcion < e xi=(b*Fa-a*Fb)/(Fa-Fb);%Ingresamos la formula de xi x=xi; Fc=eval(Fx);%Evaluamos la función en xi fprintf('\n %8.0f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f\n',i,a,b,xi,Fa,Fb,Fc); if Fa*Fc<=0 b=xi; Fb=Fc; else a=xi; Fa=Fc; end i=i+1;endfprintf('\n La raiz es %.6f \n',xi);else fprintf('\nEl resultado no contiene la raiz \n');endezplot(Fx);grid on;