Tutorial Matlab quit - salir clock - da la hora fix(clock) - hora en enteros tic - inicia cronómetro toc - tiempo transcurrido date - fecha Introducción de fórmulas r=2; vol = (4/3)*pi*r^3; vol Separación en varias líneas r=2; vol = (4/3)*pi*... r^3; Operadores aritméricos IF r = 2; if r>0, vol = (4/3)*pi*r^3; end OPERADORES RELACIONALES: > < == ~= >= <= OPERADORES LÓGICOS: & | a = 2; b = 1; c = 4; if((a==2 | b==3)&c<5) g = 5;end g ELSE y IFELSE r = 2; if r>3 b=1; elseif r==3 b=2; else b=0; end DISP disp(pi) CONSTANTES PREDEFINIDAS: pi, eps, i, j, inf, NaN, CICLOS FOR for r=1:5 vol = (4/3)*pi*r^3; disp([r,vol]) end for r=5:-1:1 vol = (4/3)*pi*r^3; disp([r,vol]) end WHILE r=0; while r<5 r=r+1; vol = (4/3)*pi*r^3; disp([r,vol]) end ANIDAMIENTO for r=1:5 for s=1:r vol = (4/3)*pi*(r^3-s^3); disp([r,vol]) end end FORMATO format long format short BREAK for i=1:6 for j=1:20 if j>2*i, break, end disp([j,i,2*i]) end end LECTURA Y ESCRITURA z = input('Tecle el radio: ') LECTURA DE CADENA z = input('Tecle el nombre: ','s') PRINTF fprintf('El volumen de la esfera es %12.5f.\n',vol) fprintf('formato_e: %12.5e.\n',12345) fprintf('formato_f: %12.2f.\n',12345) ARREGLOS x = [0, 0.1, 0.2, 0.3, 0.4, 0.5]; x(3) for i=1:6 x(i)=(i-1)*0.1; end AGREGAR AL FINAL x(7) = 0.6; y = 1:1:10; z = 30:-2:0; z' c(8) = 3; c OPERACIONES ARITMÉTICAS CON VECTORES + - .* ./ .^ x = 1:10 y = 1:2:20 y+x y-x x.*y x.^2 ACTIVIDAD Escriba un ciclo que calcule el alcance de un proyectil para ángulos entre 0 a 90 grados en pasos 5 grados y una velocidad inicial de 20 m/s. R = v^2*sin(2*theta)/g Modifique el ciclo anterior para almacenar los valores en un vector R. CRECER UN ARREGLO x = [2 3] x = [x 5] VECTOR COLUMNA y = [2; 3] y = [y; 7] x = [9, x] y = [-1; y] EXTRACCION DE UN ARREGLO w = y(3:4) LONGITUD DE U_N_ ARREGLO length(x) length(y) size(x) size(y) CADENAS v = 'glaciar' v = v' ARREGLOS BIDIMENSIONALES m = [0.1 0.2 0.3; 0.4 0.5 0.6; 0.7 0.8 0.9] m(2,2) = 10 COPIA DE FILAS o COLUMNAS c(1,:) = m(3,:); c(2,:) = m(2,:); c(3,:) = m(1,:); c m+c m-c m.*c m./c c.^2 COMPARACION DE ARREGLOS if m==c, disp('iguales'); else disp('diferentes'); end if m~=c, disp('diferentes'); else disp('iguales'); end a = 'equidna' b = 'tapir' c = 'albatross' d = 'petrel' if a~=b, disp('diferentes'); else disp('iguales'); end a = 'equidna ' b = 'tapir ' c = 'albatross' d = 'petrel ' if a~=b, disp('diferentes'); else disp('iguales'); end s = str2mat(a, b, c, d) SOLUCION DE CUADRATICA % obtener x_min x = 1; while x>0, x=x/2, end % obtener x_max x = 1; while x0, x=x/2; ex = x*0.98+1; ex=ex-1; if ex>0,ex, end end FUNCIONES MATEMATICAS Revisar funciones matemáticas sort([2 1 5]) sort([9 1 5; 2 8 4]) sum([2 1 5]) sum([9 1 5; 2 8 4]) max([2 1 5]) max([9 1 5; 2 8 4]) min([2 1 5]) min([9 1 5; 2 8 4]) k = 5; rand('seed',k) rand(1) rand(5) rand ARCHIVOS M comentarios % EJEMPLO Archivo M para escoger una carta de un lote de 13 cartas. c = clock; k = c(2)*c(3)*c(4)*c(5)*c(6); rand('seed',k) for k=1:20 n = ceil(13*rand(1)); fprintf('numero de carta sacada: %3.0f\n',n) disp(' ') disp('Teclee r y pulse return para repetir') r = input('o cualquier otra tecla para terminar ','s'); if r~='r', break, end end list1_19.m FUNCIONES DE USUARIO function y = demof_(x) y = (2*x.^3+7*x.^2+3*x-1)./(x.^2-3*x+5*exp(-x)); GUARDAR COMO demof_.m demof_(3) demof_([2 3; 5 1]) REGRESAR$ MAS DE UN VALOR function [media, dvstd] = media_ds(x) n = length(x); media = prom(x)/n; dvstd = sqrt(sum(x.^2)/n-media.^2); x = [1 5 3 4 6 5 8 9 2 4]; [m, d] = media_sd(x) FUNCIONES QUE USAN OTRS FUNCIONES function mp = f_av(nombre_f,a,b,c) mp = (feval(nombre_f,a)+2*feval(nombre_f,b)... +feval(nombre_f,c))/4; x = f_av('sin',0.5,0.6,0.7) GRAFICAS ejemplo 1 x = 0:0.05:10; y = sin(x).*exp(-0.4*x); plot(x,y) xlabel('x');ylabel('y') ejemplo 2 p = 0:0.05:8*pi; z = (cos(p)+i*sin(2*p).*exp(-0.05*p)+0.01*p); plot(real(z),imag(z)) xlabel('Re(z)');ylabel('Im(z)') ejemplo 3 x = (0:0.4:10)'; y = sin(x).*exp(-0.4*x); plot(x,y,'+g') xlabel('x');ylabel('y') GRAFICAS CON FPLOT fplot('sin',[0,2*pi]) clf - borrar gráficos axis('square') axis('off') x = (0:0.2:10)'; y = sin(x).*exp(-0.4*x); plot(x,y) grid on xlabel('x');ylabel('y') GRÁFICAS POLARES t = 0:0.05:pi+0.01; y = sin(3*t).*exp(0.03*t); polar(t,y) title('Gráfica polar') grid GRAFICA LOGARITMICA t = 0.1:0.1:3; x = exp(t); y = exp(t.*sinh(t)); loglog(x,y) grid xlabel('x');ylabel('y') t = 0.1:0.1:3; semilogy(t,exp(t.*t)) grid xlabel('t');ylabel('exp(t.*t)') t = 0.1:0.1:3; semilogx(t,exp(t.*t)) grid xlabel('t');ylabel('exp(t.*t)') GRÁFICAS DE MÚLTIPLES CURVAS x = 0:0.05:5; y = sin(x); z = cos(x); plot(x,y,x,z) plot(x,y,'--',x,z,'*') plot(x,y,':',x,z,'*g') plot(x,y,'r',x,z,'y') OTRA FORMA DE GRAFICOS MULTIPLES x = 0:0.05:5; y(1,:) = sin(x); y(2,:) = cos(x); plot(x,y) x = (0:0.05:5)'; y(:,1) = sin(x); y(:,2) = cos(x); plot(x,y) RETENCION x = 0:0.05:5; y = sin(x); plot(x,y) hold on z = cos(x); plot(x,z,'--') xlabel('x');ylabel('y(-), z (--)') HAY QUE DESHABILITAR HOLD ON clear;clf;hold off; x = 0:0.05:5; y = sin(x); plot(x,y) hold on z = cos(x); plot(x,z,'--') xlabel('x');ylabel('y(-), z (--)') hold off TITULOS M = [0:0.01:1]';k = 1.4; p0_entre_p = (1+(k-1)/2+M.^2).^(k/(k-1)); plot(M,p0_entre_p) xlabel('M, numero de Mach') ylabel('p0/p') title('Relación de presión, p(estancamiento)/p(estática)')