use for loop to call the function 600 times

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 Comments

do you mean to have the function inside a for loop, and obtaining outputs 600 times? Or are you trying to have a for loop inside this function? This is a huge difference.
You need to provide more information.. like what is the function supposed to do?
looking to use a for loop to call the function 600 times using the same values for the inputs given above,
this is a projectile motion function.
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

Sign in to comment.

Answers (1)

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 Comments

Thank you, getting an error with "undefined variable or function 'friction'" even though it is defined
Thanks again
post your function
function[v_a, x_direction, y_direction, v_b, t_flight]=func(friction, initialheight)
m=80;
g=9.81;
v_a=sqrt(2*(m*g*initialheight+friction)/(m));
x_direction=0; %initial condition
y_direction=0; %initial condition
v_b=0; %initial condition
t_flight=0; %initial condition
i=1;
dt=0.01;
v_x=initialheight;
v_y=-g*dt;
v_air=5*randn(1);
v_xfinal= v_x+v_air;
y_slope=(v_y)/(v_xfinal);
while x_direction(i)==0:500 && y_direction(i)==0:-134
v_x(i)=initialheight;
v_y(i)=-g*dt;
v_a(i)=sum(v_x(i))+sum(v_y(i));
x_direction(i+1)=x_direction(i)+x_direction(i-1)*dt;
y_direction(i+1)=y_direction(i)+y_direction(i-1)*dt;
y_slope(i+1)=(v_y(i))/(v_xfinal(i-1));
t(i+1,1) = t(i,1) + dt;
i=i+1;
end
end
just to make sure no confusion there I changed the "i" you sent in the code to something else
Thanks again!
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Tags

Asked:

on 4 Mar 2019

Edited:

on 5 Mar 2019

Community Treasure Hunt

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

Start Hunting!