% Example 5.1a (Physics 330) %********************************************************* % Define the arrays x and y % Warning: don't make the step size too small or you will % kill the system %********************************************************* clear;close all; x=-1:.1:1;y=0:.1:1.5; %********************************************************* % Use meshgrid to convert these 1-d arrays into 2-d matrices of % x and y values over the plane %********************************************************* [X,Y]=meshgrid(x,y); %********************************************************* % Get F(x,y) by using F(X,Y). Note the use of .* with X and Y % rather than with x and y %********************************************************* F=(2-cos(pi*X)).*exp(Y); %********************************************************* % Note that this function is uphill in y between x=-1 and x=1 % and has a valley at x=0 %********************************************************* surf(X,Y,F); xlabel('x'); ylabel('y');