function GraphZvsXY(x_min, x_max, N_x, y_min, y_max, N_y,FunctionString) % % function GraphZvsXY(x_min, x_max, N_x, y_min, y_max, N_y,FunctionString) % % Plots the graph of a function z=f(x,y) over the domain % x_min <= x <= x_max, y_min <= y <= y_max, with N_x grid points % in the x-direction and N_y grid points in the y-direction. % The function is specified by the string FunctionString. % For example to plot z=x^2 + y^2 the value of FunctionString would be % 'x.^2 + 3*y.^2' % % Why do we need the . in x.^2? % Well as far as matlab is concerned x is a matrix (of size 1 by N_x) % and matlab interprets x*x as matrix multiplication (see Math 221), and x^2 as % a power of a matrix. However our goal is to compute % a new vector whose j-th entry is (x_j)^2. This cannot be done % using matrix multiplication. So that matlab knows what we want we use % x.^2 in this situation. Similarly we would use x.*y to create a vector % whose j-th entry is the product of the j-th entry of x and the j-th entry % of y. % % Typical call: % % GraphZvsXY(-1,1,10,-1,1,10,'y.^2-x.^2') x = linspace(x_min,x_max,N_x); y = linspace(y_min,y_max,N_y); [X,Y] = meshgrid(x,y); Function = inline(FunctionString); Z = Function(X,Y); C = ones(size(Z)); surf(X,Y,Z,C,'linewidth',2) % This gets the handle for the current axis hax=gca; % This sets the font name and size for all the text on the plot, including % tick labels and axis labels. set(hax,'fontname','helvetica','fontweight','bold','fontsize',20); xlabel('x') ylabel('y') zlabel('z') colormap('hsv');