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

1 Ansicht (letzte 30 Tage)
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.

Akzeptierte Antwort

Johannes Fischer
Johannes Fischer am 27 Nov. 2019
Bearbeitet: Johannes Fischer am 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.

Weitere Antworten (0)

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by