MATLAB: I want to link the below two functions..hope somebody can help ..

6/3

function [u,u_exact,x,t] = lax_wendroff(f,t_0,t_f,M,N)
% define the mesh in space
dx = 2*pi/N;
x = 0:dx:2*pi;
x = x';
% define the mesh in time
dt = (t_f-t_0)/M;
t = t_0:dt:t_f;
c = 1/2;
% display('this number should be between -1 and 1 for stability:')
mu = c*dt/dx;
% choose the wave number of the initial data and give its decay rate
u= zeros(N+1,M+1);
u(:,1) = f(x);
u_exact(:,1) = u(:,1);
% I want to do the Lax Wendroff scheme:
%



% u_new(j) = u_old(j) - (mu/2)*(u_old(j+1)-u_old(j-1))
% + (mu^2/2)*(u_old(j-1)-2*u_old(j)+u_old(j+1))
for j=1:M
for k=2:N
u(k,j+1) = u(k,j) - (mu/2)*(u(k+1,j)-u(k-1,j)) + (mu^2/2)*(u(k+1,j)-2*u(k,j)+u(k-1,j));
end
% I code in the exact values at the endpoints.
u(1,j+1)=1;
u(N+1,j+1)=0;
X = x-c*t(j+1);
u_exact(:,j+1) = f(X);
end
end
function y = f(x)
% for step function initial data:
y = x<=(pi-1);
end
% [u,err,x,t] = lax_wendroff(@f,t_0,t_f,M,N)
%
% solves the heat equation u_t + u_x = 0 [0,2*pi]
%
% If I'm going to use the FFT to analyse the solution then I'd like to take
% N of the form 2^k. This will also have the advantage of making sure there's
% always a meshpoint at x = pi
%
% f.m is the function that contains the initial data

Best Answer

Your call
lax_wendroff(f(2),1,2,20,20)
is invoking the function f with the argument 2, and passing that numeric result as the first argument to lax_wendroff() .
function [u,u_exact,x,t] = lax_wendroff(f,t_0,t_f,M,N)
Here we can see that the first argument to lax_wendroff is to be known within the routine under the name "f". A numeric value was passed in to the code, so inside the call, "f" will be a numeric result.
u(:,1) = f(x);
Here, the numeric value in "f" is being indexed by "x". When "x" is not a positive integer, or a logical, that indexing will fail with the error message you received.
Now look in your own comments:
% [u,err,x,t] = lax_wendroff(@f,t_0,t_f,M,N)
This indicates that you should be passing in a handle to f, not the numeric value that results from f(2). I do not know what you intended with the 2, but possibly you wanted
lax_wendroff(@f,1,2,20,20)
Related Question