MATLAB: Error: file must return a column vector, I have been using other threads for a fix.

errorMATLABode45

function Vdot = FEL_equationFifthYear(z,V);
global N p0 a0 phi0 Re ku K gamma0 rho;
x = cos(V(1:N,1) + V(3*N+2,1));
y = sin(V(1:N,1) + V(3*N+2,1));
Vdot = zeros(5,1);
Vdot(1:N,1) = V((N+1):2*N,1);
Vdot(N+1:2*N,1) = ((-2*V(3*N+1,1)*cos(V(1:N,1) + V(3*N+2,1))));
Vdot(2*N+1,3*N,1) = -((2/3)*Re*(((1/N)*sum(gamma0))*(1+rho*(1/N)*sum(V((N+1):2*N,1))))^2*ku^2*K^2);
Vdot(3*N+1,1) = (1/N)*sum(x);
Vdot(3*N+2,1) = -(1/V(3*N+1,1))*((1/N)*sum(y));
end
clear all;
global N p0 a0 phi0 rho gamma0 Re ku K loss0 loss1 pj aj phij%N = number of electrons,
N=50; %Number of electrons
p0=0; %Pj=P0=constant. Used 0 for ease of use but from worked equations Pj=5.67e7, is this 0 up to 5.67e7. working in jotter
a0=10^-3;%A(z=0)=A0<<1, so 0.0001 was chosen
phi0 = 0;
rho = 0;
gamma0 = 0;
Re = 2.818e-15;
ku = 1;
K = 3.71;
loss0 = -(((2/3)*Re*(((1/N)*sum(gamma0))*(1+rho*((1/N)*sum(p0))))^2*ku^2*K^2));
%V0 = [1:3*N+2]'; %Vector V from 1 to 3*N+2, apostrophe used to invert the matrice. This shows the complete vector V
V0 = zeros(5,1);
V0(1:N,1) = linspace(0,((N-1)/N)*2*pi,N); %vector V from 1:N evenly spaced out between points X1 and X2 with N number of points
V0((N+1):2*N,1) = p0; %Vector V from point N+1 to 2N is equal to p0
V0((2*N+1):3*N,1) = loss0;
V0(3*N+1,1) = a0;
V0(3*N+2,1) = phi0;
zspan = [0 200];
z0=0;
options=odeset();
[z,V] = ode45('FEL_equationFifthYear',zspan,V0,options);
semilogy(z, V(:,3*N+1).^2);
As you can see I have tried both Vdot = zeros(5,1) and to the inverse but I am still receiving the error: Error using odearguments (line 93) FEL_EQUATIONFIFTHYEAR must return a column vector.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in FifthYearThirdAttempt (line 27)
[z,V] = ode45('FEL_equationFifthYear',zspan,V0,options);
any ideas?

Best Answer

You have
Vdot(2*N+1,3*N,1) = -((2/3)*Re*(((1/N)*sum(gamma0))*(1+rho*(1/N)*sum(V((N+1):2*N,1))))^2*ku^2*K^2);
You might have meant
Vdot(2*N+1:3*N,1) = -((2/3)*Re*(((1/N)*sum(gamma0))*(1+rho*(1/N)*sum(V((N+1):2*N,1))))^2*ku^2*K^2);