use for loop to call the function 600 times

1 回表示 (過去 30 日間)
william Smith
william Smith 2019 年 3 月 4 日
編集済み: Stephen23 2019 年 3 月 5 日
function[v_a, x_direction, y_direction, v_b, t_flight]=func(friction, initalelevation)
friction= 0.1
initial elevation= 100
looking to run the function 600 times using for loop
Please help!
Thanks!
  4 件のコメント
william Smith
william Smith 2019 年 3 月 4 日
not really, this question need to run the function 600 times with only two inputs of loss=0.1 and initial elevation=100, where as for the other question need to run the function once for each of the different inputs given

サインインしてコメントする。

回答 (1 件)

Kevin Phung
Kevin Phung 2019 年 3 月 4 日
friction= 0.1 : 0.05 : 0.65
initial_elevation= 100 : 15 : 200
rows = numel(friction);
col = numel(initial_elevation);
v_a = zeros(rows,col);
x_direction = zeros(rows,col);
y_direction = zeros(rows,col);
v_b = zeros(rows,col);
t_flight = zeros(rows,col);
for i = 1:numel(friction)
for j = 1:numel(initial_elevation)
[v_a(i,j), x_direction(i,j), y_direction(i,j), v_b(i,j), t_flight(i,j)] = func(friction(i), initial_elevation(j))
end
end
let me know if this is what you wanted. each parameter will be a matrix where each row is a calculation done with a different friction, and each column represents a different elevation.
  5 件のコメント
william Smith
william Smith 2019 年 3 月 5 日
just to make sure no confusion there I changed the "i" you sent in the code to something else
Thanks again!
Stephen23
Stephen23 2019 年 3 月 5 日
編集済み: Stephen23 2019 年 3 月 5 日
You need to read the documentation for every operator that you use, no matter how trivial you think it is. For example, this line
while x_direction(i)==0:500 && y_direction(i)==0:-134
is unlikely to do what you probably want (I guess you want to iterate over those values). The while documentation states "An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false", and the == eq operator performs an element-wise equivalence of your scalar value x_direction(i) with the vector 0:500, which means that your while loop will never run (at most one element returned by == will be true, so the condition will never be fulfilled).
And the && should throw an error with those non-scalar inputs anyway...
Read the documentation. Test each line until you are certain that is does exactly what you need.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGeneral Applications についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by