Function requires more input arguments to run?

42 views (last 30 days)
Hi, I'm relatively new to matlab and trying to code a magnetic dipole field. Here is my code
if true
% code
function [Br,Btheta]=magfield(theta,rho,phi,Bo,Rs)
Bo=2000; %average magnetic field
Rs=2000; %radius of planet
Br=-2*Bo *(Rs/rho)^3 * cos(theta); %radial magnetic field dependence
Btheta=-Bo*(Rs/rho)^3 * sin(theta); %azimuthal magnetic field dependence
%[X,Y,Z]=sph2cart(theta, phi, rho);
end
However when I run this I get the error message "magfield requires more input arguments to run".
Does anyone have any idea why this might be occurring and how I can rectify it?
Thanks, John

Accepted Answer

Adam
Adam on 25 Nov 2015
theta = 7;
rho = 10;
phi = 100;
Bo = 2;
Rs = 8;
[Br,Btheta] = magfield(theta,rho,phi,Bo,Rs);
is how you should call your function (as an example - obviously use your own sensible input values). If you do not then you will get that error because your function expects 5 inputs.
If you try to run it like you would a script, e.g. by just clicking 'Run' in the editor then it will try to call it with no arguments and will crash as soon as it tries to access the first of those input arguments it expected.
In your case Bo and Rs don't look like they should be input arguments because you immediately overwrite them in the function. Unless your intention is to remove the hard-coded overwrites and pass them in instead, but haven't got around to that yet.

More Answers (0)

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!