Function requires more input arguments to run?

조회 수: 39 (최근 30일)
John Draper
John Draper 2015년 11월 25일
답변: Adam 2015년 11월 25일
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에서 Particle & Nuclear Physics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by