Aula_Octave
Aula_Octave
% Comentario
a = 10
b = 100
c = 1000
d = a*b*c
e = a^2 + 5*b
f = e/10;
whos
g = f/1234
whos
% Matriz 3x3
1
A = [1 2 3; 4 5 6; 7 8 9]
% Matriz identidade
B = [1 0 0; 0 1 0; 0 0 1]
% ou entao
B = eye(3)
C = A + B
D = A
E = A*B
% Vetor q transposto
q'
% Vetor r transposto
r'
2
% F = matriz A transposta
F = A'
% Acesso a
I = A(1,1)
J = A(2,2)
K = A(3,3)
L = A(1,1:3)
M = A(1:3,1)
N = A(end,end)
O = A(2,1:end)
% Criar o vetor x = [1 2 3 4 5]
% de maneira mais simples
x = 1:5
3
y = sin(x)
% Criar vetor x = [6 5 4 3 2 1 0]
x = 6:-1:
0
% Gerar graficos
x = 0:0.01:4*pi;
y = sin(x);
plot(x,y)
4
% Exemplo mais complexo com 'for's aninhados
clear
for i=1:8
for j=1:8
A(i,j)=i+j;
B(i,j)=i-j;
end
end
A
B
C=A+B
5
end
end
end
A
6
Exemplos avançados:
function Avancado(entrada)
switch entrada
case 0
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
figure;
Z = exp(-X.^2 - Y.^2);
close all;
mesh(X,Y,Z)
figure;
contour(Z,10)
case 1
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
figure;
Z = X.* exp(-X.^2 - Y.^2);
close all;
mesh(X,Y,Z)
figure;
contour(Z,10)
case 2
figure;
x = 0:0.1:5;
fa = x.^2 -3.*x + 2;
fb = 2*x.^2 +x - 3;
f = [fa; fb];
close all;
plot(x,f)
title('Multiplas funcões com mesma abscissa')
xlabel('x')
grid on
legend('f1','f2')
figure;
x = 0:0.1:5;
f1 = x'.^2 -3.*x' +2;
f2 = 2*x'.^2 +x' -3;
plotyy(x,f1,x,f2)
7
title('Abscissas distintas para diferentes funcoes')
xlabel('x')
grid
case 3
figure;
dados1 = 2*rand(1,500) + 2; % rand --> [0 1], uniforme
dados2 = randn(1,500)+3; % randn --> [-1 1],
gaussiano ou normal
close all;
subplot(2,1,1),plot(dados1)
axis([0 500 0 6])
title('Numeros Aleatorios - dados1')
subplot(2,1,2),plot(dados2)
title('Numeros Aleatorios - dados2')
xlabel('Indice')
% Estatísticas
format short
min1 = min(dados1)
max1 = max(dados1)
media1 = mean(dados1)
min2 = min(dados2)
max2 = max(dados2)
media2 = mean(dados2)
figure;
bins1 = 10; % 10 intervalos
subplot(2,1,1)
hist(dados1,bins1)
xlabel('intervalos')
ylabel('contagens')
title('Histograma - dados1')
bins2 = [0 0.5 1 1.5 2 2.5 ...
3 3.5 4 4.5 5 5.5 6]; % Especificação
de intervalos
subplot(2,1,2)
hist(dados2,bins2)
xlabel('intervalos')
ylabel('contagens')
title('Histograma - dados2')
case 4
t=[0:256]'/256;
8
w0=2*pi; x=square(w0*t);
disp('Série de Fourier de uma onda quadrada!')
disp(' ')
cont=1;
close all;
while cont==1
nin=input('Quantos harmônicos deseja utilizar
na aproximação? ');
if nin ~= 0
xhat=zeros(size(x));
for k=1:2:nin
Xhat = xhat +
sin(k*w0*t)*4/(k*pi);
end
figure;
plot(t,x,t,xhat)
xlabel('Tempo(s)')
title(['Sinal composto de ',
num2str(nin), ' harmonicos.']);
else
cont=0;
end
end
case 5
y = [0 2 5 4 1 0];
plot(y)
t = 0:.4:4*pi;
y = sin(t);
z = cos(t);
close all;
plot(t, y, t, z)
title('Funcoes')
xlabel('t')
ylabel('Seno e Cosseno')
text(2.8, 0.4, 'Seno')
gtext('Cosseno')
case 6
figure;
x = 0:0.5:50; y = 5*x.^2;
close all;
9
subplot(2,2,1),plot(x,y)
title('Polinomio - linear/linear (plot)'),
ylabel('y'), grid
subplot(2,2,2),semilogx(x,y)
title('Polinomio - log/linear (semilogx)'),
ylabel('y'), grid
subplot(2,2,3),semilogy(x,y)
title('Polinomio - linear/log (semilogy)'),
xlabel('x')
ylabel('y'), grid
subplot(2,2,4),loglog(x,y)
title('Polinomio - log/log (loglog)'), xlabel('x')
ylabel('y'), grid
case 7
close all;
figure; % Forma Retangular
z1 = 1.5 + 0.5j;
z2 = 0.5 + 1.5j;
z3 = z1 + z2;
z4 = z1 * z2;
axis([-3 3 -3 3]);
axis square;
grid on;
hold on;
plot(z1,'b.');
plot(z2,'go');
plot(z3,'rx');
plot(z4,'m*');
text(real(z1)+ 0.1,imag(z1),'z1');
text(real(z2)+ 0.1,imag(z2),'z2');
text(real(z3)+ 0.1,imag(z3),'z1+z2');
text(real(z4)+ 0.1,imag(z4),'z1*z2');
xlabel('Parte Real')
ylabel('Parte Imaginária')
title('Plano Complexo - Forma Retangular')
10
hold on;
plot(z1,'b.');
plot(z2,'go');
plot(z3,'rx');
compass(z1);
compass(z2);
compass(z3);
text(real(z1)+0.1,imag(z1),'z1');
text(real(z2)+0.1,imag(z2),'z2');
text(real(z3)+0.1,imag(z3)+0.1,'z1+z2');
xlabel('Parte Real')
ylabel('Parte Imaginária')
title('Plano Complexo - Forma Vetorial')
end
11
Processamento de imagens
12
imshow(uint8(c))
% Limpar tudo
clear; close all; clc;
13
d = imresize(c,2,'nearest');
% Obter a versao reduzida (de tamanho 128x128) de 'b'
e = imresize(b,1/4,'nearest');
% Aumentar a versao 128x128 de volta para o tamanho 512x512
f = imresize(e,4,'nearest');
% Obter a versao reduzida (de tamanho 64x64) de 'b'
g = imresize(b,1/8,'nearest');
% Aumentar a versao 64x64 de volta para o tamanho 512x512
h = imresize(g,8,'nearest');
% Limpar tudo
clear; close all; clc;
14
% Mostrar o histograma da imagem 'mandrill.png'
figure;
imhist(double(b));
% Limpar tudo
close all; clear; clc;
% Limpar tudo
close all; clear; clc;
15
% Adicionar ruido a versao em tons de cinza de 'lena.png'
c = imnoise(b,'speckle');
% Filtrar a imagem com ruido utilizando o filtro de mediana 3x3
% (http://en.wikipedia.org/wiki/Median_filter)
d = medfilt2(c,ones(3));
% Mostrar a imagem com ruido e a imagem filtrada
figure;
imshow(uint8(c));
figure;
imshow(uint8(d));
16