Sei sulla pagina 1di 4

MATH 255 Graphing Surfaces and Symbolic Computations using MATLAB For your Copy and Paste convenience,

this le has been posted online at: http://snoopy.oit.edu/~fischerj/Matlab/matlab.html EXAMPLE 1: Graphing z = f (x, y) over a rectangle R = [a, b] [c, d]. Here z is the paraboloid z = x2 + y 2 and R = [5, 8] [4, 6]. % This program will graph z = f(x,y) over a rectangle x = linspace(-5,8); y = linspace(-4,6); [x,y] = meshgrid(x,y); z = x.^2 + y.^2 % Note the requirement of "dot times" notation since x and y are vectors. surf(x,y,z) xlabel(x) ylabel(y) zlabel(z) title(Graph of z = x^2 + y^2) EXAMPLE 2: Graphing the function f (x, y) over a disc. Just use polar coordinates! % This program will graph z = f(x,y) over a disc of radius r r = 2; r = linspace(0,r); theta = linspace(0,2*pi); [r,theta] = meshgrid(r,theta); x = r.*cos(theta); y = r.*sin(theta); z = x.^2 + y.^2; % Note the requirement of "dot times" notation since x and y are vectors. surf(x,y,z) xlabel(x) ylabel(y) zlabel(z) title(Graph of z = x^2 + y^2 over a disc) Do you see how simple it would be to modify the above to graph over a washer shaped region? EXAMPLE 3: Graphing a torus. Here we need to specify two radii, a small radius r and large radius R. There are 2 rotation angles alpha and beta, both ranging from 0 to 2 radians. % This program will graph a torus with small radius r and large radius R. % Change the values of r and R here : 1

r = 1; R = 2; alpha = linspace(0,2*pi,200); beta = alpha; [alpha,beta] = meshgrid(alpha,beta); % Note that you can change the above angle ranges to get % different parts of a torus. % Also, you can change refinement by including more or less points x = (R + r*cos(alpha)).*cos(beta); y = (R + r*cos(alpha)).*sin(beta); z = r*sin(alpha); surf(x,y,z) xlabel(x) ylabel(y) zlabel(z) title(The Graph of a Torus) EXAMPLE 4: Graphing a surface of revolution about the y axis. To change the prole curve, just change the lines where x and z are dened. % This program will graph a surface of revolution about the y axis. % Here z = f(y) is the profile curve % so that x = f(y)cos(theta) and z = f(y)sin(theta) % in this example f(y) = 0.1*cosh(y) producing a catenoid a = -2; b = 2; y = linspace(a,b); theta = linspace(0,2*pi); [y,theta] = meshgrid(y,theta); x = cosh(y).*cos(theta); z = cosh(y).*sin(theta); % Note the requirement of "dot times" notation since x and y are vectors. surf(x,y,z) xlabel(x) ylabel(y) zlabel(z) title(Revolution of z = cosh(y) about y axis) EXAMPLE 5: Graphing using spherical coordinates. We will graph a side of a sphere that is centered at (0, 0, 0) together with the same side of a sphere centered at (a, b, c) % This program will graph the "right" side of a sphere % centered at (0,0,0)$ and also the same side of a sphere % shifted to (a,b,c) % Change the values of rho, phi and theta here: phi = linspace(0,pi); theta = linspace(0,pi); [phi,theta]=meshgrid(phi,theta); 2

% you can change to a different surface by using a different % function rho = F(phi, theta) % for the sphere, rho is just a constant rho = 2; % change to rectangular in order to plot x = rho*sin(phi).*cos(theta); y = rho*sin(phi).*sin(theta); z = rho*cos(phi); surf(x,y,z) hold on % shift center to (a,b,c) = (2,-1, 3); change values here: xs = x - 2; ys = y + 1; zs = z - 3; surf(xs,ys,zs) hold off xlabel(x) ylabel(y) zlabel(z) title(Side of Sphere and shifted Right side) % square up axes axis square EXAMPLE 6: Graphing a surface over a type I region and graphing domains. This example was suggested by Brian M. Details were discussed in class. % Graph a surface over a type I region. Note that x ranges from a to b % and y ranges from g(x) to h(x) % % Start with a rectangle in u, v a = 1; b = 2; u = linspace(a,b); v = linspace(0,1); [u,v] = meshgrid(u,v); % use y = h(x)*(1 - v) + g(x)*v x = u; y = (-1 + 2*x).*(1 - v) + (9- 1.5*x.^2).*v; z = 1 + x.^2 +y.^2; % graph surface and domain on same grid surf(x,y,z) hold on surf(x,y,0*z) title(Surface over type-1 region) xlabel(x) ylabel(y) zlabel(z) hold off 3

%open second figure and graph domain with birds eye view figure surf(x,y,0*z) view(0,90) title(Type-1 Region (domain)) xlabel(x) ylabel(y)

Symbolic Computations using MATLAB EXAMPLE 7 Integration. The following could be used to compute the following double integral:
2 1 9x2

ysin(x) dydx
x2

% declare x and y as symbolic. Note that any expression in x and y will be % symbolic. Example >> f = x^2 + y^2 declares f as symbolic. syms x y f = y*sin(x); I = int(int(f,y,x^2,9 - x^2),x,1,2) % convert exact I to decimal approximation I2 I2 = double(I)

Potrebbero piacerti anche