Professional Documents
Culture Documents
INTRODUCCIÓN A MATLAB
1. Ejemplos
a=1
a= a=1;b=2
1 b=
2
b=[1 2] A=[a+b pi 3
b= b^2 0 atan(a)
2 5 sin(b) -1]
A=
a=2+i 3.0000 3.1416 3.0000
a= 4.0000 0 0.7854
2.0000 + 1.0000i 5.0000 0.9093 -1.0000
b=-5-3*i B=zeros()
b= B=
-5.0000 - 3.0000i 0
a==1 B=zeros(2,3)
ans = B=
0 0 0 0
0 0 0
v=[1 2 3 4 5]
v= A=[2 2 3;0 0 7;5 9 -1]
1 2 3 4 5 A=
2 2 3
v=1:5 0 0 7
v= 5 9 -1
1 2 3 4 5
B=zeros(size(A))
A=[2 2 3 B=
007 0 0 0
5 9 -1] 0 0 0
A= 0 0 0
2 2 3
0 0 7 C=ones(2,3)
5 9 -1 C=
1 1 1
A=[2 2 3 ; 0 0 7 ; 5 9 -1] 1 1 1
A=
2 2 3 D=diag(1:5)
0 0 7 D=
5 9 -1 1 0 0 0 0
0 2 0 0 0
0 0 3 0 0
0 0 0 4 0
0 0 0 0 5
A*B r=eig(A)
ans = r=
1 0 -1
0 1 -2
d=det(A) [V D]=eig(A)
d= V=
2 0.7071 -0.4472
-0.7071 0.8944
v=[0 -1] D=
v= -1 0
0 -1 0 -2
2. Ejercicios
function[y]=mifuntion(x)
if x<0
y=x^2
else
y=sin(x*(pi/180))
end
Evaluando
y=mifuntion(30)
y=
0.5000
y=
0.5000
Ploteando
Plot(x,y)
1.5
0.5
-0.5
0 5 10 15 20 25 30 35 40 45 50
mifuntion2.m
function[y]=mifuntion2(x)
y=x+x.^2+sin((2*pi)*x)
x=[-2:0.5:3];
y=mifuntion2(x);
Ploteando
plot(x,y)
12
10
-2
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3
figure(1)
plot(x,y,'r')
12
10
-2
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3
figure(2)
plot(x,y,'b:')
12
10
-2
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3
a) Implemente en Matlab la siguiente función, luego plotee
mifuntion4.m
function[y]=mifuntion4(x)
if x==1
y='error';
elseif x==-1
y='error';
else
y= 1./((x.^2)-1)
end
x=[2:6];
y=mifuntion4(x)
y=
plot(x,y)
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
2 2.5 3 3.5 4 4.5 5 5.5 6
b) Implementando en Matlab y plotee
funtion5.m
function[y] =funtion5(x1,x2)
if x1>0
if x2>0
y=x1+x2
else
y=x1.^2+x2.^2
y=sqrt(y)
end
else
y=x1.^2+x2.^2
y=sqrt(y)
end
x1=[0:5];
x2=[0:5];
y=funtion5(x1,x2);
plot(x1,y)
8
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
plot(x2,y,'r')
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
c) Construya una función escalar que ayude a encontrar el mínimo de f(x) = x 3-2x-5,
dentro del intervalo (0,2)
funtion6.m
function[y]=funtion6(x)
y=x.^3-2.*x-5;
y=min(y)
x=[0:2]
x=
0 1 2
y=funtion6(x)
y=
-6
y=
-6
plot(x,y)
min(y)
y=
-6
d) Construya una señal escalón unitario de 0 a 50 segundos, con step inicial en 25s. El
paso deberá ser de 0.5s. Ploteee resultado
funtion7.m
function y = funtion7(t,to)
[m,n] = size(t);
y = zeros(m,n);
i = find(t>=to);
if isempty(i)
return
end
i = i(1);
if m == 1
y(i:n) = ones(1,n-i+1);
else
y(i:m) = ones(m-i+1,1);
end
xlabel('t');ylabel('x(t)');axis([-5 50 0 1.5])
x=[0:0.5:50];
y=funtion7(x,25);
plot(x,y)
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 5 10 15 20 25 30 35 40 45 50
dirac.m
function y = dirac(x1,x2)
stem(x1,x2)
xlabel('x');ylabel('?');title('PEINE DE DIRAC')
y=ones(1,5);
x2=[1:5];
y=dirac(x2,y);
plot(x2,y)
PEINE DE DIRAC
1
0.9
0.8
0.7
0.6
0.5
?
0.4
0.3
0.2
0.1
0
1 1.5 2 2.5 3 3.5 4 4.5 5
x