MATLAB: Error as Function definitions are not permitted in this context.

function definitions are not permitted in this context.

Hi all,
I am getting error as Function definitions are not permitted in this context.
My code:
function dydt = odefcn(t,y,A,B)
dydt = zeros(2,1);
dydt(1) = y(2);
dydt(2) = (A/B)*t*y(1);
A= 1;
B= 2;
tspan=[0 5];
y0 = [0 0.01];
[t,y]= ode15s(@(t,y) odefcn(t,y,A,B), tspan, y0);
plot(t,y(:,1),'-o',t,y(:,2),'-.' )
Could you please help me.

Best Answer

You have two options:
function Main
A= 1;
B= 2;
tspan=[0 5];
y0 = [0 0.01];
[t,y]= ode15s(@(t,y) odefcn(t,y,A,B), tspan, y0);
plot(t,y(:,1),'-o',t,y(:,2),'-.' )
end
function dydt = odefcn(t,y,A,B)
dydt = zeros(2,1);
dydt(1) = y(2);
dydt(2) = (A/B)*t*y(1);
end
Save the above lines in the m file and name it Main.m and run.
Or save the odefcn function alone in as function in file odefcn.m and call it file.