MATLAB: Undefined function ‘…’ for input arguments of type ‘double’ error AND error in function

ode

Using MATLAB R2016b, I'm trying to evaluate a 2nd order ODE with Euler's method in MATLAB inputting a system of 1st order ODEs. I've already verified that all my scripts are in the directory specifed by 'pwd' [which seems to be a remedy for the 'undefined function'], but still get the undefined function '…' for input arguments of type 'double' error and the function error. All help is much appreciated:
Error using feval
Undefined function 'Fexample1' for input arguments of type 'double'.
Error in euler (line 39)
y(i,:) = y(i-1,:) + h*feval(F,t(i-1),y(i-1,:));
For the following code:
function [y,t] = euler(F,y0,a,b,m)
if nargin < 4, a = 0; b = 1; end
if nargin < 3, a = 0; b = 1; end
if nargin < 2, error('invalid number of inputs'); end
t = linspace(a,b,m)';
h = t(2)-t(1);
n = length(y0);
y = zeros(m,n);
y(1,:) = y0;
for i=2:m
y(i,:) = y(i-1,:) + h*feval(F,t(i-1),y(i-1,:));
end
In a separate .m file in the same directory I have:
function F = Fexample1(t,y)
F1 = y(2);
F2 = e.^(-t)*sin(t)-2*y(2)-2*y(1);
F = [F1,F2];
And in a third .m file in the same directory as the two above, I have:
[Y,t] = euler('Fexample1',[0 0],0,1,11);
Ye = [(1./2).*e.^(-t).*(sin(t)-t.*cos(t)) (1./2).*e.^(-t).*((t-1).*sin(t)-t.*cos(t))]

Best Answer

The two statements:
I've already verified that all my scripts are in the directory specifed by 'pwd'
and
>> which Fexample1
'Fexample1' not found.
are a contradiction. Either the current folder is changed by a timer or GUI callback unexpectedly, or you've saved the file with a "l" (lowercase L) instead of "1".
Add the folder to the path (see addpath) instead of relying on it to be the current folder. Use function handles instead of strings to specifiy a function to be feval'ed.