use for loop to call the function 600 times

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by