Metodos Numericos Parte 1
Metodos Numericos Parte 1
1
>>clock
ans =
1.0e+003 *
2.0140
0.0100
0.0090
0.0140
0.0460
14
46
0.0402
>>fix(clock)
ans =
2014
10
>> 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;
47
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
2.0000 29.3215
3.0000 108.9085
3.0000 79.5870
4.0000 263.8938
4.0000 234.5723
4.0000 154.9852
5.0000 519.4100
5.0000 490.0885
5.0000 410.5014
5.0000 255.5162
.
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
93
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
1.12
>>for i=1:6; z(i) = x(i) + 7(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
>> x=[x, 5]
x=
>> y=[2; 3]
y=
>> y=[y; 7]
y=
2
3
7
Como anteponer elementos
>> x=[9, x]
x=
>> y=[-1; y]
y=
-1
2
3
7
Como extraer elementos de un vector
>> w= y(3:4)
w=
7
..
Para saber el tamao de un vector
>> x=[9 2 3 5]
x=
>>length(x)
ans =
4
>>size(x)
ans =
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=
>> 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=
>> 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 tamao)
>> t1='digitalis'
t1 =
digitalis
>> t2='nicotina'
t2 =
nicotina
>> t3='basilicum'
t3 =
basilicum
>> t4='lychnis'
t4 =
lychnis
t5 =
chrysantemum
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
>> x=1/inf
x=0
1.4 FUNCIONES MATEMATICAS EN MATLAB
>> x=[1 2 3; 9 8 7]
x=
>>sin(x)
ans =
0.8415
0.9093
0.1411
0.4121
0.9894
0.6570
x=
>>sum(x)
ans = 11
Maximo y minino
>>x
x=
1
2
5
8
>>max(x)
ans =
>>min(x)
ans =
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);
[m,d]=media_ds(x)
Produce
M=4.7000
s=2.3685
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 of
>> x=0:0.05:5;
>> y=sin(x);
>>plot(x,y)
>>holdon
>> z=cos(x);
>>plot(x,z)
>>hold of
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 of;
>> M= [0:0.01:1]';
>> k=1.4;
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 of
>>dth=pi/20;
>> j=1:21;
>>i=1:10;
>> x = log(i);
>> y = log(j);
>>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= difuse(xn,yn,zn, L);
>>surf(x,y,z,r)
>> shading interp
2.27
>>clear,clf,hold of
>>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
2.28
>>clear, clf, hold of
>>axis([-0. 1. -0. 1.])
>>axis('square')
>>axis('of')
>> 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 of