MATLAB How can I improve my MATLAB program for the Biot Savart Law?

AI Thread Summary
The discussion revolves around a MATLAB program intended to implement the Biot-Savart law for calculating magnetic fields from an image. The user is seeking assistance with specific lines of code that are not producing the desired calculations. The program processes an image, computes gradients, and attempts to calculate the magnetic field based on pixel distances and current directions. Key issues highlighted include the calculation of distance, direction, and magnitude of current, as well as the application of the Biot-Savart law. A reference paper is provided for further context on the methodology. The user is looking for guidance to correct the problematic lines of code.
arronslacey
Messages
8
Reaction score
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
 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 LAWRxs/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
by the way, here is the reference paper http://www.cs.swan.ac.uk/~csjason/papers/xxmm-pami2008.pdf
 
Back
Top