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

1 view (last 30 days)
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.

Accepted Answer

Johannes Fischer
Johannes Fischer on 27 Nov 2019
Edited: Johannes Fischer on 27 Nov 2019
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.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!