MatLab Hugo Alonso

24
Breve digressão pelo MATLAB baseada em exemplos Hugo Alonso ([email protected]) Universidade de Aveiro 2013/2014 1 / 24

description

Matlab - Quick commands

Transcript of MatLab Hugo Alonso

Page 1: MatLab Hugo Alonso

Breve digressão pelo MATLABbaseada em exemplos

Hugo Alonso

([email protected])

Universidade de Aveiro2013/2014

(([email protected]) ) 1 / 24

Page 2: MatLab Hugo Alonso

MATLAB = MATrix LABoratory

Linguagem de computação técnica + ambiente interactivo

Funcionalidades:computação numéricacomputação simbólicacriação, visualização e manipulação de gráficosprogramação...

(([email protected]) ) 2 / 24

Page 3: MatLab Hugo Alonso

>> 6/2-1*0+2^2ans =7

>> 1/0ans =Inf

>> 0/0ans =NaN

(([email protected]) ) 3 / 24

Page 4: MatLab Hugo Alonso

>> a=1/4, b=pi; c=0a =0.2500c =0

>> bb =3.1416

>> format long

>> bb =3.141592653589793

>> format short

>> bb =3.1416

(([email protected]) ) 4 / 24

Page 5: MatLab Hugo Alonso

>> help sqrtSQRT Square root.SQRT(X) is the square root of the elements of X. Complexresults are produced if X is not positive.

See also sqrtm, realsqrt, hypot.

Overloaded methods:distributed/sqrtcodistributed/sqrtsym/sqrt

Reference page in Help browserdoc sqrt

>> sqrt(a)ans =0.5000

(([email protected]) ) 5 / 24

Page 6: MatLab Hugo Alonso

>> lookfor tangentacot - Inverse cotangent, result in radian.acotd - Inverse cotangent, result in degrees.acoth - Inverse hyperbolic cotangent.atan - Inverse tangent, result in radians.atan2 - Four quadrant inverse tangent.atand - Inverse tangent, result in degrees.atanh - Inverse hyperbolic tangent.cot - Cotangent of argument in radians.cotd - Cotangent of argument in degrees.coth - Hyperbolic cotangent.tan - Tangent of argument in radians.tand - Tangent of argument in degrees.tanh - Hyperbolic tangent.tansig - Hyperbolic tangent sigmoid transfer function.dtansig - Hyperbolic tangent sigmoid transfer derivative function.

(([email protected]) ) 6 / 24

Page 7: MatLab Hugo Alonso

>> help tanTAN Tangent of argument in radians.TAN(X) is the tangent of the elements of X.

See also atan, tand, atan2.

Overloaded methods:distributed/tancodistributed/tansym/tan

Reference page in Help browser

doc tan

>> d=tan(b/4)d =1.0000

(([email protected]) ) 7 / 24

Page 8: MatLab Hugo Alonso

>> whoYour variables are:a ans b c d

>> ansans =0.5000

>> clear a

>> whoYour variables are:ans b c d

>> clear

>> who

(([email protected]) ) 8 / 24

Page 9: MatLab Hugo Alonso

>> diary aula1.txt>> f=inline(’exp(x-1)*sqrt(x)/(1+x^2)’)f =Inline function: f(x) = exp(x-1)*sqrt(x)/(1+x^2)>> f(0),f(1),f(2)ans =0ans =0.5000ans =0.7688>> f=inline(’exp(x-1).*sqrt(x)./(1+x.^2)’)f =Inline function: f(x) = exp(x-1).*sqrt(x)./(1+x.^2)>> f([0 1 2])ans =0 0.5000 0.7688>> diary off

(([email protected]) ) 9 / 24

Page 10: MatLab Hugo Alonso

>> xx=linspace(0,2,1000);

>> plot(xx,f(xx),’k-’,xx,f(xx)+1,’r-’)

>> xlabel(’x’),ylabel(’y’),legend(’f(x)’,’f(x)+1’)

>> grid on

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

x

y

f(x)

f(x)+1

(([email protected]) ) 10 / 24

Page 11: MatLab Hugo Alonso

>> f(x)??? Undefined function or variable ’x’.

>> syms x real

>> f(x)ans =(x^(1/2)*exp(x - 1))/(x^2 + 1)

>> diff(f(x),x)ans =exp(x - 1)/(2*x^(1/2)*(x^2 + 1)) + (x^(1/2)*exp(x - 1))/(x^2 + 1) -(2*x^(3/2)*exp(x - 1))/(x^2 + 1)^2

>> int(f(x),x,0,2)Warning: Explicit integral could not be found.ans =int((x^(1/2)*exp(x - 1))/(x^2 + 1), x = 0..2)

>> quad(f,0,2)ans =0.9462

(([email protected]) ) 11 / 24

Page 12: MatLab Hugo Alonso

>> clear, clc

>> soma=0;

>> for i=1:10,soma=soma+i;end;

>> somasoma =55

>> clear soma

>> i=1:10;

>> soma=sum(i)soma =55

(([email protected]) ) 12 / 24

Page 13: MatLab Hugo Alonso

>> clear

>> u=[1 2 3]u =1 2 3

>> v=[1 2 1]v =1 2 1

>> w=[1 0 1]’w =101

>> whosName Size Bytes Class Attributes

u 1x3 24 doublev 1x3 24 doublew 3x1 24 double

(([email protected]) ) 13 / 24

Page 14: MatLab Hugo Alonso

>> [u v]ans =1 2 3 1 2 1

>> [u;v]ans =1 2 31 2 1

>> [u’ w]ans =1 12 03 1

(([email protected]) ) 14 / 24

Page 15: MatLab Hugo Alonso

>> u+vans =2 4 4

>> u*v??? Error using ==> mtimesInner matrix dimensions must agree.

>> u*wans =4

(([email protected]) ) 15 / 24

Page 16: MatLab Hugo Alonso

>> for i=1:3,y(i)=u(i)*v(i);end;

>> yy =1 4 3

>> y=u.*vy =1 4 3

(([email protected]) ) 16 / 24

Page 17: MatLab Hugo Alonso

>> y(1)ans =1

>> y(1)=0y =0 4 3

>> y([1 3])ans =0 3

>> y([1 3])=[-1 -2]y =-1 4 -2

(([email protected]) ) 17 / 24

Page 18: MatLab Hugo Alonso

>> for i=1:3,if y(i)<0,disp(i);end;end;13

>> find(y<0)ans =1 3

(([email protected]) ) 18 / 24

Page 19: MatLab Hugo Alonso

>> clear

>> A=[1 2;3 4], B=[1 1 0;0 1 0]A =1 23 4B =1 1 00 1 0

>> whosName Size Bytes Class Attributes

A 2x2 32 doubleB 2x3 48 double

>> A*Bans =1 3 03 7 0

>> B*A??? Error using ==> mtimesInner matrix dimensions must agree.

(([email protected]) ) 19 / 24

Page 20: MatLab Hugo Alonso

>> A*Aans =

7 1015 22

>> A^2ans =

7 1015 22

>> A.*Aans =1 49 16

>> A.^2ans =1 49 16

(([email protected]) ) 20 / 24

Page 21: MatLab Hugo Alonso

>> 2*Aans =2 46 8

>> 1+Aans =2 34 5

>> sqrt(A)ans =1.0000 1.41421.7321 2.0000

(([email protected]) ) 21 / 24

Page 22: MatLab Hugo Alonso

>> A(2,1)ans =3

>> A(2,1)=0A =1 20 4

>> B(1,1:3)ans =1 1 0

>> B(1,:)ans =1 1 0

(([email protected]) ) 22 / 24

Page 23: MatLab Hugo Alonso

>> B(1:2,1:2)ans =1 10 1

>> B(:,1:2)ans =1 10 1

>> B(:,[1 3])ans =1 00 0

(([email protected]) ) 23 / 24

Page 24: MatLab Hugo Alonso

Consultar Moodle para mais material!

(([email protected]) ) 24 / 24