Table of Contents

Octave: Surface, Contour (Isolines), Gradient

Functions of Two Variables

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

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