prg calculo

31
UNIVERSIDAD NACIONAL DEL CENTRO DEL PERÚ FACULTAD DE INGENIERÍA QUÍMICA FACULTAD DE INGENIERÍA QUÍMICA LENGUAJES DE LENGUAJES DE PROGRAMACION PROGRAMACION CATEDRÁTICO: CATEDRÁTICO: Ing. LÓPEZ GUTIERREZ, Ing. LÓPEZ GUTIERREZ, Helmer Helmer PRESENTADO POR: PRESENTADO POR: MARAVI RIOS, Ramiro Alex MARAVI RIOS, Ramiro Alex

Upload: elvis-castro

Post on 26-Oct-2014

150 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Prg Calculo

UNIVERSIDAD NACIONAL DEL CENTRO DEL PERÚ

FACULTAD DE INGENIERÍA QUÍMICAFACULTAD DE INGENIERÍA QUÍMICA

LENGUAJES DELENGUAJES DE PROGRAMACIONPROGRAMACION

CATEDRÁTICO:CATEDRÁTICO: Ing. LÓPEZ GUTIERREZ, HelmerIng. LÓPEZ GUTIERREZ, Helmer

PRESENTADO POR:PRESENTADO POR: MARAVI RIOS, Ramiro AlexMARAVI RIOS, Ramiro Alex

HUANCAYO – PERUHUANCAYO – PERU

MARZO-2009MARZO-2009

Page 2: Prg Calculo

DEDICATORIA

El presente trabajo lo dedico a todos los ingenieros de la Facultad de Ingeniería Química por la enseñanza que nos brindan.

Ejercicio 1%PROGRAMA PARA OBTENER LA DERIVADAclc;disp('==================================================')disp('-----------------***OBTENCION DE LA DERIVADA***---------------')disp('---Y GRAFICAS DE LAS FUNCIONES: INICIAL Y DERIVADA---’)disp('==================================================')

Page 3: Prg Calculo

syms x y %syms define a X e Y como variablesA=input('ingrese funcion:') %pide la funcion a ser evaluadaB=diff(A); %determina la derivada de la funcion inicialdisp('la derivada de la funcion será:')disp(B) %muestra en la pantalla el resultado de la derivada de la funcion inicialdisp('A continuacion se mostrarán las graficas: ')disp('Primero de la Funcion inicial')disp('Luego de la funcion derivada')subplot(1,2,1), ezplot(y-A) %la funcion ezplot muestra el grafico panoramico de la funcionsubplot(1,2,2), ezplot(y-B) %El ezplot muestra el titulo por defecto

SALIDA:===================================================----------------***OBTENCION DE LA DERIVADA***----------------------Y GRAFICAS DE LAS FUNCIONES: INICIAL Y DERIVADA----===================================================

ingrese funcion:3*x+x^2

A =

3*x+x^2

la derivada de la funcion será:3+2*x A continuacion se mostrarán las graficas: Primero de la Funcion inicialLuego de la funcion derivada

Ejercicio 2%PROGRAMA PARA HALLAR DERIVADAS PARCIALES y SU GRAFICAclc;disp('=================================================')disp('---------------------OBTENCION DE LA DERIVADA-------------------')disp('----------------------PARCIAL RESPECTO A X e Y----------------------')disp('=================================================') syms x y z %la funcion syms define como variables a X e Y

Page 4: Prg Calculo

A=input('ingrese funcion:');B=diff(A,x)C=diff(A,y)disp('la derivada respecto a x es:'),;disp(diff(A,x)) %calcula la derivada parcial respecto a Xdisp('la derivada respecto a y es:');disp(diff(A,y)) %calcula la derivada parcial respecto a ysubplot(1,2,1), ezplot(z-B) %la funcion ezplot muestra el grafico panoramico de la funcionsubplot(1,2,2), ezplot(z-C) %El ezplot muestra el titulo por defecto

SALIDA:===================================================---------------------OBTENCION DE LA DERIVADA---------------------------------------------PARCIAL RESPECTO A X e Y------------------------===================================================

ingrese funcion:x^2+y^3la derivada respecto a x es:2*x la derivada respecto a y es:3*y^2

Ejercicio 3 %METODO de NEWTON RAPHSON mas rápido converge la función(llega a la solucion)clc;disp('===========================================')disp('-------------* METODO DE NEWTON RAPHSON *---------')disp('===========================================')nombre_f=input(' ingrese función asociada f(x)=','s');x0=input(' ingrese valor inicial:');fprintf('\n');

Page 5: Prg Calculo

fprintf('iter aproximación g(x) error\n');i=1;e=1;delta=0.001;while e>=3E-12 & i<=18 x=x0; fx0=eval(nombre_f); x=x0-delta; df1=eval(nombre_f); x=x0+delta; df2=eval(nombre_f); dfx0=(df2-df1)/(2*delta); r=x0-(fx0/dfx0); e=abs((r-x0)/r); fprintf('%3.0f %10.6f %10.6f %10.6f\n',i,x0,r,e); x0=r; i=i+1;enddisp('-----------------------------------------')fprintf(' la raiz es:%10.9f\n',x0);disp('-----------------------------------------')

SALIDA:===================================================-------------------*METODO DE NEWTON RAPHSON*------------------===================================================

ingrese funcion asociada f(x)=5*x - x^3ingrese valor inicial:2

iter aproximación g(x) error 1 2.000000 2.285714 0.125000 2 2.285714 2.237640 0.021484 3 2.237640 2.236070 0.000702 4 2.236070 2.236068 0.000001 5 2.236068 2.236068 0.000000---------------------------------------------- la raiz es:2.236067977---------------------------------------------->>

Ejercicio 4%METODO de VONN MISSESclc;disp('==========================================')disp('---------------* METODO DE VONN MISSES *-------------')disp('==========================================')nombre_f=input(' ingrese funcion asociada f(x)=','s');x0=input(' ingrese valor inicial:');fprintf('\n'); fprintf('iter aproximacion g(x) error\n');

Page 6: Prg Calculo

i=1;e=1;delta=0.001;

x=x0; fx0=eval(nombre_f); x=x0-delta; df1=eval(nombre_f); x=x0+delta; df2=eval(nombre_f); dfx0=(df2-df1)/(2*delta); while e>=3E-12 & i<=500 x=x0; fx0=eval(nombre_f); r=x0-(fx0/dfx0); e=abs((r-x0)/r); fprintf('%3.0f %10.6f %10.6f %10.6f\n',i,x0,r,e); x0=r; i=i+1;enddisp('-----------------------------------------')fprintf(' la raiz es:%10.9f\n',x0);disp('-----------------------------------------')

SALIDA:===================================================-----------------------* METODO DE VONN MISSES *--------------------=================================================== ingrese funcion asociada f(x)=5*x-x^3 ingrese valor inicial:2

iter aproximacion g(x) error 1 2.000000 2.285714 0.125000 2 2.285714 2.212412 0.033132 3 2.212412 2.245672 0.014811 4 2.245672 2.231863 0.006187 5 2.231863 2.237853 0.002676 6 2.237853 2.235300 0.001142 7 2.235300 2.236397 0.000490 8 2.236397 2.235927 0.000210 9 2.235927 2.236128 0.000090 10 2.236128 2.236042 0.000039 11 2.236042 2.236079 0.000017 12 2.236079 2.236063 0.000007 13 2.236063 2.236070 0.000003 14 2.236070 2.236067 0.000001 15 2.236067 2.236068 0.000001 16 2.236068 2.236068 0.000000 17 2.236068 2.236068 0.000000

Page 7: Prg Calculo

18 2.236068 2.236068 0.000000 19 2.236068 2.236068 0.000000 20 2.236068 2.236068 0.000000 21 2.236068 2.236068 0.000000 22 2.236068 2.236068 0.000000 23 2.236068 2.236068 0.000000 24 2.236068 2.236068 0.000000 25 2.236068 2.236068 0.000000 26 2.236068 2.236068 0.000000 27 2.236068 2.236068 0.000000 28 2.236068 2.236068 0.000000 29 2.236068 2.236068 0.000000 30 2.236068 2.236068 0.000000---------------------------------------------- la raiz es:2.236067977----------------------------------------------

Ejercicio 5%PROGRAMA PARA INTEGRALES DEFINIDAS POR EL METODO DE SIMPSONclc;disp('===================================================')disp('---------------------------* METODO DE SIMPSON *-----------------------')disp('===================================================')syms xA=input('ingrese función:')B=input('ingrese primer limite:')C=input('ingrese segundo limite:')

D=subs(A,B); %Evalúa la función A para el valor de BE=subs(A,C); %Evalúa la función A para el valor de CD=(B+C)/2;F=subs(A,D); %Evalúa la función para la semisuma de valoresG=(C-B)*(D+E+4*F)/6;disp('-------------------------------')fprintf('La solución es: %10.6f\n',G);disp('-------------------------------')

SALIDA:===================================================---------------------------* METODO DE SIMPSON *------------------------===================================================

ingrese funcion:log(x/2)-x^3+x

A = log(1/2*x)-x^3+x

Page 8: Prg Calculo

ingrese primer limite:4

B = 4

ingrese segundo limite:2

C = 2

----------------------------------La solución es: 32.459380----------------------------------

Ejercicio 6%METODO DEL PUNTO FIJOclc;disp('=====================================================')disp('--------------------------* METODO DEL PUNTO FIJO *----------------------')disp('=====================================================')clearformat short

F=input('ingrese función:','s');x0=input('ingrese aproximación inicial:');T=input('ingrese tolerancia de error:');N=input('ingrese número máximo de iteraciones:');

a1=subs(F,x0);a2=subs(F,a1);if abs(a1-x0) <= abs(a2-a1) 'el método no convergerá' breakelse 'Felicidades el método a convergido’end

i=1;x1=subs(F,x0);while abs(x1-x0)>T & i<N; x0=x1; x1=subs(F,x0); i=i+1;end

Page 9: Prg Calculo

if i==N fprintf('el método fracasó después de %2.0f iteraciones',i)else fprintf('el método tuvo éxito después de %2.0f iteraciones',i) fprintf('\n') fprintf( ' iter Aproximación Raíz') fprintf('\n') disp([ i x0 x1])

end

SALIDA:==================================================----------------------* METODO DEL PUNTO FIJO *----------------------==================================================

ingrese función:x^2ingrese aproximación inicial:0.01ingrese tolerancia de error:0.001ingrese número máximo de iteraciones:50

ans =

Felicidades el metodo a convergido

el método tuvo éxito despues de 2 iteraciones iter Aproximacion Raiz 2.0000 0.0001 0.0000

Ejercicio 7%Este programa sirve para hallar el siguiente la siguiente integral definida:

%ingresando por teclado el límite superior (a) y el limite inferior (b): clc;disp('=======================================================')disp('-------PROGRAMA PARA HALLAR LA INTEGRAL DE sen(ln x)----------')disp('=======================================================')a=input('ingrese el limite superior:');b=input('ingrese el limite inferior:'); if a>b;%SI r=cos(log(a))-cos(log(b)); disp('el resultado de la integral es:') disp(r)else %CASO CONTRARIO

Page 10: Prg Calculo

r=tan(a)-tan(b); disp('el resultado de la integral es:') disp(r) end %FIN disp('=====================================')disp('PROGRAMA TERMINADO')disp('=====================================')

SALIDA==================================================-----PROGRAMA PARA HALLAR LA INTEGRAL DE sen(ln x)----==================================================

ingrese el limite superior:5ingrese el limite inferior:8el resultado de la integral es: 3.4192

=====================================PROGRAMA TERMINADO=====================================

Ejercicio 8%Este programa sirve para hallar la siguiente integral definida:

%ingresando por teclado el limite superior (a) y el limite inferior (b)clc;disp('=======================================================')disp('----PROGRAMA PARA HALLAR LA INTEGRAL DE (sec(x))^2-----')disp('=======================================================')a=input('ingrese el limite superior:');b=input('ingrese el limite inferior:');if a>b; r=tan(a)-tan(b); disp('el resultado de la integral es:') disp(r)else r=tan(b)-tan(a); disp('el resultado de la integral es:') disp(r)enddisp('=====================')disp('FIN DEL PROGRAMA')disp('=====================')

SALIDA:=======================================================--------PROGRAMA PARA HALLAR LA INTEGRAL DE (sec(x))^2--------=======================================================

Page 11: Prg Calculo

ingrese el limite superior:5ingrese el limite inferior:12el resultado de la integral es: 2.7447

=====================FIN DEL PROGRAMA=====================Ejercicio 9%este programa sirve para hallar el siguiente la siguiente integral definida :

%ingresando por teclado el límite superior (a) y el limite inferior (b): clc;disp('=====================================================')disp('--------PROGRAMA PARA HALLAR LA INTEGRAL DE sen(ln x)-------- ')disp('=====================================================')a=input('ingrese el limite superior:');b=input('ingrese el limite inferior:'); if a>b r=cos(log(a))-cos(log(b)); disp('el resultado de la integral es:') disp(r)else r=tan(a)-tan(b); disp('el resultado de la integral es:') disp(r) enddisp('=====================')disp('FIN DEL PROGRAMA')disp('=====================')

SALIDA:=================================================----PROGRAMA PARA HALLAR LA INTEGRAL DE sen(ln x)--- =================================================

ingrese el limite superior:5ingrese el limite inferior:8el resultado de la integral es: 3.4192

=====================FIN DEL PROGRAMA=====================

Ejercicio 10%Programa para integrar por el método del trapecio clc,

Page 12: Prg Calculo

disp('================================================')disp('------------------------* METODO DEL TRAPECIO *----------------------')disp('================================================')fprintf('\n');f=input('Ingrese la función a integrar f(x) =','s');a=input('Ingrese limite inferior :');b=input('Ingrese limite superior :');n=input('Ingrese números de trapecios a considerar en la integración :');xmin=a-1;xmax=b+1;h=(b-a)/n;x=a:h:b;fx=eval(f);y=abs(fx);A=y(1)+y(n+1);B=2*sum(y(2:n));integral=(h/2)*(A+B);fprintf('el área es:%10.9f\n',integral);%graficaxp=xmin:0.2:xmax;x=xp;yp=eval(f);plot(xp,yp,'g');hold onx=a:0.05:b;y=eval(f);bar(x,y,'r');grid

SALIDA:==============================================--------------------* METODO DEL TRAPECIO *-------------------==============================================

Ingrese la función a integrar f(x) =sqrt(1+x.^2)Ingrese limite inferior :2Ingrese limite superior :3Ingrese números de trapecios a considerar en la integración :8el área es:2.694824624

Page 13: Prg Calculo

Ejercicio 12clc;disp('=================================================')disp('------------------------* METODO DE SIMPSON *-------------------------')disp('=================================================') f=input('ingrese la función integral f(x)=','s');a=input('ingrese limite inferior:');b=input('ingrese limite superior:');fprintf('ingrees numero de trapecios a \n');n=input('considerar en la integración:')n=2*n;xmin=a-1; xmax=b+1;h=(b-a)/n;x=a:h:b;fx=eval(f);y=abs(fx);suma1=y(1)+y(n+1);suma2=3*sum(y(2:3:n-1));suma3=3*sum(y(3:3:n));suma4=2*sum(y(4:3:n-2));suma=suma1+suma2+suma3+suma4;integral=(3/4)*h*suma;fprintf('El area es:%10.9f\n',integral);%graficaxp=xmin:0.2:xmax;x=xpyp=eval(f);plot(xp,yp,'g');hold onx=a:0.05:b;y=eval(f);bar(x,y,'r');grid on

SALIDA:==============================================----------------------* METODO DE SIMPSON *--------------------==============================================

ingrese la funcion integral f(x)=sqrt(x.^5+x.*3+2)ingrese limite inferior:2ingrese limite superior:4ingrees numero de trapecios a considerar en la integracion:40

n = 40

El area es:65.038996358

Page 14: Prg Calculo

x = Columns 1 through 6

1.0000 1.2000 1.4000 1.6000 1.8000 2.0000

Columns 7 through 12

2.2000 2.4000 2.6000 2.8000 3.0000 3.2000

Columns 13 through 18

3.4000 3.6000 3.8000 4.0000 4.2000 4.4000

Columns 19 through 21

4.6000 4.8000 5.0000

Ejercicio 13%programa para determinar la raíz de una función por el método de la secanteclc;disp('==================================================')disp('--------------------------* METODO DE LA SECANTE *--------------------')disp('==================================================') format bankF=input('ingrese función:','s');x0=input('ingrese primera aproximación inicial:');x1=input('ingrese segunda aproximación inicial:');T=input('ingrese tolerancia de error:');N=input('ingrese número máximo de iteraciones:');

Page 15: Prg Calculo

i=2;x2=x1-subs(F,x1)*(x1-x0)/(subs(F,x1)-subs(F,x0));while abs(x2-x1)>T & i<N; x0=x1; x1=x2; x2=x1-subs(F,x1)*(x1-x0)/(subs(F,x1)-subs(F,x0)); i=i+1;endif i==N fprintf('El método fracasó despues de %2.0f iteraciones',i)else fprintf('El método tuvo éxito despues de %2.0f iteraciones',i) fprintf('\n') disp('---------------------------------------------') fprintf( '......iteraciones.......g(x)..........error') fprintf('\n') disp([ i x0 x1 ]) disp('--------------------------------------------')end

SALIDA:===========================================----------------* METODO DE LA SECANTE *---------------===========================================

ingrese función:sqrt(x)+2*xingrese primera aproximación inicial:1ingrese segunda aproximación inicial:4ingrese tolerancia de error:0.001ingrese número máximo de iteraciones:1000El método tuvo éxito despues de 50 iteraciones

--------------------------------------------------------------------iteraciones.......g(x)..........error 50.00 -0.00 0.00

--------------------------------------------------------------------

Ejercicio 14clc, clear alldisp('=========================================================')disp('------------* Método Numérico Aproximación por Mínimos Cuadrados *--------')disp('=========================================================') N=input(' Ingrese el grado de polinomio a aproximar: ');X=input(' Ingrese los valores x(i): ');F=input(' Ingrese los valores f(xi): ');M=length(X);A=zeros(N+1);H=zeros(N+1,N+1);A(1,1)=M;

Page 16: Prg Calculo

for l=1:2*N; s(l)=sum((X.^l));endfor l=0:N; b(l+1)=sum(F.*(X.^l));endA(1,2:N+1)=s(1:N);for l=2:N+1; A(l,1:N+1)=s(l-1:N+l-1);endb=b';DET=1;I=1;format shortwhile I<=(N) DET=DET*A(I,I); if DET == 0 disp(' "Hay un 0 en la diagonal principal" '); break else K=I+1; while K<=N+1 J=I+1; while J<=N+1 A(K,J)=A(K,J)-A(K,I)*A(I,J)/A(I,I); J=J+1; H(K:N+1,I)=A(K:N+1,I); end b(K)=b(K)-A(K,I)*b(I)/A(I,I); K=K+1; end I=I+1; endendB=A;A=A-H;DET=DET*A(N+1,N+1);if DET==0 disp(' "Hay un 0 en la diagonal principal" '); breakelse x(N+1)=b(N+1)/A(N+1,N+1); I=N;endwhile I>=1 x(I)=b(I); J=1+I; while J<=N+1 x(I)=x(I)-A(I,J)*x(J); J=J+1; end x(I)=x(I)/A(I,I); I=I-1;enddisp(' ')disp('-----------------------------------------')

Page 17: Prg Calculo

disp(' Solución: ')for l=0:N;fprintf(' El coeficiente (%d) es: %f\n',l,x(l+1))disp('-----------------------------------------')

end SALIDA:

====================================================------* Método Numérico Aproximación por Mínimos Cuadrados *-------====================================================

Ingrese el grado de polinomio a aproximar: 3 Ingrese los valores x(i): [-1 0 1 2] Ingrese los valores f(xi): [4 8 16 34] --------------------------------------------------- Solución: El coeficiente (0) es: 8.000000 El coeficiente (1) es: 5.000000 El coeficiente (2) es: 2.000000 El coeficiente (3) es: 1.000000---------------------------------------------------

Ejercicio 15% programa para realizar el ajuste a curvas de la parábola.clcdisp('========================================================')disp('----* PROGRAMA PARA HACER EL AJUSTE DE CURVAS DE LA * ----')disp('-------------------------------* PARABOLA *--------------------------------------------')disp('========================================================')a=input('ingrese la matriz a:');b=input('ingrese la matriz b:');c=sum(a);d=sum(a.^2);e=sum(a.^3);f=sum(a.^4);g=sum(b);h=sum(a.*b);i=sum((a.^2).*b);x=inv([11,c,d;c,d,e;d,e,f])*[g;h;i]

SALIDA:

====================================================----* PROGRAMA PARA HACER EL AJUSTE DE CURVAS DE LA *------------------------------------* PARÁBOLA *------------------------------------====================================================

ingrese la matriz a:[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]

Page 18: Prg Calculo

ingrese la matriz b: [0,0.1002,0.2013,0.3045,0.4108,0.5211,0.6367,0.7586,0.8881,1.0265,1.1752]x = 0.0067 0.8955 0.2659

Ejercicio 17%Este programa sirve para hacer el ajuste de curvas de un " polinomio de tercer grado"clcdisp('=========================================================')disp('--* PROGRAMA HACER EL AJUSTE DE CURVAS DE UN POLINOMIO *--')disp('----------------------------------* DE TERCER GRADO *----------------------------------')disp('=========================================================')pero ejemplo x^3 +x-2=0a=input('ingrese la matriz a:');b=input('ingrese la matriz b:');c=sum(a);d=sum(a.^2);e=sum(a.^3);f=sum(a.^4);h=sum(a.^5);i=sum(a.^6);j=sum(b);k=sum(a.*b);l=sum((a.^2).*b);m=sum((a.^3).*b);x=inv([11,c,d,e;c,d,e,f;d,e,f,h;e,f,h,i])*[j;k;l;m]

SALIDA:

Page 19: Prg Calculo

=========================================================--* PROGRAMA HACER EL AJUSTE DE CURVAS DE UN POLINOMIO *----------------------------------- * DE TERCER GRADO * --------------------------------

==========================================================

ingrese la matriz a:[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]ingrese la matriz b:[0,0.1002,0.2013,0.3045,0.4108,0.5211,0.6367,0.7586,0.8881,1.0265,1.1752]x = -0.0001 1.0046 -0.0201

0.1907

Ejercicio 18%PROGRAMA PARA HALLAR LA RAIZ DE UN SISTEMA DE ECUACIONES %POR EL METODO DE LA BISECCIONdisp('==============================================================')disp('------------------------------------* MÉTODO DE LA BISECCION *----------------------------')disp('-------------------------------- ---* PARA DETERMINAR RAICES *----------------------------')disp('==============================================================')

fprintf(‘\n’); nombre_f=input(‘ ingrese funcion asociada f(x)= ‘,’s’);a=input(‘ ingrese limite inferior: ‘);b=input(‘ ingrese limite superior: ‘);fprintf(‘\n’); fprintf(‘ it a b aprox error\n’);

Page 20: Prg Calculo

i=1;e=1; r=0; while e>=3E-6 & i<=10 va=r; r=(a+b)/2; x=a;fa=eval(nombre_f); x=b;fb=eval(nombre_f); x=r;fr=eval(nombre_f); fprintf(‘ %3.0f %10.6f %10.6f %10.6f’,I,a,b,r); if fa*fr<=0 b=r; e=abs((r-va)/r); fprintf(‘ %10.6f\n’,e); else a=r; e=abs((r-va)/r); fprintf(‘ %10.6f\n’,e); end i=i+1; end disp('------------------------------------------------------------') fprintf('\n'); fprintf('la raiz es: %10.9f\n', r); disp('------------------------------------------------------------')

SALIDA:=================================================

=======--------------------------* MÉTODO DE LA BISECCION *-------------------------------------------------- ---* PARA DETERMINAR RAICES *----------------------------

========================================================

ingrese funcion asociada f(x)= 5*x^2ingrese limite inferior: 0.1ingrese limite superior: 0.5

it a b aprox error 1 0.100000 0.500000 0.300000 1.000000 2 0.300000 0.500000 0.400000 0.250000 3 0.400000 0.500000 0.450000 0.111111 4 0.450000 0.500000 0.475000 0.052632 5 0.475000 0.500000 0.487500 0.025641 6 0.487500 0.500000 0.493750 0.012658 7 0.493750 0.500000 0.496875 0.006289 8 0.496875 0.500000 0.498437 0.003135 9 0.498437 0.500000 0.499219 0.001565 10 0.499219 0.500000 0.499609 0.000782-------------------------------------------------------------- la raiz es: 0.49960937-------------------------------------------------------------------

Ejercicio 19%POR EL METODO DE LA REGLA FALSAdisp('================================================================')

Page 21: Prg Calculo

disp('------------------------------------* MÉTODO DE LA REGLA FALSA *----------------------------')disp('================================================================')

fprintf('\n'); nombre_f=input(' ingrese funcion asociada f(x)= ','s');a=input(' ingrese limite inferior: ');b=input(' ingrese limite superior: ');fprintf('\n'); fprintf(' it a b aprox error\n');i=1;e=1; r=0; while e>=3E-6 & i<=10 va=r; r=(a+b)/2; x=a;fa=eval(nombre_f); x=b;fb=eval(nombre_f); x=r;fr=eval(nombre_f); fprintf(' %3.0f %10.6f %10.6f %10.6f',i,a,b,r); if fa*fr<=0 b=r; e=abs((r-va)/r); fprintf(' %10.6f\n',e); else a=r; e=abs((r-va)/r); fprintf(' %10.6f\n',e); end i=i+1; enddisp('------------------------------------------------------------') fprintf('\n'); fprintf('la raiz es: %10.9f\n', r);disp('------------------------------------------------------------')

SALIDA:

==============================================================--------------------------------* MÉTODO DE LA REGLA FALSA *----------------------------==============================================================

ingrese funcion asociada f(x)= sqrt(x)+2*xingrese limite inferior: 2ingrese limite superior: 6

it a b aprox error 1 2.000000 6.000000 4.000000 1.000000 2 4.000000 6.000000 5.000000 0.200000 3 5.000000 6.000000 5.500000 0.090909

Page 22: Prg Calculo

4 5.500000 6.000000 5.750000 0.043478 5 5.750000 6.000000 5.875000 0.021277 6 5.875000 6.000000 5.937500 0.010526 7 5.937500 6.000000 5.968750 0.005236 8 5.968750 6.000000 5.984375 0.002611 9 5.984375 6.000000 5.992188 0.001304 10 5.992188 6.000000 5.996094 0.000651-------------------------------------------------------------- la raiz es: 5.996093750--------------------------------------------------------------

Ejercicio 20

%METODO DEL PUNTO FIJO disp('=====================================================')disp('---------------------------* MÉTODO DEL PUNTO FIJO *-----------------------')disp('=====================================================')

clc;fprintf('\n'); nombre_f=input(' ingrese funcion asociada \n');nombre_f=input(' al punto fijo g(x)=','s');x0=input(' ingrese valor inicial : ');fprintf('\n'); fprintf(' it aprox g(x) error\n');i=1;e=1; while e>=3E-6 & i<=18 x=x0; r=eval(nombre_f); e=abs((r-x0)/r); fprintf(' %3.0f %10.6f %10.6f %10.6f\n',i,x0,r,e); x0=r; i=i+1; end disp('------------------------------------------------------------') fprintf('la raiz es: %10.9f\n', x0); disp('------------------------------------------------------------')

SALIDA:

Page 23: Prg Calculo

=====================================================

---------------------------* MÉTODO DEL PUNTO FIJO *-----------------------============================================

========= ingrese funcion asociada: sqrt(x)+2*x al punto fijo g(x)=1 ingrese valor inicial : 15

it aprox g(x) error 1 15.000000 1.000000 14.000000 2 1.000000 1.000000 0.000000-------------------------------------------------- la raiz es: 1.000000000--------------------------------------------------

Ejercicio 21%determinar las raices de una ecuacion de segundo grado del tipo%Ax^2+Bx+C=0 considerar la sigiente formula x=-B+-sqrt(B^2-4AC)/2A;%NOTA:considerar el caso en el que las raices son imaginarias.disp('==========================================================')disp('---------------------------* DETERMINACION DE RAICES *----------------------------')disp('---------------* PARA UNA ECUACION DE SEGUNDO GRADO *-----------------')disp('==========================================================')

clc;A=input('ingrese el coeficiente cuadratico:');B=input('ingrese el coeficiente del termino lineal:');C=input('ingrese el termino independiente:');if A==0 & B==0 'ecuacion sin raices'end if A==0raiz1=(-1)*C/Briaz2=raiz1;end disc=B^2-4*A*C; if disc<0 'raices imaginarias'disc=disc*(-1);raiz1=(-B-sqrt(disc))/2*A;raiz2=(-B+sqrt(disc))/2*A;elseraiz1=(-B-sqrt(disc))/2*A;raiz2=(-B+sqrt(disc))/2*A;enddisp('------------------------------------------------------------')disp(‘las raices son:’)disp([raiz1 raiz2])disp('------------------------------------------------------------')

Page 24: Prg Calculo

SALIDA:=================================================

=========---------------------------* DETERMINACION DE RAICES *-------------------------------------------* PARA UNA ECUACION DE SEGUNDO GRADO *-----------------

==========================================================

ingrese el coeficiente cuadratico:2 ingrese el coeficiente del termino lineal:14ingrese el termino independiente:4------------------------------------------------------------Las rices son: -26.8062 -1.1938------------------------------------------------------------