MATLAB: 2nd order differential question

MATLABmatlab mfile scriptode ode45 plotode45script

I am doing a project for my electrical engineering course. We have never used matlab; we are supposed to copy this code into matlab word for word, which i did, however i keep receiving the three errors shown below. Could anyone tell me why this is? the code typed into the matlab info is underneath this message and the m file is underneath the error messages.
Thanks!
in matlab window:
>> tspan=[0 2]; >> y0=[4,-5]; >> [t,y]=ode45('order2',tspan,y0); Error using feval Undefined function 'order2' for input arguments of type 'double'.
Error in odearguments (line 88) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 114) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, … ************************************ after the line where i had my error i was supposed to type:>>plot(t,y(:1)) this would be the end of the code ********************************* This is my m file
function dydt = order2(t,y) dydt = zero(size(y)); a=1.0; b=-2.0; r=0.0; dydt(1)=y(2); dydt(2)=r-a*y(2)-b*y(1);

Best Answer

The 'order2' reference is to the function ode45 integrates. Since it exists in a separate function file, you have to be sure the file order2.m exists in the path that MATLAB knows about. Putting it in the same directory the script containing your call to ode45 is in will work. In Windows, put order2.m in the Documents\MATLAB\ directory. That should solve your problem.
Related Question