for loop - how to obtain a list of solutions

3 vues (au cours des 30 derniers jours)
Sergio Orasch
Sergio Orasch le 9 Sep 2020
hello,
I would like to show you my problem in MATLAB. In order to solve an equation with an variable input, I created a for loop in order to obtain a list with all solutions. The for loop should run n times. Everytime it starts s0_y should be changed (add +100 to the current value). The output v_safe should be safed in a seperate list for further calculations. How do I run the loop n times? How do I increase s0_y by 100? How do I save v_safe in a seperate list? Thank you very much for any support.
If I run the program I get following error code.
Index exceeds the number of array elements (1).
s0_y = y_k(k)
Command Window results:
n = 7
s0_y = 1000
s0_y = 1000
s0_y = 1100
s0_y = 1100
s0_y = 1300
s0_y = 1300
s0_y = 1600
s0_y = 1600
code:
v0_x = 100; %m/s
s0_x = -5000; %m
a_x = 0; %m/s^2
syms v0_y;
s0_y = 1000;
a_y = 0; %m/s^2
h_B = 1500; %m
S_H = 100; %m
h_BS = h_B+S_H;
n = length(s0_y:100:h_BS)
for k = 1:n
y_k = [s0_y:100:h_BS];
s0_y = y_k(k)
s0_y
syms t;
s_x = s0_x+v0_x*t+(a_x*t*t)/2 == 0;
t_col = solve(s_x,t);
s_sol = s0_y+v0_y*t_col+(a_y*t_col*t_col)/2-h_BS;
v_safe = solve(s_sol,v0_y);
end

Réponse acceptée

Walter Roberson
Walter Roberson le 9 Sep 2020
y_k = [s0_y:100:h_BS];
for k = 1:n
s0_y = y_k(k)
s0_y
syms t;
s_x = s0_x+v0_x*t+(a_x*t*t)/2 == 0;
t_col = solve(s_x,t);
s_sol = s0_y+v0_y*t_col+(a_y*t_col*t_col)/2-h_BS;
v_safe(k) = solve(s_sol,v0_y);
end

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by