Matlab: help with biot savart law

In summary: In the first case, you would need to convert your magnitudes and angles into complex numbers and do the multiplication and then convert back to magnitudes and angles. And in the second case, you would need to use your trigonometry to add or subtract 90 degrees to your current angle and keep the magnitude as it is...Now, what are you doing in your first nested loop? You are assigning values to R(c,r)
  • #1
arronslacey
9
0
Hi, if anyone good with MATLAB and knows biot savart law then i hope you can help. I have the following program:

Code:
 clear all
 Img = imread('littlecircle.png');
 Img = Img(:,:,1);
 Img = double(Img);
 w = size(Img,1);               % width size
 h = size(Img,2);               % height size
 [Ix,Iy] = gradient(Img);       %gradient of image
 
 i=1;     %iteration for magnetic field loop
 b=0;     %initialize b to zero
 
 % Magnetic Field
 for pxRow = 1:h % fixed pixel row
 for pxCol = 1:w % fixed pixel column
 
 for r = 1:h % row of distant pixel
 for c = 1:w % column of distant pixel
 
 R(c,r) = sqrt((r-pxRow)^2 + (c-pxCol)^2);                               % pythagoras theorem to get distance to each pixel
[COLOR="Red"] O(c,r) = atan(Iy(c,r)./Ix(c,r));                         % direction of current
 If(c,r) = sqrt((Ix(c,r)).^2 + (Iy(c,r)).^2);                            % magnitude of current 
 Rxs(c,r) = R(c,r)./norm(R(c,r));                                        % unit vector from x to s                     
 b(c,r) = If(c,r).*O(c,r).*(Rxs(c,r)./(R(c,r)).^2);                      % b field = If(s)O(s) x Rxs/R.^2  BIOT SAVART LAW[/COLOR]Rxs/R.^2  BIOT SAVART LAW
 end
 end
 B(i) = {b}; % filling a cell array with results. read below
 i = i+1;
 end
 end

all lines in red are currently not calculating what I want them to. Can anyone help? Thanks!
 
Physics news on Phys.org
  • #2
by the way, here is the reference paper http://www.cs.swan.ac.uk/~csjason/papers/xxmm-pami2008.pdf
 
  • #3
Debugging hints/help:

They are not calculating what you want them to?
What ARE they calculating? and
what do YOU expect them to be?

Are you sure your starting numbers are correct (Ix,Iy) ?

What does the function 'norm' do for you?
You are taking the norm of scalar R(c,r)...what is that? Its length? Its magnitude? Is it always a positive number?
By the way, you just calculated R(c,r) above as the sqrt() of something...so, R(c,r) IS a scalar and IS positive; also, it is just a scalar...not a matrix.
So...what do you expect Rxs(c,r) = R(c,r)./norm(R(c,r)) to be? You call it a unit vector...but I don't think it is a vector at all...it is just a number.

It seems to me that Rxs(c,r) will always be 1.0 ?
 
  • #4
I am struggling with getting to grips with vectors in MATLAB i'll admit. but here's what i need to do:

1. read in an image
2. compute image gradient vectors [Ix,Iy], and then magnitude sqrt(Ix.^2+Iy.^2) = If(c,r)
3. compute the edge orientation of the image by rotating the gradient vectors 90 degrees clockwise or anti clockwise (O(c,r))
4. take each pixel in the image, and find its distance to all other pixels (R(c,r))
5. compute the unit vectors of the pixel distances Rxs(c,r)
6. Compute b field with Biot-Savart law B = If.*O cross (Rxs/R.^2)

and yes my understanding of how MATLAB handles vectors isn't helping the issue.
 
  • #5
I hear you...but you did not answer ANY of my questions. :-(
 
  • #6
sorry i'll have a go -

only R(c,r) calculates what I want. it calculates the distance one pixel to all others and stores into an array.

O(c,r) is supposed to be the current direction, ie taking the gradient vectors Ix(c,r) and Iy(c,r) and rotating them by 90 degrees. When I quiver plot these rotated vectors it looks as expected, however I am told that the actual result of these are just scalars, ie numbers - but they need to be vectors.

Rxs(c,r) is supposed to be the unit vector of R(c,r). Matlab computes unit vectors with the norm function:

Rxs(c,r) = R(c,r)/norm(R(c,r)) give me the unit vectors, and they are indeed all supposed = 1

Where I could be going wrong, as you say is that none of the above are vectors in which I have a problem. I am new to MATLAB and when you do your sums on the blackboard you take the nature of vectors for granted and just punch them in the calculator. but I'm having difficulty representing them here in matlab.

The clue "you are taking the norm of a scalar R(c,r)" as you say...well how do I turn R(c,r) into a vector? If i knew this I am sure the rest of the program would fall into place. I have been relying on matlabs quiver function (vector plot) to show me the direction of these numbers, and they do indeed have direction, hence why I'm assuming I'm returning vectors...argh!
 
  • #7
Yeap...you are struggling.

When on the blackboard you take the nature of vectors granted?
What do vectors look like?

Recall that a vector has a magnitude AND a direction (angle).

So, step back a bit and take a good look at your own code as if you had not written it...just because you had something in mind and had certain intent, that does not mean that it went through...take a look at your calculations and you will see that you are just doing a bunch of scalar operations...nothing there is a vector...

That is not necessarily bad, i.e., keeping track of your own magnitudes and your own angles separately is o.k...if you know that you are doing it!.

Other than that, you say that O(c,r) is a current direction (angle)? I guess it is, after all, it is the arctan() of something; so it is an angle, but just an angle, not a vector...are these the angles of your vectors? Where are the corresponding magnitudes? If()?

And Rxs() is not giving you unit vectors, it is just giving you units!



Alternatively...if you want to take vectors for granted in matlab, than you first need to find out how to handle vectors in matlab...really, vectors, you know, those things that do have magnitude and angle...can MATLAB handle this quantity in a single entity, variable? I think it might but I have not used MATLAB in about 20 years...so, take a few well invested minutes and do a google search or read the user's manual...I guess I could do it, too; but it is best if you learn this by yourself.
 

Related to Matlab: help with biot savart law

What is the Biot-Savart Law in Matlab?

The Biot-Savart Law is a mathematical equation used to calculate the magnetic field produced by a current-carrying wire or a moving charged particle in a given space. In Matlab, this law is used to simulate and analyze magnetic fields.

How do I implement the Biot-Savart Law in Matlab?

To implement the Biot-Savart Law in Matlab, you will need to define the current-carrying wire or moving charged particle as a vector or matrix, calculate the magnetic field at each point in space using the equation, and then visualize the results using plotting functions.

What are the inputs and outputs of the Biot-Savart Law in Matlab?

The inputs for the Biot-Savart Law in Matlab are the current-carrying wire or moving charged particle parameters, such as position, current, and velocity, as well as the position where you want to calculate the magnetic field. The output is the calculated magnetic field at each point in space.

Can the Biot-Savart Law be used for 3D simulations in Matlab?

Yes, the Biot-Savart Law can be used for 3D simulations in Matlab. You will need to define the current-carrying wire or moving charged particle as a 3D vector or matrix and calculate the magnetic field at each point in a 3D space using the equation.

Are there any limitations to using the Biot-Savart Law in Matlab?

Yes, there are some limitations to using the Biot-Savart Law in Matlab. This law assumes that the current-carrying wire or moving charged particle is infinitesimally small and that the magnetic field is constant throughout the space. Additionally, it does not take into account any external magnetic fields that may affect the results.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
138
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Introductory Physics Homework Help
Replies
6
Views
624
Replies
8
Views
706
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
3K
  • Introductory Physics Homework Help
Replies
3
Views
1K
  • Special and General Relativity
Replies
26
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
1
Views
6K
Back
Top