use for loop to call the function 600 times
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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!
Respuestas (1)
Kevin Phung
el 4 de Mzo. de 2019
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 comentarios
Stephen23
el 5 de Mzo. de 2019
Editada: Stephen23
el 5 de Mzo. de 2019
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.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!