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

조회 수: 1 (최근 30일)
Oskar Mevik Päts
Oskar Mevik Päts 2019년 11월 27일
댓글: Oskar Mevik Päts 2019년 11월 27일
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.

채택된 답변

Johannes Fischer
Johannes Fischer 2019년 11월 27일
편집: Johannes Fischer 2019년 11월 27일
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
Oskar Mevik Päts
Oskar Mevik Päts 2019년 11월 27일
I see, that's the key. Thank you, this bugged me out. Muy appriciated!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by