MATLAB Answers

Array indices must be positive integers or logical values

4 views (last 30 days)
So I am trying to create an easy function to calculate an equation for all the values between 0 and 90 degrees, in order to create a plot of the Earth dipole field for different altitudes, but I keep getting the same error message:
Array indices must be positive integers or logical values.
I have tried different approaches, but I am not able to make it work. If I don't store the values for the plot, the loop runs and gives me the correct values, but the moment I try to store them to plot them all together, it gives me the error. Can somebody help?
%% MAGNETORQUER SIZING
clear all; clc;
%% Magnitude of the Dipole Field:
nu0 = 1.256637*10^-6; % [H/m] Permeability of free space
m = 8*10^22; % [A m2] Earth's magnetic dipole moment
h = 400; % Altitude in [km]
r = 6378 + h; % Orbit radius in [km]
rm = r*1000; % Scaling to [m]
for i = 0:0.1:90
B = (nu0/(4*pi))*(m/rm^3)*sqrt(1+3*sind(i)^2);
Bplot(i) = B;
end
figure(1);
grid on; hold on;
plot(B,i,'b-','Displayname','Magnitude of the dipole field');

  0 Comments

Sign in to comment.

Accepted Answer

Alex Mcaulley
Alex Mcaulley on 12 Aug 2019
You are trying to acces to: B(0), B(0.1) and so on, which are not allowed indexes. Try with this:
%% MAGNETORQUER SIZING
clear all; clc;
%% Magnitude of the Dipole Field:
nu0 = 1.256637*10^-6; % [H/m] Permeability of free space
m = 8*10^22; % [A m2] Earth's magnetic dipole moment
h = 400;
r = 6378 + h;
rm = r*1000;
i = 0:0.1:90
Bplot = (nu0/(4*pi))*(m/rm^3)*sqrt(1+3*sind(i).^2);
figure(1);
grid on; hold on;
plot(Bplot,i,'b-','Displayname','Magnitude of the dipole field');

  1 Comment

Sign in to comment.

More Answers (0)

Sign in to answer this question.