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

 采纳的回答

KSSV
KSSV 2020-8-12
编辑:KSSV 2020-8-12
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 个评论

HI KSSV
Thanks for your heap I tried your code but it kept coming up with an error
Error: File: untitled3.m Line: 16 Column: 52
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
There is a typo error....edited the answer. There was one extra paranthesis '('
Thank you so much that was a great help

请先登录,再进行评论。

更多回答(1 个)

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 个评论

HI Akira
Thanks for that Sadly one of the requirements for my code is that it must be a for loop.

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by