MATLAB: Missing plot line and legend mismatch

legend mismatchplotting line not showing up

My coding is as follow and the result shown only 2 lines are plotted with the same colour. Also, the legend mismatched with the plotting line. May I know what are the things went wrong in this coding?
% Initial condition & parameters
u = 7.9e15; % Wbm
a = 6.90633e6; % m
w0 = 0.0011; % rad/s
im = 51.6*pi/180; % rad
t= [0:2000];
% Magnetic field
Bx = (u/a^3)*cos(w0*t)*sin(im);
By = -(u/a^3)*cos(im);
Bz = 2*(u/a^3)*sin(w0*t)*sin(im);
% subplot(3,2,3)
figure (3)
plot(t, Bx, t, By, t, Bz)
title('Graph of magnetic field vs time')
xlabel('Time,t(s)')
ylabel('Magnetic field, b (Tesla)')
legend('Bx','By','Bz')
gridfig.jpg

Best Answer

Note that ‘By’ is a scalar, so it actually will not appear on the plot (unless you plot it with a marker). They will all plot as they should if you multiply ‘By’ by a ones vector:
% subplot(3,2,3)
figure (3)
plot(t, Bx, t, By*ones(size(t)), t, Bz)
title('Graph of magnetic field vs time')
xlabel('Time,t(s)')
ylabel('Magnetic field, b (Tesla)')
legend('Bx','By','Bz')
That should do what you want.
Related Question