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

1 vue (au cours des 30 derniers jours)
Oskar Mevik Päts
Oskar Mevik Päts le 27 Nov 2019
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.

Réponse acceptée

Johannes Fischer
Johannes Fischer le 27 Nov 2019
Modifié(e) : Johannes Fischer le 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.
  1 commentaire
Oskar Mevik Päts
Oskar Mevik Päts le 27 Nov 2019
I see, that's the key. Thank you, this bugged me out. Muy appriciated!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by