Borrar filtros
Borrar filtros

use for loop to call the function 600 times

1 visualización (últimos 30 días)
william Smith
william Smith el 4 de Mzo. de 2019
Editada: Stephen23 el 5 de Mzo. de 2019
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 comentarios
william Smith
william Smith el 4 de Mzo. de 2019
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

Iniciar sesión para comentar.

Respuestas (1)

Kevin Phung
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
william Smith
william Smith el 5 de Mzo. de 2019
just to make sure no confusion there I changed the "i" you sent in the code to something else
Thanks again!
Stephen23
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.

Iniciar sesión para comentar.

Categorías

Más información sobre General Applications en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by