metodos numericos parte 1
TRANSCRIPT
1.1
>>clock
ans =
1.0e+003 *
2.0140 0.0100 0.0090 0.0140 0.0460 0.0402
>>fix(clock)
ans =
2014 10 9 14 46 47
>> date
ans =
17-Oct-2014
1.1.2
1.1b
>> r=2;
>>vol=(4/3)*pi*r^3
vol = 33.5103
…………………………………………………………..
1.3
>> r=2;
>>if r>0, vol=(4/3)*pi*r^3;
end
>>vol
vol = 33.5103
………………………………………….
1.4
>> r=2;
>>if r==3, vol=(4/3)*pi*r^3;
end
>>vol
vol = 33.5103
……………………………………………..
1.5
>> r=2;
>>if r~=3, vol=(4/3)*pi*r^3;
end
>>vol
vol = 33.5103
………………………………………………….
1.7
>>for r=1:5
vol=(4/3)*pi*r^3;
disp([r,vol])
end
1.0000 4.1888
2.0000 33.5103
3.0000 113.0973
4.0000 268.0826
5.0000 523.5988
…………………………………………………………
1.8
>> r=0;
>>while r<5
r=r+1;
vol=(4/3)*pi*r^3;
disp([r,vol])
end
1.0000 4.1888
2.0000 33.5103
3.0000 113.0973
4.0000 268.0826
5.0000 523.5988
………………………………………………………………….
El índice del ciclo puede decrementarseasi:
>>for r=5:-1:1
vol=(4/3)*pi*r^3;
disp([r,vol])
end
5.0000 523.5988
4.0000 268.0826
3.0000 113.0973
2.0000 33.5103
1.0 4.188
Podemos escribir ciclos dobles y triples; por ejemplo,
……………………………………………………….
1.9
>>for r=1:5
for s=1:r
vol=(4/3)*pi*(r^3-s^3);
disp([r,vol])
end
end
1 0
2.0000 29.3215
2 0
3.0000 108.9085
3.0000 79.5870
3 0
4.0000 263.8938
4.0000 234.5723
4.0000 154.9852
4 0
5.0000 519.4100
5.0000 490.0885
5.0000 410.5014
5.0000 255.5162
5 0
…………………………………………………………….
FORMATO
>>formatlong
>>pi
ans =
3.141592653589793
>>format short
>>pi
ans =
3.1416
Listado 1.10
>>for i=1:6
for j=1:20
if j>2*i,break,end
end
end
………………………………………………………..
1.11
>>while r<10
r=input('Teclee el radio (o -1 para terminar):');
if r<0; break, end
vol=(4/3)*pi*r^3;
fprintf('Volumen = %7.3f\n',vol)
end
Teclee el radio (o -1 para terminar):5
Volumen = 523.599
Teclee el radio (o -1 para terminar):3
Volumen = 113.097
Teclee el radio (o -1 para terminar):7
Volumen = 1436.755
Teclee el radio (o -1 para terminar):90
Volumen = 3053628.059
>> 1233456646758768897
ans =
1.2335e+018
Borrar variables
clear x y z
Borra ventana de comandos
Clc
>>fprintf ('formato_f: %12.0f\n', 93)
formato_f: 93
>>fprintf('archivo_x', 'Volumen= %12.5f\n', vol)
archivo_x>>
…………………………………………………………..
1.2 VARIABLES DE ARREGLO
>> x=[0, 0.1, 0.2, 0.3, 0.4, 0.5];
>>x(3)
ans =
0.2000
>>for i=1:6
x(i)=(i-1)*0.1;
end
>>x(6)
ans = 0.5000
>> x=2:-0.4:-2
x =
Columns 1 through 7
2.0000 1.6000 1.2000 0.8000 0.4000 0 -0.4000
Columns 8 through 11
-0.8000 -1.2000 -1.6000 -2.0000
……………………………………………………………………………
1.12
>>for i=1:6; z(i) = x(i) + 7(i); end
>>for i=1:6; z(i) = x(i) - 7(i); end
>>for i=1:6; z(i) = x(i) *y 7(i); end
>>for i=1:6; z(i) = x(i) +/y7(i); end
>> g=z.^1.2;
>>z
z =
0
0.1000
0.2000
0.3000
0.4000
0.5000
>>g
g =
0
0.0631
0.1450
0.2358
0.3330
0.4353
>>for i=1:6; g(i) = z(i)^1.2; end
Comando para anexar
x =
2 3
>> x=[x, 5]
x =
2 3 5
>> y=[2; 3]
y =
2
3
>> y=[y; 7]
y =
2
3
7
Como anteponer elementos
>> x=[9, x]
x =
9 2 3 5
>> y=[-1; y]
y =
-1
2
3
7
Como extraer elementos de un vector
>> w= y(3:4)
w =
3
7
…………………………………………………..
Para saber el tamaño de un vector
>> x=[9 2 3 5]
x =
9 2 3 5
>>length(x)
ans =
4
>>size(x)
ans =
1 4
Variables de cadena
>> v='glaciar'
v =
glaciar
>> v=v'
v =
g
l
a
c
i
a
r
………………………………………………………….
Variables de arreglo bidimensional: (equivale a una matriz)
>> m=[0.1, 0.2, 0.3; 0.4, 0.5, 0.6; 0.7, 0.8, 0.9;];
>>m
m =
0.1000 0.2000 0.3000
0.4000 0.5000 0.6000
0.7000 0.8000 0.9000
Operaciones bidimensionales:
m =
0.1000 0.2000 0.3000
0.4000 0.5000 0.6000
0.7000 0.8000 0.9000
>> b=[9 8 7; 6 5 4; 3 2 1;]
b =
9 8 7
6 5 4
3 2 1
>> c=m+b;
>>c
c =
9.1000 8.2000 7.3000
6.4000 5.5000 4.6000
3.7000 2.8000 1.9000
>> c=m-b
c =
-8.9000 -7.8000 -6.7000
-5.6000 -4.5000 -3.4000
-2.3000 -1.2000 -0.1000
>> c=m.*b
c =
0.9000 1.6000 2.1000
2.4000 2.5000 2.4000
2.1000 1.6000 0.9000
>> c=m./b
c =
0.0111 0.0250 0.0429
0.0667 0.1000 0.1500
0.2333 0.4000 0.9000
……………………………………………
1.14:
C=a+b
C=a-b
C=a.*b
C=a./b
(a y b son del mismo tamaño)
>> t1='digitalis'
t1 =
digitalis
>> t2='nicotina'
t2 =
nicotina
>> t3='basilicum'
t3 =
basilicum
>> t4='lychnis'
t4 =
lychnis
>> t5= 'chrysantemum'
t5 =
chrysantemum
>> s= str2mat(t1, t2, t3, t4, t5)
s =
digitalis
nicotina
basilicum
lychnis
chrysantemum
Listado1.15
>> % Pra obtener x_min
>> x=1; while x>0, x=x/2, end
x = 0
Listado 1.16
>> % Para obtener x_max
>> x=1; while x<inf, x=x*2, end
x = Inf
………………………………………………………………..
1.17
>> % Para obtener el épsilon de la maquina
>> x=1; while x>0, x=x/2; ex=x*0.98+1; ex=ex -1;
if ex>0, ex, end
end
ex = 2.2204e-016
>> x=1/inf
x = 0
1.4 FUNCIONES MATEMATICAS EN MATLAB
>> x=[1 2 3; 9 8 7]
x =
1 2 3
9 8 7
>>sin(x)
ans =
0.8415 0.9093 0.1411
0.4121 0.9894 0.6570
………………………………………………………………………
1.5 FUNCIONES QUE REALIZAN TAREAS
Sumatoria:
>>x
x =
9 1 5
2 8 4
>>sum(x)
ans = 11 9 9
Maximo y minino
>>x
x =
9 1 5
2 8 4
>>max(x)
ans =
9 8 5
>>min(x)
ans =
2 1 4
CREACION DE IUN PROGRAMA EN FORMA DE ARCHIVO M
1.19
>> 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’)
Teclee r y pulse return para repetir
>> r=input(' o cualquier otra letra para terminar ','s');
o cualquier otra letra para terminar
>>if r~= 'r' break, end
>>end
>>end
1.7 COMO ESCRIBIR FUNCIONES DE USUARIO PROPIAS
function y=demof_(x)y=(2*x.^3+7*x.^2+3*x-1)./(x.^2-3*x+5*exp(-x));
produce y= 502.1384
……………………………………………………………..
1.21
function [media, dvstd] = media_ds(x)n=length(x);media = sum(x)/n;dvstd = sqrt(sum(x.^2)/n - media.^2);
con x=[1 5 3 4 6 5 8 9 2 4];
[m,d]=media_ds(x)
Produce
M=4.7000 s=2.3685
1.8 COMO GUARDAR Y CARGAR DATOS
Guardar y cargar : se utiliza save.
Guarda todas las variables en el archivo llamado nombre_archivo.mat. Cuando quiera recuperar las variables; escriba
Load nombre_archivo
Si solo desea guardar ciertas variables, escriba sus nombres después de nombre_archivo; por ejemplo, Savenombre archivo a b c
CAPITULO 2
GRAFICAS CON MATLAB
2.1Graficacion simple
Listado 2.1
>> x=0:0.05:10;
>> y=sin(x).*exp(-0.4*x);
>>plot(x,y)
>>xlabel('x'); ylabel('y')
2.2
>> x=(0:0.05:10)';
>> y=sin(x).*exp(-0.4*x);
>>plot(x,y)
>>xlabel('x'); ylabel('y')
2.3
>> 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)')
2.4
>> x=(0:0.4:10)';
>> y=sin(x).*exp(-0.4*x);
>>plot(x,y,'+')
>>xlabel('x'); ylabel('y')
>> x=(0:0.4:10)';
y=sin(x).*exp(-0.4*x);
plot(x,y,'+')
xlabel('x'); ylabel('y')
>>axis([-2, 6, -0.7, 0.7])
2.5
>> x=(0:0.2:10)';
>> y=sin(x).*exp(-0.4*x);
>>plot(x,y)
>>gridon
>>xlabel('x'), ylabel('y')
2.6
>> t=0:.05:pi+0.1;
>> y=sin(3*t).*exp(-0.3*t);
>>polar(t,y)
>>title('Grafica polar')
>>grid
2.7
>> t=.1:.1:3;
>> x=exp(t);
>> y=exp(t.*sinh(t));
>>loglog(x,y)
>>grid
>>xlabel('x');ylabel('y')
2.8
>> t=.1:.1:3;
>>semilogy(t,exp(t.*t))
>>grid
>>xlabel('t'); ylabel('exp(t.*t');
2.9
>> t=.1:.1:3;
>>semilogx(t,exp(t.*t))
>>grid
>>xlabel('t'); ylabel('exp(t.*t');
2.10
>> x=0:0.05:5;
>> y=sin(x);
>> z=cos(x);
>>plot(x,y,x,z)
>>plot(x,y,x,z)
>> x=0:0.05:5;
>> y=sin(x);
>> z=cos(x);
>>plot(x,y,'--', x,z,'*')
>>plot(x,y,x,z)
>> x=0:0.05:5;
>> y=sin(x);
>> z=cos(x);
>>plot(x,y,':', x,z,'*g')
>>plot(x,y,x,z)
>> x=0:0.05:5;
>> y=sin(x);
>> z=cos(x);
>>plot(x,y,'r', x,z,'y')
2.11
>> x=0:0.05:5;
>>y(1,:)=sin(x);
>>y(2,:)=cos(x);
>>plot(x,y)
2.12
>> x= (0:0.05:5)';
>>y(:,1)=sin(x);
>>y(:,2)=cos(x);
>>plot(x,y)
2.13
>> x= (0:0.05:5)';
>> y=sin(x);
>>plot(x,y)
>>holdon
>> z=cos(x);
>>plot(x,z,'--')
>>xlabel('x'); ylabel('y(-), z(--)');
2.14
>>clear; clf; hold off
>> x=0:0.05:5;
>> y=sin(x);
>>plot(x,y)
>>holdon
>> z=cos(x);
>>plot(x,z)
>>hold off
2.15A
>> M= [0:0.01:1]'; k=14;
>> 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('Relaciondepresion, p(estancamiento)/p(estatica)')
2.15B
>> % Relacion de presion vs. numero de Mach
>>clear; clf; hold off;
>> M= [0:0.01:1]';
>> k=1.4;
>> p0_entre_p = (1 + ((k-1)/2+M.^2).^(k/k-1));
>>hold on
>>axis('square'); % hace que la grafica sea cuadrada
>>plot(M,p0_entre_p)
>>xlabel('M, Numero de Mach')
>>ylabel('p0/p')
>>title('Relacion de presion, p(estancamiento)/p(estatica)')
>>text(0.45, 1.55, 'Compresible')
>> Mb=[0:0.01:0.7]';
>> p0_entre_pb= 1 + k/2*Mb.^2;
>>plot(Mb,p0_entre_pb, '--')
>>text(0.5, 1.1,'Incompresible')
2.16
>>clear; clf
>> t= 0:.3:30;
>>subplot(2,2,1), plot(t,sin(t)), title('SUBGRAFICA 2,2,1')
>>xlabel('t'); ylabel('sin(t)')
>>subplot(2,2,2), plot(t,t.*sin(t)), title('SUBGRAFICA 2,2,2')
>>xlabel('t'); ylabel('t.*sin(t)')
>>subplot(2,2,3), plot(t,t.*sin(t).^2), title('SUBGRAFICA 2,2,3')
>>xlabel('t'); ylabel('t.*sin(t).^2')
>>subplot(2,2,4), plot(t,t.^2.*sin(t).^2), title('SUBGRAFICA 2,2,4')
>>xlabel('t'); ylabel('t.^2.*sin(t).^2')
2.17
>>clear,clf
>> t=0:0.1:20;
>> r= exp(-0.2*t);
>>th=pi*t*0.5;
>> z=t;
>> x=r.*cos(th);
>> y=r.*sin(th);
>> plot3(x,y,z)
>>plot3([1,1], [-0,5,0], [0,0])
>>text(1,-0.7,0, 'A')
>> n=length(x);
>>text(x(n), y(n), z(n)+2, 'B')
>>xlabel('x'); ylabel('y'); zlabel('z');
2.18
>>clear, clf
>>xa=-2:.2:2;
>>ya=-2:.2:2;
>> [x,y]=meshgrid(xa,ya);
>> z=x.*exp(-x.^2-y.^2);
>>mesh(x,y,z)
>>title('Esta es una grafica 3-D de z=x*exp(-x^2 - y^2)')
>>xlabel('x'); ylabel('y'); zlabel('z');
2.19
>>xm= -2:.2:2; ym= -2:.2:2;
>> [x,y]= meshgrid(xm,ym);
>> z = x.*exp(-x.^2 - y.^2);
>>zmax=max(max(z)); zmin=min(min(z));
>>dz=(zmax-zmin)/10;
>>nivel = zmin + 0.5*dz:dz:zmax;
>> h=contour(x,y,z,nivel); clabel(h,'manual')
title('Grafica de contorno hecha con contour (x,y,z,nivel)')
xlabel('x'); ylabel('y')
2.20
>>clear,clf
>>xm=-3:0.2:3; ym=-2:0.2:1;
>> [x,y]=meshgrid (xm,ym)
>> f= y.^3 + exp(y) - tanh(x);
>>contour(x,y,f, [0,0])
>>xlabel('x'); ylabel('y')
2.24
>>clear, clf
>>for i =1:4 % corresponde a la direccion x
for j=1:7 % corresponde a la direccion y
z(j,i)=sqrt(i^2 + j^2);
end
end
>>mesh(z)
>>xlabel('i')
>>ylabel('j')
>>zlabel('z')
2.25
>>clear, clf
>>yp=1:5;
>>xp=1:4;
>> [x,y]=meshgrid(xp,yp);
>> z=sqrt(x.^2 + y.^2);
>> %
>>subplot(221)
>>mesh(x,y,z)
>>axis([0,5,0,5,0,10])
>>title('perspectiva por omision')
>>xlabel('X')
>>ylabel('Y')
>>zlabel('Z')
>> %
>>subplot(222)
>>mesh(x,y,z)
>>axis([0,5,0,5,0,10])
>>title('[35, 20]')
>>view([35, 20])
>>xlabel('X')
>>ylabel('Y')
>>zlabel('Z')
>> %
>>subplot(223)
>>mesh(x,y,z)
>>axis([0,5,0,5,0,10])
>>title('[35, -20]')
>>view([35, -20])
>>xlabel('X')
>>ylabel('Y')
>>zlabel('Z')
>> %
>>subplot(224)
>>mesh(x,y,z)
>>axis([0,5,0,5,0,10])
>>title('[10,90]')
>>xlabel('X')
>>ylabel('Y')
>>zlabel('Z')
>>view([10,90])
>>axis('square')
2.26
>>clear, clf, hold off
>>dth=pi/20;
>> j=1:21;
>>i=1:10;
>> x = log(i);
>> y = log(j);
>> [x,y] = meshgrid(x,y);
>> z=sqrt(0.1*((x-log(5)).^2 + (y-log(5)).^2))+1;
>>meshc(x,y,z)
>>clear,clf
>>axis([-1.5, 1.5, -1.5, 1.5, -1.3, 1.3])
>>view([1 -0.5 0.31])
>>caxis([-0.8 1.5])
>>colormap hot
>> hold on
>> L=[0.5,0.3,0.7]; V=[1,1,1];
>> [x,y,z] = sphere(20);
>> [xn,yn,zn,] = surfnorm(x,y,z);
>> % r= specular(xn,yn,zn, L, V);
>> r= diffuse(xn,yn,zn, L);
>>surf(x,y,z,r)
>> shading interp
2.27
>>clear,clf,hold off
>>dth=pi/20;
>>for j=1:21
fori=1:10
r=0.5+0.2*i + j*0.01*i;
th= dth*(j-i);
x(i,j) = r*cos(th);
y(i,j) = r*sin(th);
z=cos(0.1*(x.^2 + y.^2))+1;
end
end
>>surf(x,y,z) % graficacion de una superficie
>>xlabel('X')
>>ylabel('Y')
>>zlabel('Z')
>>axis([-5, 4, -1, 5])
>>view([-135,40])
>>holdon
>>mesh(x,y,zeros(size(x)) % graficacion de una reticula en % el plano x-y
>>colormaphot
>>caxis([-0.5,3])
>>hold off
>> % shading flat % para la segunda grafica de la Fig. 2.27
2.28
>>clear, clf, hold off
>>axis([-0. 1. -0. 1.])
>>axis('square')
>>axis('off')
>> hold on
>> plot([0,1,1,0,0], [0,0,1,1,0])
>> h=pi/10;
>> t=0:h:pi*2;
>>xx = cos(t);
>>yy = sin(t);
>>for n=1:40
r= rand(1)*0.1;
xc = rand(1);
yc = rand(1);
x = xx*r + xc;
y = yy*r + yc;
plot(x,y)
end
>>hold off