X,Y

Anuncio
GUIÓN DE LA CLASE
INTRODUCCION A LA PROGRAMACION EN MATLAB
Discretizacion 1D y 2D
y = linspace(a,b,n)
meshgrid Ejm. [X,Y] = meshgrid(x,y)
Notación compacta de matrices en matlab
Plot(x,y), subplot, hold on
subplot
surf
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X,Y,Z)
contour
v = -2:0.2:2;
[x,y] = meshgrid(v);
z = x .* exp(-x.^2 - y.^2);
[px,py] = gradient(z,.2,.2);
contour(v,v,z), hold on, quiver(v,v,px,py), hold off
surf
v = -2:0.2:2;
[x,y] = meshgrid(v);
z = x .* exp(-x.^2 - y.^2);
[px,py] = gradient(z,.2,.2);
surf(v,v,z), hold on, quiver(v,v,px,py), hold off
quiver
C = dot(A,B)
A'*B
Cross
C = cross(A,B,dim)
Dlmwrite / dlmread
Load / save
Length / Size
Datenum
Datestr
Today
Now
Datenum = datestr(now)
datestr(now, 'mm/dd/yy')
dt = datestr(now, 'mmmm dd, yyyy HH:MM:SS.FFF AM')
Funciones lógicas
If
For
While, continue, break
while expression ==, <, >, <=, >=, or ~=.
1
statements
end
eps = 1;
while (1+eps) > 1
eps = eps/2;
end
eps = eps*2
Disp
Int2str
Strcat, Strvcat
t1 = 'first'; t2 = 'string'; t3 = 'matrix'; t4 = 'second';
S1 = strvcat(t1, t2, t3)
S2 = strvcat(t4, t2, t3)
Syms, sym
Diff
Diff (f,b,2)
int(x^n,x)
int(sin(2*x),0,pi/2) or int(sin(2*x),x,0,pi/2)
gradient
v = -2:0.2:2;
[x,y] = meshgrid(v);
z = x .* exp(-x.^2 - y.^2);
[px,py] = gradient(z,.2,.2);
contour(v,v,z), hold on, quiver(v,v,px,py), hold off
find
fzero
simple
simplify
pretty
syms a
f = (1/a^3+6/a^2+12/a+8)^(1/3);
simple(simple(f))
solve
syms a b c x
S = a*x^2 + b*x + c;
solve(S)
sumatorio
symsum
syms x k
s1 = symsum(1/k^2,1,inf)
s2 = symsum(x^k,k,0,inf)
Depuración de código con MATLAB
Referencias
Manuales de MATLAB
2
EJEMPLOS MAS COMPLEJOS
INTRODUCCION A LA PROGRAMACION
Meshgrid y funciones de dibujo abanzadas
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
mesh(X,Y,Z)
surf(X,Y,Z,'FaceColor','interp',...
'EdgeColor','none',...
'FaceLighting','phong')
daspect([5 5 1])
axis tight
view(-50,30)
camlight left
Uso de loops y comparación con fzero
1)
a = 0; fa = -Inf;
b = 3; fb = Inf;
while b-a > eps*b
x = (a+b)/2;
fx = x^3-2*x-5;
if sign(fx) == sign(fa)
a = x; fa = fx;
else
b = x; fb = fx;
end
end
x
2)
function y = f(x)
y = x.^3-2*x-5;
z = fzero(@f,2)
Uso de while para lectura de ficheros
1)
fid = fopen('fft.m', 'r');
s = '';
while ~feof(fid)
line = fgetl(fid);
if isempty(line)
break
end
s = strvcat(s, line);
end
disp(s)
2)
fid = fopen('magic.m', 'r');
count = 0;
while ~feof(fid)
3
line = fgetl(fid);
if isempty(line) | strncmp(line, '%', 1)
continue
end
count = count + 1;
end
disp(sprintf('%d lines', count));
4
INTRODUCCION A MATLAB
EJERCICIOS PROPUESTOS
Para la realización de los ejercicios 1 a 3 se hará uso de los datos contenidos
en ficheros ascii: mndata.hdr, mndata.sen que contienen información en
formato ascii relativa a medidas de presión.
EJERCICIO 1
Visualizar contenido de ficheros mndata.sen.
EJERCICIO 2
Lectura de datos de presión y temperatura del fichero mndata.sen. Lectura de
fichero mndata.time con conversión de fecha a tiempo juliano. Escritura en
5
Descargar