MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by fang-chu
on 12 Jul 2013

%clc clear %parameters dt = 1e-6; B1 = 10; phi = 0; gamma = 267.513*10^6; %rad/(s*T) Bzi = -5; %in HZ Bzj = 5; %in HZ a = 1000000; G = 42.576; % Gyromagnetic ratio (MHz/T) MHZ=1*10^6 B_zi = Bzi/(a*G); %in tesla B_zj = Bzj/(a*G); %in tesla % mag_initial = [0 0 1; 0 0 1];

%functions % equation magnetizaion = [m_x1 m_y1 m_z1; m_x2 m_y2 m_z2]; mag = zeros(2,3); B = zeros(size(mag));

% line up mag(:,3) = 1;

B(1,3) = B_zi; B(2,3) = B_zj;

%time evolution for nn = (1:10000) % calculate B M = mean(mag); %average of mag m_x = M(1); m_y = M(2); euler = complex(cos(phi),sin(phi));

m = complex(m_x,m_y); B0 = euler*m; B_x = real(B0); B_y = imag(B0); B = [B_x B_y B_zi; B_x B_y B_zj]; % equation B = [B_x+B1 B_y B_zi; B_x+B1 B_y B_zj]; [tt, mm] = ode45('bloch',[0:dt/2:dt],mag,[],gamma,B); mag = mm(:,3); end

my function is function dmdt = bloch(tspan,mag,spot,gamma,B) %outputs and inputs mag = reshape(mag,2,3); dmdt = gamma*cross(mag,B); dmdt = dmdt(:); return

the error I got

Attempted to access M(2); index out of bounds because numel(M)=1.

Error in main5 (line 32) m_y = M(2);

Error in run (line 57) evalin('caller', [s ';']);

*No products are associated with this question.*

Answer by Image Analyst
on 12 Jul 2013

Looks like it should work. In fact it does for me. So I think your mean is not what you think it is. After you call mean(), put these lines:

which -all mean whos M

What do you see?

fang-chu
on 13 Jul 2013

Name Size Bytes Class Attributes

M 1x3 24 double

Name Size Bytes Class Attributes

M 1x1 8 double

Attempted to access M(2); index out of bounds because numel(M)=1.

Error in main5 (line 34) m_y = M(2);

this is what show after I run it

Do I have to add "if" comment? what do you mean my mean is not what I think it is?

## 0 Comments