WaitForIt
WaitForIt

Reputation: 131

Plotting 2D functions on a grid

Hello I have a function f:[0,1]^2 -->R^2 to plot. f(x,y)=((x+1)/y, -(x+1)/y). I have to create an equidistant grid on [0,1]^2 consisting of 2*N grid lines and (M+1) grid points.


But I don't know how to do it for 2 dimesnional functions. I can do it for 1D functions:

    % Generate equidistant grid on [0,1] and plot grid points
    M=25;             %number of internal grid points
    xgrid = linspace(0,1,M+1)';
    null = zeros(size(xgrid));
    plot(xgrid,null,'.','MarkerSize',15)
    % Sample function at the grid points and plot samples
    ysample = xgrid+1;
    plot(xgrid,ysample,'r.','MarkerSize',15);
    title('Sampling a function on the grid')
    hold off

Can anyone tell me how to do it for a 2-D function f:[0,1]-->R^2?

Upvotes: 0

Views: 189

Answers (1)

G.J
G.J

Reputation: 795

Are you trying to achieve something like this ?

M = 25;
x = linspace(0, 1, M);
y = linspace(0, 1, M);
[mesh_x, mesh_y] = meshgrid(x, y);
v1 = (mesh_x + 1) ./ mesh_y;
v2 = -(mesh_x + 1) ./ mesh_y;

figure('Name', 'My Plot');
subplot(121);
surf(x, y, v1);
grid on;xlabel('x');ylabel('y');zlabel('v1');
subplot(122);
surf(x, y, v2);
grid on;xlabel('x');ylabel('y');zlabel('v2');

Upvotes: 2

Related Questions