Matlab field (quiver) plot and gradient

In summary: Instead of using dp(k).x and dp(k).y, they should use dp(k).x and dp(k).y, since that's what the gradient func. expects.
  • #1
zslevi
8
0
I have been playing around with the Matlab quiver plot, and I found something strange: it seems that the gradient vector isn't computed correctly. ( I use the gradient of an exponential function as a velocity field). Please try the following code. The interesting part is in the last loop (simtime). (It's in the attachment as well.)

Code:
clear;
hold on
figure(1);
title_handle = title('cucc');
simtime = 100;
lenx=50;
leny=50;
field = zeros(lenx,leny);
%field = randn(lenx,leny);


d.x = 20;
d.y = 20;
d.width = 20;
d.height = 10;
datapoints(1) = d;
% these will be the charges, or whatever 

%{
d.x = 30;
d.y = 10;
d.width = 6;
d.height = 3;
datapoints(2) = d;

d.x = 40;
d.y = 25;
d.width = 2;
d.height = 3;
datapoints(3) = d;
%}

for i=1:lenx,
    for j=1:leny,        
        dp = datapoints;      
        for k=1:length(dp),
            field(i,j) = field(i,j) + exp(-1*((dp(k).x-i)^2+(dp(k).y-j)^2)/dp(k).width^2)*dp(k).height;
        end        
    end
end

[DX,DY] = gradient(field,1,1);
quiver(DX,DY);

hold on 

rect.x=3;
rect.y=37;
fill([rect.x,(rect.x+1),(rect.x+1),rect.x],[rect.y,rect.y,(rect.y+1),(rect.y+1)],'g');
ht=fill([rect.x,(rect.x+1),(rect.x+1),rect.x],[rect.y,rect.y,(rect.y+1),(rect.y+1)],'r');
for i=1:simtime,
    ix=(round(rect.x));
    iy=(round(rect.y));
    dx = DX(ix,iy)
    dy = DY(ix,iy)
    rect.x = dx+rect.x;
    rect.y = dy+rect.y;
    X=[rect.x,(rect.x+1),(rect.x+1),rect.x];
    Y=[rect.y,rect.y,(rect.y+1),(rect.y+1)];
    set(ht,'XData',X); % redrawing the moving square
    set(ht,'YData',Y);
        
    str1 = num2str((round(rect.x)),'x:%d, ');
    str2 = num2str((round(rect.y)),'y:%d ');
    str = strcat(str1,str2)

    set(title_handle,'String',str);
    drawnow
end
hold off
 

Attachments

  • fluid.m
    1.4 KB · Views: 520
Physics news on Phys.org
  • #2
Now I've got it.
Both the gradient func., both quiver plot uses the following indexing scheme:
field(y,x)
 

Related to Matlab field (quiver) plot and gradient

What is a Matlab field (quiver) plot?

A Matlab field plot, also known as a quiver plot, is a type of vector plot that displays vectors as arrows in a 2D or 3D space. It is commonly used to visualize vector fields, which represent the magnitude and direction of a vector at each point in a given space.

How do I create a quiver plot in Matlab?

To create a quiver plot in Matlab, you can use the "quiver" function. This function takes in the x, y, and z coordinates of the vectors, as well as their magnitude and direction. You can also customize the appearance of the arrows, such as their color, size, and shape.

What is the difference between a quiver plot and a gradient plot in Matlab?

A quiver plot displays vector fields, while a gradient plot displays scalar fields. This means that a quiver plot shows both the magnitude and direction of a vector at each point, while a gradient plot only shows the magnitude of a scalar value at each point.

Can I add a color map to a Matlab quiver plot?

Yes, you can add a color map to a Matlab quiver plot by using the "colormap" function. This function maps a range of values to a specific color, allowing you to visualize the magnitude of the vectors with different colors.

How can I interpret a Matlab quiver plot?

A Matlab quiver plot can be interpreted by looking at the length, direction, and color of the arrows. The length of the arrows represents the magnitude of the vector, the direction of the arrows represents the direction of the vector, and the color of the arrows can represent different values if a color map is added. Overall, a quiver plot can give a visual representation of the vector field and help identify patterns or trends in the data.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
11
Views
6K
Back
Top