aratasaraiva@gmail.com
Plotagem
Equaes
Introduo
Plotagem
Equaes
Introduo
?
http://newsreader.mathworks.com
Matlab Screen
Command Window
type commands
Current Directory
View folders and m-files
Workspace
View program variables
Double click on a variable
to see it in the Array Editor
Command History
view past commands
save a whole session
using diary
Variveis
No preciso indicar o tipo de varivel
int a;
double b;
float c;
Array, Matriz
um vector
x =
1
uma matriz
x =
1
5
3
2
1
2
x = [1 2 5 1]
x = [1 2 3; 5 1 4; 3 2 -1]
3
4
-1
transposta
y = x
1
2
5
1
y =
t =1:10
t =
=2:-0.5:-1
k =
B
x =
1
5
1.5
0.5
= [1:4; 5:8]
2
6
3
7
4
8
-0.5
-1
10
x = zeros(1,3)
x =
0
x = ones(1,3)
x =
1
rand(M,N) matriz uniformemente
distribuida de MxN
Nmeros entre (0,1)
x = rand(1,3)
x =
0.9501
0.2311 0.6068
Matrix Index
The matrix indices begin from 1 (not 0 (as in C))
The matrix indices must be positive integer
Given:
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or
logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
Concatenation of Matrices
x = [1 2], y = [4 5], z=[ 0 0]
A = [ x y]
1
B = [x ; y]
1 2
4 5
C = [x y ;z]
Error:
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
Operators (arithmetic)
+ addition
subtraction
multiplication
division
^ power
Matrices Operations
Given A and B:
Addition
Subtraction
Product
Transpose
x = A(1,:)
x=
1 2 3
y = A(3 ,:)
y=
3 4 -1
b = x .* y
c=x./y
d = x .^2
b=
c=
0.33 0.5 -3
d=
3 8 -3
K= x^2
Erorr:
??? Error using ==> mpower Matrix must be square.
B=x*y
Erorr:
??? Error using ==> mtimes Inner matrix dimensions must agree.
Tipos Bsicos
Matriz
Char Array
Estrutura
Estrutura:
Dicas!
ix) Determinante:
det(matriz)
x) Inversa:
inv(matriz)
xi) Dimenses da matriz:
size(matriz)
lenght(matriz)
numel(matriz)
13
10
14
11
15
12
16
x=[1 2 3 4 5 6;
2 1 3 3 2 1];
%Forma linear:
xmin=min(x);
xmin=min(xmin);
[i,j]=find(x==xmin);
%Forma condensada:
[i,j]=find(x==(min(min(x))));
funo
estimativa inicial
Controle de fluxo
if
for
while
break
if:
AND
OR
Falso
Verdadeiro
AND
OR
resultado
resultado
While:
1
0.8
0.6
>>y=sin(x);
0.4
0.2
0
-0.2
-0.4
>>plot(y)
-0.6
-0.8
-1
10
20
30
40
50
60
70
80
90
100
>>y1=exp(-x/3);
>>y2=y*y1;
0.7
0.6
>>plot(y2)
0.5
0.4
0.3
0.2
0.1
0
-0.1
-0.2
-0.3
10
20
30
40
50
60
70
80
90
100
Display Facilities
0.7
0.6
plot(.)
0.5
0.4
0.3
Example:
>>x=linspace(0,4*pi,100);
>>y=sin(x);
>>plot(y)
>>plot(x,y)
0.2
0.1
0
-0.1
-0.2
-0.3
10
20
30
40
50
60
70
80
90
100
10
20
30
40
50
60
70
80
90
100
0.7
stem(.)
0.6
0.5
0.4
0.3
Example:
>>stem(y)
>>stem(x,y)
0.2
0.1
0
-0.1
-0.2
-0.3
Display Facilities
title(.)
>>title(This is the sinus function)
xlabel(.)
0.6
0.4
ylabel(.)
0.2
sin(x)
>>xlabel(x (secs))
0
-0.2
-0.4
-0.6
>>ylabel(sin(x))
-0.8
-1
10
20
30
40
50
60
x (secs)
70
80
90
100
>> figure(1)
>> figure(2)
>> t=0:0.01:10;
>> y=sin(t);
>> plot(t,y)
>> z=cos(t);
>> plot(t,z)
Dica!
>> figure(3)
>> plot(t,y)
>> subplot(1,2,1)
>> subplot(1,2,2)
>> plot(t,z)
>>
>>
>>
>>
>>
>>
>>
t=0:0.25:10;
y=sin(t);
plot(t,y,'r+')
xlabel('tempo')
ylabel('seno')
title('Seno vs. Tempo')
Axis([0 10 -2 2])
>>
>>
>>
>>
>>
t=0:0.01:10;
y=sin(t);
z=cos(t);
plot(t,y,'g-',t,z,'r-')
legend('seno','cosseno')
Ou...
>> t=0:0.01:10;
>> y=sin(t);
>> z=cos(t);
>> plot(t,y,'g-)
>> hold on
>> plot(t,z,'r-')
>> legend('seno','cosseno')
xx=0:0.01:1;
yy=0:0.01:1;
[X,Y]=meshgrid(xx,yy);
Z=exp(-0.5*(X.^2+Y.^2));
colormap jet
figure(1);surf(X,Y,Z); rotate3d on; shading
interp;
Alguns Z so
negativos! No pode!
vv1=(X.*log(X))+(Y.*log(Y))+(Z.*log(Z));
%grfico da superfcie
colormap jet
figure(1);surf(X,Y,vv1); rotate3d on; shading interp;
xlabel('X1');ylabel('X2');zlabel('DeltaGi/RT');
Use of M-File
Click to
create a new
M-File
Extension .m
A text file containing script or function or program
to run
Use of M-File
Notes:
Exemplos
Exemplo
1
dmt
FE F
dt
(2)
dmt
dht
A
dt
dt
(3)
dht 1
FE F
dt
A
(4)
Ainda,
e, portanto,
altura
da
coluna
de
lquido
inversamente
h
F
R
(5)
dht 1
h
FE
dt
A
R
(6)
Logo,
RA
ht RFE 1 e
(7)
Dica!
Exemplo
2
dht 1
h
FE
dt
A
R
(6)
function dh = dhdt(t,h,flag,par)
R = par(1);
A = par(2);
Fe = par(3);
dh = (Fe-(h/R))/A;
Exemplo
3
V dT
d :VT
como
dt
dt
dV
dh
dT
A h
T
dt
dt
h dt
1
dht
FE
dt
A
R
dT T
h
C p A h
FE FE HE FH Q
dt
A
R
como:
O
balano de energia
escrito
VTT UT
FE
dT C1 d F
U
E E Fh H
FH
E
E T
p
dt
dt
h A
C p
C p
A
(9)
(6)
(10)
(8)
(11)
Real
dy(1)
dh/dt
y(1)
dy(2)
dT/dt
y(2)
% Visualizao da simulao
figure(1);
plot(t,y(:,1));
title('Tanque de aquecimento');
xlabel('Tempo (h)');
ylabel('Altura (m)');
figure(2);
plot(t,y(:,2));
title('Tanque de aquecimento');
xlabel('Tempo (h)');
ylabel('Temperatura (K)');
function dy = dydt(t,y,flag,par);
U = par(1);
A = par(2);
Ro = par(3);
Cp = par(4);
Fe = par(5);
R = par(6);
Te = par(7);
Th = par(8);
dy(1) = (Fe-(y(1)/R))/A;
dy(2) = (1/y(1))* ( ((Fe*Te/A)+(U*Th/(Ro*Cp)))...
- ( y(2)*((Fe/A)+(U/(Ro*Cp)))) );
dy = dy(:);
Use o comando:
matriz coluna = matriz linha (:)
Dica!
figure(1);
plot(t,y(:,1));
title('Tanque de aquecimento');
xlabel('Tempo (h)');
ylabel('Altura (m)');
Dica!
Exemplo
4
VP = VF / ( 1 + i )n
Onde i a taxa de juros mensal e n o nmero de meses entre o VF e o VP.
clc
close all
clear all
ivetor=0:0.01:0.50;
VPvetor114=[];
VPvetor98=[];
prompt{1}='Nmero de meses do pagamento da serie A:';
prompt{2}='Nmero de meses do pagamento da serie B:';
prompt{3}='Valor de cada parcela da serie A:';
prompt{4}='Valor de cada parcela da serie B:';
resposta=inputdlg(prompt,'Calculo da taxa de equilibrio');
nummeses114=str2num(char(resposta(1)));
nummeses98=str2num(char(resposta(2)));
v114=str2num(char(resposta(3)));
v98=str2num(char(resposta(4)));
for J = 1:length(ivetor),
i=ivetor(J);
VP=[];
for K = 1:nummeses114,
VP(K)=v114/(1+i)^K;
end
VPfinal=sum(VP);
VPvetor114=[VPvetor114, VPfinal];
VP=[];
for K = 1:nummeses98,
VP(K)=v98/(1+i)^K;
end
VPfinal=sum(VP);
VPvetor98=[VPvetor98, VPfinal];
end
plot(ivetor*100,VPvetor114,'-b')
hold on
plot(ivetor*100,VPvetor98,'-r')
title('Valor presente das parcelas a serem pagas')
legend( [ num2str(nummeses114), ' parc de ', num2str(v114) ,' reais cada'] , ...
[ num2str(nummeses98), ' parc de ', num2str(v98) ,' reais cada' ] )
xlabel('Taxa de juros mensal')
ylabel('Valor presente em Reais')
if (VPvetor114(1)<VPvetor98(1)),
posicoes=VPvetor114<VPvetor98;
achaZero=find(posicoes==0);
achaPrimeiroZero=min(achaZero);
plot(100*ivetor(achaPrimeiroZero),VPvetor114(achaPrimeiroZero),'ok')
else
posicoes=VPvetor98<VPvetor114;
achaZero=find(posicoes==0);
achaPrimeiroZero=min(achaZero);
plot(100*ivetor(achaPrimeiroZero),VPvetor114(achaPrimeiroZero),'ok')
end
text(100*ivetor(achaPrimeiroZero),50+VPvetor114(achaPrimeiroZero), ...
['Juros de equilibrio (a.m.) = ',num2str(100*ivetor(achaPrimeiroZero)),' %'] )
Exemplo
5
Ou...
xmin = fminsearch('custo', 4)
plot(x, y,'ob')
hold on
pause(0.1)
y=((x-3).^2)-6;
Exemplo
6
%pontos experimentais
yexp=[1.1 2.12 2.85 4.4 5.0 6.5];
xexp=[1 2 3 4 5 6];
Parametros = fminsearch('custo',[1,2]);
Exemplo
7
t < tdegrau
t > tdegrau
Programa principal:
continua...
% Condies iniciais
Cr0 = 0.16; % lbm/ft3
T0 = 603; %R
% Simulao do modelo
[t,y] = ode45('dcstrdeg',t,[Cr0 T0],[],[U A DH Ro Cp E R k0 V Te Th
Fe Cre],[td fd]);
% Visualizao da simulao
figure(1);
plot(t,y(:,1)); title('CSTR com Reao Exotrmica');
xlabel('Tempo (h)'); ylabel('Concentrao de Reagente (lbm/ft3)');
figure(2);
plot(t,y(:,2)); title('CSTR com Reao Exotrmica');
xlabel('Tempo (h)'); ylabel('Temperatura (R)');
Funo dcstrdeg:
function dy = dcstrdeg(t,y,flag,par,deg);
U = par(1); A = par(2);
DH = par(3); Ro = par(4);
Cp = par(5); E = par(6);
R = par(7); k0 = par(8);
V = par(9); Te = par(10);
Th = par(11);
continua...
Fe = deg(2);
else
Fe = par(12);
end;
Cre = par(13);
dy(1) = (Fe/V)*(Cre-y(1)) - k0*exp(-E/(R*y(2)))*y(1);
dy(2) = (Fe/V)*(Te-y(2)) + ((DH*k0*exp(-E/(R*y(2)))*y(1))/(Ro*Cp)) - ...
(U*A*(y(2)-Th)/(V*Ro*Cp));
dy = dy(:);
Exemplo
8
Programa principal:
% Tempo de simulao
t = 0.0 : 0.01 : 10.0; %h
% Condies iniciais
Cr0 = [0.16 0.32 0.48 0.64]; % lbm/ft3
T0 = 603; %R
continua...
figure(1); legend(leg);
figure(2); legend(leg);
hold off;
Dica!
Dvidas?