Hi I need help with for loop

7 Ansichten (letzte 30 Tage)
Benedict Comerford
Benedict Comerford am 12 Aug. 2020
Hi I'm very new to matlab and was wondering if there was a way to code all of this within just one for loop

Akzeptierte Antwort

KSSV
KSSV am 12 Aug. 2020
Bearbeitet: KSSV am 12 Aug. 2020
This single loop is fine enough:
N = length(v_x) ;
xpos = zeros(N-1,1) ;
xpos(1) = 4 ;
for i = 2:N-1
xpos(i) = v_x(i)*(t(i)-t(i-1))+xpos(i-1) ;
endfor
  3 Kommentare
KSSV
KSSV am 12 Aug. 2020
There is a typo error....edited the answer. There was one extra paranthesis '('
Benedict Comerford
Benedict Comerford am 12 Aug. 2020
Thank you so much that was a great help

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Akira Agata
Akira Agata am 12 Aug. 2020
No need to use for-loop. How about the following way?
% Read data file
T1 = readtable('A1_input.txt');
% Postion of (x,y) at time = 0
x0 = 4;
y0 = 0;
% Calculate the position for each time step
xPos = cumtrapz(T1.time, T1.vx) + x0;
yPos = cumtrapz(T1.time, T1.vx) + y0;
  1 Kommentar
Benedict Comerford
Benedict Comerford am 12 Aug. 2020
HI Akira
Thanks for that Sadly one of the requirements for my code is that it must be a for loop.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by