Top Banner
Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x= linspace (0,0); y= linspace (-5,5); z=y.^2; plot3 (x,y,z); To produce exactly what I’ve graphed above you need to change various appearences: x= linspace (0,0); y= linspace (-5,5); z=y.^2; plot3 (x,y,z, ' k ' , ' LineWidth ' ,2); lims = axis ; % lims = [xmin xmax ymin ymax zmin zmax] axis off hold on; plot3 (lims(1:2),[0 0],[0 0], ' k ' , ' LineWidth ' ,2) % for x-axis plot3 ([0 0],lims(3:4),[0 0], ' k ' , ' LineWidth ' ,2) % for y-axis plot3 ([0 0], [0 0], lims(5:6), ' k ' , ' LineWidth ' ,2) % for z- axis text ([lims(2)+0.5 0 0],[0 lims(4)+0.5 0],[0 0 lims(6)+1.5],{ ' x ' , ' y ' , ' z ' }, ' FontSize ' ,15); view (-263,15); 178
32

Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

May 03, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

Appendix A

Matlab examples

12.1

y

z

x

The simplest way to graph this is:

x=linspace (0,0);

y=linspace (-5,5);

z=y.^2;

plot3(x,y,z);

To produce exactly what I’ve graphed above you need to change various appearences:

x=linspace (0,0);

y=linspace (-5,5);

z=y.^2;

plot3(x,y,z,'k','LineWidth ' ,2);lims = axis; % lims = [xmin xmax ymin ymax zmin zmax]

axis off

hold on;

plot3(lims (1:2) ,[0 0],[0 0],'k','LineWidth ' ,2) % for x-axis

plot3 ([0 0],lims (3:4) ,[0 0],'k','LineWidth ' ,2) % for y-axis

plot3 ([0 0], [0 0], lims (5:6) ,'k','LineWidth ' ,2) % for z-

axis

text([lims (2) +0.5 0 0],[0 lims (4) +0.5 0],[0 0 lims (6) +1.5] ,{

'x', 'y', 'z'},'FontSize ' ,15);view (-263,15);

178

Page 2: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 179

hold off;

y

z

x

x=linspace (0,0);

y=linspace (-5,5);

z=y.^2;

hold on;

plot3(x,y,z);

plot3(x+0.5,y,z);

plot3(x+1,y,z);

plot3(x-0.5,y,z);

hold off;

y

z

x

[x,y]= meshgrid(linspace (-2,2,5),linspace (-5,5));

z=y.^2;

surf(x,y,z);

Page 3: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 180

y

z

x

z=linspace (0,0);

x=linspace (-5,5);

y=sin(x);

plot3(x,y,z);

y

z

x

z=linspace (0,0);

x=linspace (-5,5);

y=sin(x);

plot3(x,y,z);

hold on;

plot3(x,y,z+0.1);

plot3(x,y,z-0.6);

hold off;

Page 4: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 181

-10

x

0-1

-1

-0.5

y

-0.50

0z

100.51

0.5

1

[x,z]= meshgrid(linspace (-2*pi ,2*pi),linspace (-1,1,10));

y=sin(x);

surf(x,y,z);

12.3

u · v = 4⇥ 2� 1⇥ 5 + 3⇥ (�6)

= 8� 5� 18

= �15

u=[4 -1 3]

v=[2 5 -6]

u*v' % this or

dot(u,v) % this

Page 5: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 182

|a| =p22 + 32 + 12

=p4 + 9 + 1

=p14

a · b = 2⇥ 1� 3⇥ 6 + 1⇥ (�2)

= 2� 18� 2

= �18

proja

(b) =a · b|a|

a

|a|

=�18p14

ap14

=�18

14a

= �9

7a

=

⌧�18

7, �24

7,�9

7

compa

(b) =a · b|a|

=�18p14

%% Calculate Proj and comp for just this a and b

a = [2 -3 1]

b = [1 6 -2]

a*b'*a/norm(a)^2a*b'/norm(a)%% Or, define proj and comp as functions

proj = @(a,b) a*b'*a/norm(a)^2;comp = @(a,b) a*b'/norm(a);a = [2 -3 1]

b = [1 6 -2]

proj(a,b)

comp(a,b)

Page 6: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 183

12.4

a⇥ b =

������

i j k

3 �4 11 2 5

������

= i

�����4 12 5

����� j

����3 11 5

����+ k

����3 �41 2

����

= i(�4(5)� 1(2))� j(3(5)� 1(1)) + k(3(2)� (�4)(1))

= �22i� 14j + 10k

= h�22, �14, 10ia = [3 -4 1]

b = [1 2 5]

cross(a,b)

V = |a · (b⇥ c)|

=

�������

������

1 4 52 �1 15 2 7

������

�������

=

����� 1�����1 12 7

����� 4

����2 15 7

����+ 5

����2 �15 2

����

�����

=��1(�1(7)� 1(2))� 4(2(7)� 1(5)) + 5(2(2)� (�1)(5))

��

= |�9� 36 + 45)|= 0

a = [1 4 5]

b = [2 -1 1]

c = [5 2 7]

abs(a*cross(b,c)')

Page 7: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 184

12.6

%% plot ellipsoid

[x,y,z]= ellipsoid (0,0,0,6,3,3,30);

surf(x,y,z);

axis off

axis equal

grid off

Page 8: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 185

%% plot hyperboloid of one sheet

t=linspace (-10,10,25); % parameterizes z-value

[x,y,z] = cylinder(sqrt (0.003*t.^2+0.05));

surf(x,y,z);

axis equal

view (-38,13);

grid off

axis off

Page 9: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 186

%% plot cone

t=0:pi /30:2* pi;

[x,y,z] = cylinder ([1 0 1]);

surf(x,y,z);

axis off

axis equal

grid off

view (-32,11)

Page 10: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 187

%% hyperboloid of two sheets

x = linspace (-7,7,25);

y = linspace (-4,4,25);

[X,Y] = meshgrid(x,y);

z = sqrt(X.^2/3 + Y.^2+1);

surf(X,Y,z); hold on

z=-sqrt(X.^2/3+Y.^2+1);

surf(X,Y,z); hold off

axis equal

zlim([-4 4])

view (-38,11)

grid off

axis off

Page 11: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 188

%% plot elliptic parabaloid

x = linspace (-5,5,25);

y = linspace ( -2.5 ,2.5 ,25);

[X,Y] = meshgrid(x,y);

z = X.^2/4+Y.^2;

surf(X,Y,z)

axis equal

zlim ([0 5])

grid off

axis off

Page 12: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 189

%% hyperbolic paraboloid

x = linspace (-7,7,25);

y = linspace (-4,4,25);

[X,Y] = meshgrid(x,y);

z = X.^2/4 - Y.^2;

surf(X,Y,z);

axis equal

zlim([-4 4])

view (-38,11)

grid off

axis off

Page 13: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 190

13.1

0

5

10

15

−4

−2

0

2

4−3

−2

−1

0

1

xy

z

t=linspace (0,4*pi);

x = t;

y = 3*cos(3*t+3*pi/2);

z = sqrt(t) -5;

plot3(x,y,z);

xlabel('x'),ylabel('y'),zlabel('z')

−1

−0.5

0

0.5

1

0

5

10

15−1

−0.5

0

0.5

1

xy

z

t = linspace (0,4*pi);

x = cos(2*t);

y = t;

z = sin(2*t);

plot3(x,y,z);

xlabel('x'),ylabel('y'),zlabel('z')

Page 14: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 191

−2

−1

0

1

2

−1

−0.5

0

0.5

1−1

−0.5

0

0.5

1

xy

z

t = linspace (0,2*pi);

x = sqrt (2)*cos(t);

y = sin(t);

z = sin(t);

plot3(x,y,z);

xlabel('x'),ylabel('y'),zlabel('z')

−2

−1

0

1

2

−2

−1

0

1

2−2

−1

0

1

2

xy

z

t = linspace (0,2*pi);

x = 2*cos(t);

y = 2*sin(t);

z = 4*sin(t).*cos(t);

plot3(x,y,z);

xlabel('x'),ylabel('y'),zlabel('z')

Page 15: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 192

t = linspace (0,2*pi);

x = 2*cos(t);

y = 2*sin(t);

z = 4*sin(t).*cos(t);

plot3(x,y,z, 'k', 'LineWidth ', 3);

hold on

[xc,yc,zc]= cylinder (2 ,50)

surf(xc,yc ,3*zc)

surf(xc,yc ,-3*zc)

alpha (0.3)

[u,v]= meshgrid(linspace (-2,2));

z=u.*v;

mesh(u,v,z)

xlabel('x'),ylabel('y'),zlabel('z')hold off

Page 16: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 193

13.2

−1 0 1 2 3 4 5 6 7 80

0.5

1

1.5

2

2.5

3

3.5

4

t=linspace (-1,2);

x=t.^3;

y=t.^2;

plot(x,y,'k')hold on

plot(1,1,'r*')quiver (1,1,3,2,1,'r');hold off

Page 17: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 194

14.1

−5

0

5

−5

0

5−30

−20

−10

0

10

20

30

x

z=x2 − y

2

y

z

[x,y]= meshgrid(linspace (-5,5));

z=x.^2-y.^2;

mesh(x,y,z)

xlabel('x')ylabel('y')zlabel('z')title('z=x^2 - y^2');

−20 −20

−20 −20

−15

−15

−15

−15

−10

−10

−10

−10

−10

−10

−5

−5 −5

−5

−5 −5

0

0

0

0

0

0

0

0

5

5

5

5

5

5

10

10

10

10

10

10

15

15

15

15

20

20

20

20

Level curves for z=x2 − y2

10 20 30 40 50 60 70 80 90 100

10

20

30

40

50

60

70

80

90

100

[x,y]= meshgrid(linspace (-5,5));

z=x.^2-y.^2;

contour(z)

[c,h]= contour(z);

clabel(c,h)

title('Level curves for z=x^2 - y^2')

Page 18: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 195

14.2

−5

0

5

−5

0

50

10

20

30

40

50

Example 2: h(x,y) = x2 + y2

[x,y]= meshgrid(linspace (-4,4,35));

z=x.^2+y.^2;

mesh(x,y,z)

xlabel('x'),ylabel('y'),zlabel('z')title('z=x^2+y^2')

−2.3−2.2

−2.1−2

−1.9−1.8

−1.7−1.6

2.6

2.8

3

3.2

3.4150

200

250

300

350

400

x

Example: limit as (x,y) −> (−2,3)

y

z

u=linspace (-4,0,25);

v=linspace (1,5,25);

[x,y]= meshgrid(u,v);

z=x.^3-2*y.*x.^2+y.^2 -5.*x.*y.^3;

mesh(x,y,z)

xlabel('x'),ylabel('y'),zlabel('z')title('Example: limit as (x,y) -> (-2,3)')

Page 19: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 196

−4

−2

0

2

4

−4

−2

0

2

4−0.5

0

0.5

x

g(x,y)=(xy)/(x2+y2)

y

z

[x,y]= meshgrid(linspace (-4,4,35));

z=x.*y./(x.^2+y.^2);

mesh(x,y,z)

xlabel('x'),ylabel('y'),zlabel('z')title('g(x,y)=(xy)/(x^2+y^2)')

−10

−5

0

5

10

−10

−5

0

5

10−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

x

Example 3 −− the sinc function

y

f(x,

y)

[x,y] = meshgrid(linspace (-10,10,40));

R=sqrt(x.^2 + y.^2);

z = sin(R)./R;

mesh(x,y,z)

title ('Example 3 -- the sinc function ')xlabel('x'), ylabel('y'), zlabel('f(x,y)')

Page 20: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 197

14.3

−4

−2

0

2

4

−4

−2

0

2

4−4

−3

−2

−1

0

1

2

3

4

xy

z

[x,y]= meshgrid(linspace (-4,4,50),linspace (-1,4,25));

z=y.*sin(x);

mesh(x,y,z)

alpha 0.3

hold on

x=linspace(pi/6-5,pi/6+5);

y=1+0*x;

z=y.*sin(x);

plot3(x,y,z,'r','LineWidth ' ,3)alpha 0.3

z=(sqrt (3)/2)*(x-pi/6)+1*sin(pi/6);

plot3(x,y,z,'k','LineWidth ' ,3)xlabel('x'),ylabel('y'),zlabel('z')

Page 21: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 198

14.4

%% zoomed out

clc , clf % clear console and clear figure

[x,y] = meshgrid(linspace (-2,2));

z=exp(x.^2-y.^2);

mesh(x,y,z,'EdgeAlpha ','0.5')hold on

z=2*(x-1) +2*(y+1)+1;

surf(x,y,z,'EdgeColor ','green ','EdgeAlpha ','0.5','FaceAlpha ','0.5')

plot3(1,-1,1,'ro','MarkerFaceColor ','r')

Page 22: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 199

%% zoomed in and rotated

clc , clf

[x,y] = meshgrid(linspace (0.5 ,1.5), linspace ( -1.5 ,-0.75));

z=exp(x.^2-y.^2);

mesh(x,y,z,'EdgeAlpha ','0.5')hold on

z=2*(x-1) +2*(y+1)+1;

surf(x,y,z,'EdgeColor ','green ','EdgeAlpha ','0.5','FaceAlpha ','0.5')

plot3(1,-1,1,'ro','MarkerFaceColor ','r')view (11 ,4)

Page 23: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 200

14.8

-3.5 -3 -2.5 -2 -1.5 -1 -0.5

2

2.2

2.4

2.6

2.8

3

3.2

3.4

3.6

3.8

4

x=linspace (-3.5,-0.5);

y=linspace (2,4);

[X,Y]= meshgrid(x,y);

Z=2*X.^2+3*Y.^2-4*X-5;

contour(X,Y,Z,'LineColor ','b','LineWidth ' ,1.5);hold on

contour(X,Y,Z,'LineColor ','b','LineWidth ' ,1.5,'LevelList ',47);

contour(X,Y,X.^2+Y.^2,'LineColor ','r','LineWidth ' ,1.5,'LevelList ' ,16);

Page 24: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 201

15.2

x=linspace (0,2,20);

y=linspace (0,1,20);

[X,Y] = meshgrid(x,y);

Z=X.*exp(-X.*Y);

surf(X,Y,Z,'FaceAlpha ' ,0.5);hold on

surf(X,Y,0*X,'FaceColor ','green ','FaceAlpha ' ,0.5);hold off

Page 25: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 202

15.3

% initial values of x and y

[ix,iy] = meshgrid(linspace (0,1,20),linspace (0,1,10));

x=ix; % real values for x

y=iy.*sqrt(ix)+(1-iy).*ix.^2; % real values for y

z=x+y; % define top surface

surf(x,y,z,'FaceAlpha ' ,0.5); % graph top surface

hold on

% graph bottom surface , note "0" in z-coordinate

surf(x,y,0.*x,'FaceAlpha ' ,0.5,'FaceColor ','green ')hold off

view (-4,24) % adjust perspective

Page 26: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 203

15.7

V = [0 0 0; 0 1 0; 1 1 0; 0 1 1];

F = [1 2 3; 1 2 4; 1 3 4; 2 3 4];

p=patch('Faces ',F,'Vertices ',V);view (3);

% the rest is just visual tweeks

p.FaceColor =['cyan'];p.FaceAlpha =0.5;

xlabel('x');ylabel('y');zlabel('z');axis ([0 1.5 0 1.5 0 1.25])

pbaspect ([1 1 0.75])

grid on;

view (118 ,8);

Page 27: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 204

[x,y] = meshgrid(linspace (0,1,10), linspace (0,3,30));

z=sqrt(9-y.^2);

surf(x,y,z,'FaceAlpha ' ,0.95);hold on

% same x and y, set z=0

surf(x,y,0.*x,'FaceColor ','black ','FaceAlpha ' ,0.5);% now use y and z as input variables

[y,z]= meshgrid(linspace (0,3,10));

surf(y./3,y,z,'FaceColor ','cyan','FaceAlpha ' ,0.25);%x = y/3

surf (0.*y,y,z,'FaceColor ','red','FaceAlpha ' ,0.5); % x =0

xlabel('x');ylabel('y');zlabel('z');view (118 ,12);

hold off

Page 28: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 205

[x,y] = meshgrid(linspace (0 ,1 ,100), linspace (0 ,3 ,100));

z=sqrt(9-y.^2);

z(y<3.*x)=NaN;

surf(x,y,z,'FaceAlpha ' ,0.95,'EdgeColor ','none');hold on

z=0.*x;

z(y<3.*x)=NaN;

surf(x,y,z,'FaceColor ','black ','FaceAlpha ' ,0.5);[y,z]= meshgrid(linspace (0 ,3 ,100));

x=y./3;

x(y>sqrt(9-z.^2))=NaN;

surf(x,y,z,'FaceColor ','cyan','FaceAlpha ' ,0.75,'EdgeColor ','none');

surf (0.*x,y,z,'FaceColor ','red','FaceAlpha ' ,0.5);% add back some black lines

[x,y]= meshgrid(linspace (0,1,10),linspace (0,3,30));

z=sqrt(9-y.^2);

z(y<3.*x)=NaN;

mesh(x,y,z,'EdgeColor ','black ');xlabel('x');ylabel('y');zlabel('z');view (118 ,12);

hold off

Page 29: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 206

-4

-3

-2

-5

-1

0

1

2

z

3

4

y

00

x

5105 15

[y,z]= meshgrid(linspace (-4,4,20));

x=y.^2+z.^2;

surf(x,y,z);

xlabel('x');ylabel('y');zlabel('z');xlim ([0 16]);

view (157 ,16)

16.1

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

[x,y]= meshgrid (0:0.1:1 , 0:0.1:1);

z=exp(x).*sin(y);

[dx ,dy]= gradient(z,0.1 ,0.1);

quiver(x,y,dx ,dy ,1.5)

xlim ([ -0.01 1])

ylim ([0 1])

Page 30: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 207

16.2

0

x

00.5

0.2

0.4

0.6

-0.2

0.8

z

1

0

1.2

1.4

0.2

y

0.4 10.6 0.8 1 1.2

t=linspace(0,pi/2);

x=cos(t);

y=sin(t);

z=exp(x).*sin(y);

plot3(x,y,z,'LineWidth ' ,2)hold on

plot3(x,y,0.*t,'LineWidth ' ,2);% pretty it up

xlabel('x');ylabel('y');zlabel('z');xlim ([ -0.25 1.25])

ylim ([ -0.25 1.25])

view (115 ,12)

grid on

hold off

Page 31: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 208

t=linspace(0,pi/2);

x=cos(t);

y=sin(t);

z=exp(x).*sin(y);

plot3(x,y,z,'LineWidth ' ,2)hold on

plot3(x,y,0.*t,'LineWidth ' ,2);% pretty it up

xlabel('x');ylabel('y');zlabel('z');xlim ([ -0.25 1.25])

ylim ([ -0.25 1.25])

view (115 ,12)

grid on

% To fill , we'll make a 3D polygon , using x,y,z values that

go out

% along the top , and then backwards along the bottom

X=horzcat(x,fliplr(x));

Y=horzcat(y,fliplr(y));

Z=horzcat(z,0.*t);

C=Z;

fill3(X,Y,Z,C,'FaceAlpha ' ,0.25)colormap gray %

caxis ([0.1 1.4]) % mute extremes of gray a little bit

brighten ( -0.5)

% Let 's add vertical lines. We don 't need a vertical line

at every

% value of t that we started with , so let 's prune x,y and z

N=5 % take every Nth value of x,y,z

x=x(1:N:length(x));

y=y(1:N:length(y));

z=z(1:N:length(z));

% Now we'll use plot3. Each line segment will come from a

Page 32: Matlab examples - Loyola University Marylandevergreen.loyola.edu/educkworth/www/351_Spring_2019/...Appendix A Matlab examples 12.1 y z x The simplest way to graph this is: x=linspace(0,0);

APPENDIX A. MATLAB EXAMPLES 209

column

% of X,Y,Z so we want to make

% Col_1(X) = [x_1 x_1], etc

% Col_1(Y) = [y_1 y_1], etc

% Col_1(Z) = [0 z_1], etc.

X = [x;x];

Y = [y;y];

Z = [0.*z;z];

plot3(X,Y,Z,'b');hold off