Function requires more input arguments to run?

19 vues (au cours des 30 derniers jours)
John Draper
John Draper le 25 Nov 2015
Réponse apportée : Adam le 25 Nov 2015
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
  1 commentaire
the cyclist
the cyclist le 25 Nov 2015
How are you calling the function?

Connectez-vous pour commenter.

Réponse acceptée

Adam
Adam le 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.

Plus de réponses (0)

Catégories

En savoir plus sur Time Series Objects dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by