for loop - how to obtain a list of solutions

조회 수: 2 (최근 30일)
Sergio Orasch
Sergio Orasch 2020년 9월 9일
답변: Walter Roberson 2020년 9월 9일
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

채택된 답변

Walter Roberson
Walter Roberson 2020년 9월 9일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by