Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Curl

Subject: Curl

From: arron

Date: 20 Feb, 2012 23:13:11

Message: 1 of 7

Hi everyone. I am having trouble with the curl function in the following code:

-------------------------------------------------------------------
Iter = 10;
I = zeros(x,y);
sigma = 3;
G = fspecial('gaussian', 5, sigma);

for n = 1:Iter
A = conv2(I, G, 'same');
%B = curl of A.
end
-------------------------------------------------------------------

So not too complicated...but I would just like to find the curl of A. Can anyone help? Thanks!

Subject: Curl

From: Roger Stafford

Date: 21 Feb, 2012 01:48:10

Message: 2 of 7

"arron" wrote in message <jhuk27$8og$1@newscl01ah.mathworks.com>...
> Hi everyone. I am having trouble with the curl function in the following code:
>
> -------------------------------------------------------------------
> Iter = 10;
> I = zeros(x,y);
> sigma = 3;
> G = fspecial('gaussian', 5, sigma);
>
> for n = 1:Iter
> A = conv2(I, G, 'same');
> %B = curl of A.
> end
> -------------------------------------------------------------------
>
> So not too complicated...but I would just like to find the curl of A. Can anyone help? Thanks!
- - - - - - - - -
  With 'I' set to all zeros, that would make A all zeros, so all its partial derivatives would also be zero.

  Assuming you don't really mean that, another objection is that curl is ordinarily meaningful only in a three-dimensional vector field. However, not only is your A defined only in 2D space, it is not a vector field but only a scalar field and the curl concept cannot be applied with it. If you want the curl of the gradient of A, that would always be identically zero.

  I think you should explain what you really have in mind so that we do not have to guess.

Roger Stafford

Subject: Curl

From: arron

Date: 21 Feb, 2012 10:40:12

Message: 3 of 7

"Roger Stafford" wrote in message <jhut4q$4d4$1@newscl01ah.mathworks.com>...
> "arron" wrote in message <jhuk27$8og$1@newscl01ah.mathworks.com>...
> > Hi everyone. I am having trouble with the curl function in the following code:
> >
> > -------------------------------------------------------------------
> > Iter = 10;
> > I = zeros(x,y);
> > sigma = 3;
> > G = fspecial('gaussian', 5, sigma);
> >
> > for n = 1:Iter
> > A = conv2(I, G, 'same');
> > %B = curl of A.
> > end
> > -------------------------------------------------------------------
> >
> > So not too complicated...but I would just like to find the curl of A. Can anyone help? Thanks!
> - - - - - - - - -
> With 'I' set to all zeros, that would make A all zeros, so all its partial derivatives would also be zero.
>
> Assuming you don't really mean that, another objection is that curl is ordinarily meaningful only in a three-dimensional vector field. However, not only is your A defined only in 2D space, it is not a vector field but only a scalar field and the curl concept cannot be applied with it. If you want the curl of the gradient of A, that would always be identically zero.
>
> I think you should explain what you really have in mind so that we do not have to guess.
>
> Roger Stafford

---------------------------------------------------------------------

HI Roger, thanks for your reply. Ok you can see how I'm getting myself confused. Essentially, I want to create a magnetic field using the gradient of a greyscale image. So the algorithm would go:

1. Turn image into a matrix (2d)
2. Take image derivatives to find the edges (like a sobel or canny filter)
3. Get the signed distance matrix
4. Convolve the image derivatives with the inverse of the signed distance matrix. This should give me the B field.

Subject: Curl

From: Richard Crozier

Date: 21 Feb, 2012 12:30:12

Message: 4 of 7

"arron" wrote in message <jhvsac$388$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <jhut4q$4d4$1@newscl01ah.mathworks.com>...
> > "arron" wrote in message <jhuk27$8og$1@newscl01ah.mathworks.com>...
> > > Hi everyone. I am having trouble with the curl function in the following code:
> > >
> > > -------------------------------------------------------------------
> > > Iter = 10;
> > > I = zeros(x,y);
> > > sigma = 3;
> > > G = fspecial('gaussian', 5, sigma);
> > >
> > > for n = 1:Iter
> > > A = conv2(I, G, 'same');
> > > %B = curl of A.
> > > end
> > > -------------------------------------------------------------------
> > >
> > > So not too complicated...but I would just like to find the curl of A. Can anyone help? Thanks!
> > - - - - - - - - -
> > With 'I' set to all zeros, that would make A all zeros, so all its partial derivatives would also be zero.
> >
> > Assuming you don't really mean that, another objection is that curl is ordinarily meaningful only in a three-dimensional vector field. However, not only is your A defined only in 2D space, it is not a vector field but only a scalar field and the curl concept cannot be applied with it. If you want the curl of the gradient of A, that would always be identically zero.
> >
> > I think you should explain what you really have in mind so that we do not have to guess.
> >
> > Roger Stafford
>
> ---------------------------------------------------------------------
>
> HI Roger, thanks for your reply. Ok you can see how I'm getting myself confused. Essentially, I want to create a magnetic field using the gradient of a greyscale image. So the algorithm would go:
>
> 1. Turn image into a matrix (2d)
> 2. Take image derivatives to find the edges (like a sobel or canny filter)
> 3. Get the signed distance matrix
> 4. Convolve the image derivatives with the inverse of the signed distance matrix. This should give me the B field.

I understand what you are doing, as the previous poster said, you want the curl of A which is actually a 3D field. However, if A is constant along one dimension the curl can be found using something like:

% sample A over a grid
gridspace = 1;

% get suitible sample positions on an n x n grid
[xi,yi] = meshgrid(minx:gridspace:maxx, miny:gridspace:maxy);

% get z-directed A at every point on the grid somehow (up to you)

% A is constant in z so curl formula reduced to:
%
% curl A = [dA/dy] i + [-dA/dx] j + 0 k
% Get numerical gradients of A in x and y directions
[delAdelx, delAdely] = gradient(interpA, gridspace, gridspace);

Bx = delAdely;
By = -delAdelx;

You need to look at the actual definition of the curl function to understand why this is so.

Subject: Curl

From: Roger Stafford

Date: 21 Feb, 2012 19:04:11

Message: 5 of 7

"arron" wrote in message <jhvsac$388$1@newscl01ah.mathworks.com>...
> .... I want to create a magnetic field using the gradient of a greyscale image. .....
- - - - - - - - -
  As I stated in my previous posting, the curl of the gradient of a scalar field must be identically zero. This is true even in your two-dimensional situation. This is of course because the cross derivatives appearing in the expression for the curl must be equal: d^2F(x,y)/dx/dy = d^2F(x,y)/dy/dx.

Roger Stafford

Subject: Curl

From: Richard Crozier

Date: 21 Feb, 2012 22:35:10

Message: 6 of 7

"Roger Stafford" wrote in message <ji0prb$cpj$1@newscl01ah.mathworks.com>...
> "arron" wrote in message <jhvsac$388$1@newscl01ah.mathworks.com>...
> > .... I want to create a magnetic field using the gradient of a greyscale image. .....
> - - - - - - - - -
> As I stated in my previous posting, the curl of the gradient of a scalar field must be identically zero. This is true even in your two-dimensional situation. This is of course because the cross derivatives appearing in the expression for the curl must be equal: d^2F(x,y)/dx/dy = d^2F(x,y)/dy/dx.
>
> Roger Stafford

Just to clarify what I think the poster means, is that he can obtain from his image the magnetic vector potential. This is a 3D field, but in this case, we assume it is in an infinite plane in the 'into the page' direction, and the vectors also all point in this direction I will call z. The magnitude of the z directed vector field varies in the x and y directions, but not the z direction.

The magnetic field can then be found from B = curl A

In 3D (cartesian coordinates) this is given by:

[dAz/dy - dAy/dz] i + [dAx/dz - dAz/dx] j + [dAy/dx - dAx/dy] k

where i, j and k are unit vectors in x,y and z directions

but since A is constant in z
dAy/dz = 0
dAx/dz = 0

So the curl is given by

B = [dAz/dy] i + [-dAz/dx] j + [0] k

B is then a vector which varies in magnitude and direction in the x and y directions, but is zero in the z direction.

Subject: Curl

From: arron

Date: 22 Feb, 2012 10:12:12

Message: 7 of 7

"Richard Crozier" wrote in message <ji166u$qau$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <ji0prb$cpj$1@newscl01ah.mathworks.com>...
> > "arron" wrote in message <jhvsac$388$1@newscl01ah.mathworks.com>...
> > > .... I want to create a magnetic field using the gradient of a greyscale image. .....
> > - - - - - - - - -
> > As I stated in my previous posting, the curl of the gradient of a scalar field must be identically zero. This is true even in your two-dimensional situation. This is of course because the cross derivatives appearing in the expression for the curl must be equal: d^2F(x,y)/dx/dy = d^2F(x,y)/dy/dx.
> >
> > Roger Stafford
>
> Just to clarify what I think the poster means, is that he can obtain from his image the magnetic vector potential. This is a 3D field, but in this case, we assume it is in an infinite plane in the 'into the page' direction, and the vectors also all point in this direction I will call z. The magnitude of the z directed vector field varies in the x and y directions, but not the z direction.
>
> The magnetic field can then be found from B = curl A
>
> In 3D (cartesian coordinates) this is given by:
>
> [dAz/dy - dAy/dz] i + [dAx/dz - dAz/dx] j + [dAy/dx - dAx/dy] k
>
> where i, j and k are unit vectors in x,y and z directions
>
> but since A is constant in z
> dAy/dz = 0
> dAx/dz = 0
>
> So the curl is given by
>
> B = [dAz/dy] i + [-dAz/dx] j + [0] k
>
> B is then a vector which varies in magnitude and direction in the x and y directions, but is zero in the z direction.


Thank you both. Richard has pointed out exactly want I want to do. And if any of you are interested, you can calculate A by convolving the image derivatives with a signed distance function rather than having to calculate A with the magnetic vector potential equation.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us