MATLAB: Using an index in a loop as a variable in another function

functionindexingMATLAB

function alpha = uppp2(x)
N = 100; d = 75; g = 5; h = 0.1;
for n = 1:x
alpha(1,n) = (x+1-n) * d;
end
for n = 1:N
alpha((n+1),1) = alpha(n,1) + g * h;
end
for j = 2:x
for n = 1:N % index n
alpha(n+1,j) = alpha(n,j) + h * vel((alpha(n,j-1)) - alpha(n,j))
end
end
end
function kappa = vel(y)
kappa(y >= d) = n * h * 25 / 6 % Here i want to use the same index n as in the foor loop
kappa(y == d) = 25
kappa(y <= d) = g - 1
end
% How do I achive this? It seems like the function Kappa doesn't find the variable n.

Best Answer

You need to provide it as an additional argument (the same holds for d, g, and h):
...
alpha(n+1,j) = alpha(n,j) + h * vel(n, d, g, h, (alpha(n,j-1)) - alpha(n,j))
...
function kappa = vel(n, d, g, h, y)
kappa(y >= d) = n * h * 25 / 6 % Here i want to use the same index n as in the foor loop
kappa(y == d) = 25
kappa(y <= d) = g - 1
end
Your 'vel' functions can't access n, d, g and h because they are only defined in uppp2.