1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab issue

  1. Jul 17, 2009 #1
    % 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


    % initialize variables

    % perform calculation
    for j=1:Ntest
    for i=1:df:360
    Rsuv=[xL yL 0]/a;
    dLuv=cross([0 0 1],Rsuv);
    R=[x-xL -yL 0];

    % generate plot
    grid on

    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

    Error in ==> test at 43

    how can i solve this anyone???
  2. jcsd
  3. Jul 17, 2009 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Well for a start you haven't defined the function magvector, so that probably explains your error of an undefined function.
  4. Jul 17, 2009 #3
    well i rewrited this code from the book in which there is an figure of solution due to this code...i also found similiar 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...
  5. Jul 17, 2009 #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)



    obviously matlab does not have a built in command for magvector

    Attached Files:

  6. Jul 17, 2009 #5


    User Avatar
    Science Advisor
    Homework Helper

    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:


    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: Jul 17, 2009
  7. Jul 17, 2009 #6
    thanks a lot...this was very helpfull! :D
  8. Jul 18, 2009 #7


    User Avatar
    Science Advisor
    Homework Helper

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook