User:Eml4500.f08.echo.lacroix/HW1

Homework 1 page...

>> A=[1 2 3; 4 5 6; 7 8 9]; >> A=[1 2; 3 4] + i*[5 6;7 8]

A =

1.0000 + 5.0000i  2.0000 + 6.0000i 3.0000 + 7.0000i  4.0000 + 8.0000i

>> [1,2,3,4].*[1,2,3,4]

ans =

1    4     9    16

>> [1,2,3,4].^2

ans =

1    4     9    16

>> [1,2,3,4]*[1,2,3,4] ??? Error using ==> mtimes Inner matrix dimensions must agree.

>> [1,2,3,4]*[1,2,3,4]'

ans =

30

>> zeros(size(A))

ans =

0    0     0     0

>> test=[1 2 3; 4 5 6; 7 8 9]

test =

1    2     3     4     5     6     7     8     9

>> test2=[1 2 3]

test2 =

1    2     3

>> diag(test2)

ans =

1    0     0     0     2     0     0     0     3

>> diag(test)

ans =

1    5     9

>> diag(test)'

ans =

1    5     9

>> diag(diag(test))

ans =

1    0     0     0     5     0     0     0     9

>> A

A =

1.0000 + 5.0000i  2.0000 + 6.0000i 3.0000 + 7.0000i  4.0000 + 8.0000i

>> A=test

A =

1    2     3     4     5     6     7     8     9

>> B=[A, zeros(3,2);zeros(2,3),eye(2)]

B =

1    2     3     0     0     4     5     6     0     0     7     8     9     0     0     0     0     0     1     0     0     0     0     0     1

>> x=[]; for i = 1:3, x=[x,i^2], end

x =

1

x =

1    4

x =

1    4     9

>> x=[]; for i = 3:-1:1, x=[x,i^2], end

x =

9

x =

9    4

x =

9    4     1

>> 3<5

ans =

1

>> 3>5

ans =

0

>> 3==5

ans =

0

>> 3==3

ans =

1

>> a=rand(5)

a =

0.9501   0.7621    0.6154    0.4057    0.0579    0.2311    0.4565    0.7919    0.9355    0.3529    0.6068    0.0185    0.9218    0.9169    0.8132    0.4860    0.8214    0.7382    0.4103    0.0099    0.8913    0.4447    0.1763    0.8936    0.1389

>> b=triu(a)

b =

0.9501   0.7621    0.6154    0.4057    0.0579         0    0.4565    0.7919    0.9355    0.3529         0         0    0.9218    0.9169    0.8132         0         0         0    0.4103    0.0099         0         0         0         0    0.1389

>> a==b

ans =

1    1     1     1     1     0     1     1     1     1     0     0     1     1     1     0     0     0     1     1     0     0     0     0     1

>> max(a)

ans =

0.9501   0.8214    0.9218    0.9355    0.8132

>> max(max(a))

ans =

0.9501

>> eig(a)

ans =

2.8043           -0.0490 + 0.5617i -0.0490 - 0.5617i 0.2093           -0.0381

>> [U,D]=eig(a)

U =

-0.4565            0.0663 + 0.1672i   0.0663 - 0.1672i  -0.3303             0.1189 -0.4427           -0.0382 + 0.0756i  -0.0382 - 0.0756i   0.6049            -0.5044 -0.5215           -0.1729 - 0.5325i  -0.1729 + 0.5325i  -0.5077             0.3669 -0.4070           -0.1619 + 0.3456i  -0.1619 - 0.3456i   0.1671             0.2074 -0.3975            0.7081             0.7081             0.4892            -0.7442

D =

2.8043                 0                  0                  0                  0                  0            -0.0490 + 0.5617i        0                  0                  0 0                 0            -0.0490 - 0.5617i        0                  0 0                 0                  0             0.2093                  0                  0                  0                  0                  0            -0.0381

>> A(:,[2 4 5]) = B(:,1:3)

A =

0.9501   1.0525    0.6154   -1.7572    0.8069    0.2311         0    0.7919    2.1907   -1.8821    0.6068         0    0.9218         0    1.0848    0.4860         0    0.7382         0         0    0.8913         0    0.1763         0         0

>> A(:,[2,4])=A(:,[2,4])*[1 2;3 4]

A =

0.9501  -4.2191    0.6154   -4.9238    0.8069    0.2311    6.5722    0.7919    8.7629   -1.8821    0.6068         0    0.9218         0    1.0848    0.4860         0    0.7382         0         0    0.8913         0    0.1763         0         0

>> x=[2 5; 3 8]

x =

2    5     3     8

>> x=x(2:-1:1)

x =

3    2

>> y=fliplr(x)

y =

2    3

>> y=flipud(x')

y =

2    3

>> why A smart programmer suggested it.

>> flops Warning: Flop counts are no longer available.

>> tic, x=A\b, toc

x =

0.0000   0.0000         0   -0.1264    0.1761   -1.8055   -1.9356   -2.4197   -0.9192   -0.6836   -0.0000   -0.0000   -0.0000    0.6390   -0.1026    1.3541    1.5038    2.0876    0.8218    0.7161         0    0.0000    0.8497    0.3730    0.7382

Elapsed time is 0.000486 seconds. >> tic, x=inv(A)*b, toc

x =

0.0000   0.0000    0.0000   -0.1264    0.1761   -1.8055   -1.9356   -2.4197   -0.9192   -0.6836   -0.0000   -0.0000   -0.0000    0.6390   -0.1026    1.3541    1.5038    2.0876    0.8218    0.7161    0.0000    0.0000    0.8497    0.3730    0.7382

Elapsed time is 0.000455 seconds.

(in an m.file “expnormal”): function y = expnormal(x) y=exp(-x.^2);

>> fplot('expnormal',[-1.5,1.5])

>> t=0:.001:2*pi; x=cos(3*t); y=sin(2*t); plot(x,y)

>> t=.01:.01:20*pi; x=cos(t);y=sin(t);z=t.^3;plot3(x,y,z)

>> mesh(eye(10))

>> surf(eye(10))

>> xx=-2:.2:2; yy=xx; [x,y]=meshgrid(xx,yy); z=exp(-x.^2-y.^2);mesh(z)

>> xx=-2:.2:2; yy=xx; [x,y]=meshgrid(-2:.2:2,-2:.2:2); z=exp(-x.^2-y.^2);surf(z)

>> shading interp >> xx=-2:.2:2; yy=xx; [x,y]=meshgrid(-2:.2:2,-2:.2:2); z=exp(-x.^2-y.^2);surf(z) >> colormap cool >> xx=-2:.2:2; yy=xx; [x,y]=meshgrid(-2:.2:2,-2:.2:2); z=exp(-x.^2-y.^2);surf(z) >> colormap copper >> xx=-2:.2:2; yy=xx; [x,y]=meshgrid(-2:.2:2,-2:.2:2); z=exp(-x.^2-y.^2);surf(z) >> colormap flag >> xx=-2:.2:2; yy=xx; [x,y]=meshgrid(-2:.2:2,-2:.2:2); z=exp(-x.^2-y.^2);surf(z)

>> F=floor(10*rand(6)); F=triu(tril(F,1),-1); >> S=sparse(F)

S =

(1,1)       9   (2,1)        7   (1,2)        9   (2,2)        6   (3,2)        2   (2,3)        6   (3,3)        6   (4,3)        3   (3,4)        3   (5,4)        3   (4,5)        6   (5,6)        6   (6,6)        7

>> issparse(A)

ans =

0

>> m=6;n=6;e=ones(n,1);d=-2*e; >> T=spdiags([e,d,e],[-1,0,1],m,n)

T =

(1,1)      -2   (2,1)        1   (1,2)        1   (2,2)       -2   (3,2)        1   (2,3)        1   (3,3)       -2   (4,3)        1   (3,4)        1   (4,4)       -2   (5,4)        1   (4,5)        1   (5,5)       -2   (6,5)        1   (5,6)        1   (6,6)       -2

>> i=[1 2 3 4 4 4]; j=[1 2 3 1 2 3]; s=[5 6 7 8 9 10]; S =sparse(i,j,s,4,3),full(S)

S =

(1,1)       5   (4,1)        8   (2,2)        6   (4,2)        9   (3,3)        7   (4,3)       10

ans =

5    0     0     0     6     0     0     0     7     8     9    10

>> sparse(i,j,s,m,n)

ans =

(1,1)       5   (4,1)        8   (2,2)        6   (4,2)        9   (3,3)        7   (4,3)       10

>> n=6;e=floor(10*rand(n-1,1));E=sparse(2:n,1:n-1,e,n,n)

E =

(2,1)       6   (4,3)        4   (5,4)        4   (6,5)        3