I want to write a fast MATLAB code where I need to write a for loop and I need to solve an ordinary differential equation each time.Is there any way to vectorize the code? Following is the part of the code:
tspan=0:0.01:20;
dw=rand(p,1);
M0=repmat([0 0 1],p,1)';
for p=1:ns
[t,M(:,:,p)]=ode45(@(t,M) testfun(t,M,dw(p)),tspan,M0(:,p));
end
where
function dM=testfun(t,M,w1)
M_x=M(1);
M_y=M(2);
M_z=M(3);
dM=[w1*M_y;-w1*M_x+w1*M_z-2*w1*M_y;-w1*M_y-(1-M_z)];