User Tools

Site Tools


supp:octave:contrib:surface_plot:start

Octave: Surface, Contour (Isolines), Gradient

Functions of Two Variables

In Octave analyse the function f(x,y) = x^2*y - 2*y.

  • Understand what a meshgrid is, how it is generated and how it is used.
    Define x=[-2:0.1:2]; and y=[-1:0.1:1]; and generate the meshgrid yielding X and Y.
  • Define Z=(X.^2).*Y-2*Y; . Z is the matrix containing the function f evaluated at all points of the meshgrid.
    Generate a surface plot in the first figure window.
  • Generate a contour plot in the second figure window.
  • Use the function [dX,dY] = gradient(Z); to estimate the gradient of f at each point of the meshgrid.
  • Open a third figure window. Add a contour plot, hold the plot, and add a vector arrow plot (octave buit-in function quiver).
    How are contour lines and gradient vectors related?

Solution

Download the script by klicking on the file name in the box header. Execute the file in Octave. Compare the results with the gradients determined in the lecture.

surf_cont_grad.m
% surf_cont_grad
% Plot surface, contour lines and gradient of functions of two variables
% 2015-04-22, Rolf Becker
 
x = [-2:2]
y = [-1:1]
 
[XX,YY] = meshgrid(x,y) % explain
 
%break
 
x = [-2:0.1:2]; % x values
y = [-1:0.1:1]; % y values
 
[X,Y] = meshgrid(x,y);
 
Z=(X.^2).*Y-2*Y; % the function f(x,y)
 
[dX,dY] = gradient(Z);
 
figure(1)
surf(X,Y,Z)
xlabel("x"); ylabel("y");zlabel("f(x,y)")
 
figure(2)
contour(X,Y,Z)
xlabel("x"); ylabel("y");title("Contour lines of f(x,y)")
grid on 
 
figure(2)
contour(X,Y,Z)
xlabel("x"); ylabel("y");title("Contour lines of f(x,y)")
grid on 
 
figure(3)
contour(X,Y,Z)
hold on
quiver(X,Y,dX,dY)
hold off
xlabel("x"); ylabel("y");title("Gradient f(x,y)")
grid on
supp/octave/contrib/surface_plot/start.txt · Last modified: 2016/04/14 09:12 by admin