Calculating Magnetic Field of Current Loop in Matlab

  • MATLAB
  • Thread starter jadelsky
  • Start date
  • Tags
    Matlab
In summary, the author of the macro wrote a function to compute the magnitude of a three dimensional vector, but when the user attempted to use the function, they received an error because the "norm" command already computed the magnitude for that dimension.
  • #1
jadelsky
13
0
% varibles
% I current(A) in +phi direction on ring
% a ring radius (m)
% Ndeg number of increments for phi
% f angle of phi in radians
% df differential change in phi
% dL differential length vector on the ring
% dLmag magnitude of dL
% dLuv unit vector in direction of dL
% [xL,yL,0] location of source point
% Ntest number of test points
% Rsuv unit vector from origin to source point
% R vector from source to test point
% Ruv unit vector for R
% Rmag magnitude of R
% dH differential portion of H
% dHmag magnitude pf dH
% radius radial distance from origin
% Hz total magnetic field at test point

clc
clear

% initialize variables
a=1;
I=1;
Ndeg=90;
Ntest=40;
df=360/Ndeg;
dLmag=(df*pi/180)*a;

% perform calculation
for j=1:Ntest
x=(j-1)*a/Ntest;
for i=1:df:360
f=i*pi/180;
xL=a*cos(f);
yL=a*sin(f);
Rsuv=[xL yL 0]/a;
dLuv=cross([0 0 1],Rsuv);
dL=dLmag*dLuv;
R=[x-xL -yL 0];
Rmag=magvector(R);
Ruv=R/Rmag;
dH=I*cross(dL,Ruv)/(4*pi*Rmag^2);
dHmag(i)=magvector(dH);
end
radius(j)=x;
Hz(j)=sum(dHmag);
end

% generate plot
plot(radius,Hz)
grid on
xlabel('radius(m)')
ylabel('Hz(A/m)')

well this shoul calculate the magnetic field of current loop, but i get this error:

? Undefined function or method 'magvector' for input arguments of type
'double'.

Error in ==> test at 43
Rmag=magvector(R);

how can i solve this anyone?
 
Physics news on Phys.org
  • #2
Well for a start you haven't defined the function magvector, so that probably explains your error of an undefined function.
 
  • #3
well i rewrited this code from the book in which there is an figure of solution due to this code...i also found similar code on the net and again the same problem...i'm not good in MATLAB but i assume that magvector should be some function and that i don't have to define it but MATLAB doesn't recognize it...
 
  • #4
well if its the magnitude of R, you just say

Rmag = sqrt(R(1)^2+R(2)^2+R(3)^2) or

Rmag = sqrt(-0.999847695156391^2+-0.0174524064372835^2+0^2)

and

dHmag(i)=sqrt(dH(1)^2+dH(2)^2+dH(3)^2);

obviously MATLAB does not have a built in command for magvector
 

Attachments

  • untitled.jpg
    untitled.jpg
    26.5 KB · Views: 382
  • #5
Is this a stand alone function or is it designed to be used as part of a larger task?

Normally, the first line defines what variables will input from the outside and what variables will be output to the outside.

For example, in one of my functions, the first line established that my function would input 9 variables from outside, process them, and output 3 new variables (except most of my variables were actually strings of data in matrix format).

function [bit_errs_ss,i_out,q_out]=despread(tiq,sumiq,cs_t,sn_t,seq1,seq2,data,N,fcarr)

--------------------------------
Or, conversely, yours is the master program, but you haven't actually written the magvector(R) function yet. Write the magvector function and your program will run. Presumably, the 'R' matrix is passed to the magvector function and the magvector function calculates the magnitude and passes it back to your main program.

In my main function (or program) there's a line that is practically identical to the first line of my function:

[bit_errs_ss,i_out,q_out]=despread(tiq,sumiq,cs_t,sn_t,seq1,seq2,data,N,fcarr)

This line calls the "despread" function. Technically, the variable names don't have to match, as long the number of variables out and the number of variables in are identical. That's pretty handy if you've written a function that you'll wind up using over and over; in a lot of different processes.

For example, calculating the magnitude of a three-dimensional vector is a pretty common task that could come in handy for any number of things besides just magnetic fields (having magnitude and magnetic share the first 3 letters probably adds a little confusion in your situation).

In my example, the main reason for making the routine modular was to make debugging a lot more manageable. This program took a long time to run from start to finish if I ran every module.
 
Last edited:
  • #6
thanks a lot...this was very helpfull! :D
 
  • #7
A function that computed the magnitude of a three dimensional vector would be even more useful if Matlab's "norm(x)" command didn't already compute the magnitude of any dimension vector.

The idea of writing your own function to compute the magnitude is either to give you practice with Matlab or the original author of the macro didn't know about the norm command.
 

Related to Calculating Magnetic Field of Current Loop in Matlab

1. What is Matlab issue?

Matlab issue refers to any problem or error encountered while using the Matlab software. It can range from installation errors to coding errors or compatibility issues with other software.

2. How can I troubleshoot a Matlab issue?

To troubleshoot a Matlab issue, you can start by checking the error message for any specific details. You can also try restarting the software or your computer, updating Matlab to the latest version, or checking for any conflicting software that may be causing the issue.

3. How do I report a Matlab issue?

If you encounter a Matlab issue, you can report it to the Mathworks support team by submitting a support request on their website or by contacting their technical support hotline. You can also post your issue on the Matlab community forum to get help from other users.

4. Can I fix a Matlab issue on my own?

It depends on the type and severity of the issue. Some Matlab issues can be easily fixed by following troubleshooting steps or updating the software. However, more complex issues may require assistance from the Matlab support team or other experienced Matlab users.

5. How can I prevent future Matlab issues?

To prevent future Matlab issues, it is important to regularly update the software to the latest version, avoid using outdated or unsupported functions, and make sure your computer meets the minimum system requirements. It is also helpful to follow best practices and properly document your code to avoid errors.

Similar threads

  • Introductory Physics Homework Help
Replies
4
Views
391
  • Introductory Physics Homework Help
Replies
7
Views
307
  • Introductory Physics Homework Help
Replies
12
Views
264
  • Introductory Physics Homework Help
Replies
17
Views
452
  • Introductory Physics Homework Help
Replies
1
Views
623
  • Differential Geometry
Replies
21
Views
718
  • Advanced Physics Homework Help
Replies
5
Views
1K
  • Introductory Physics Homework Help
Replies
6
Views
1K
  • Electromagnetism
Replies
5
Views
1K
Replies
2
Views
171
Back
Top