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 个评论
回答(1 个)
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 个评论
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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!