Function requires more input arguments to run?

19 次查看(过去 30 天)
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

采纳的回答

Adam
Adam 2015-11-25
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by