Finding the Velocity at Different intervals
23 ビュー (過去 30 日間)
古いコメントを表示
tryng to write a code to display the velocity and displacement given a time interval and acceleration at those intervals using integration. getting an error in my for loop what might be the reason
clear all
close all
% Acceleration Vector
a = [0 2 4 7 11 17 24 32 41 48 51];
% define time interval
k =1;
%define time vector
t = 0:k:10;
%initialize velocity
for ii = 1:11
v(ii) = trapz(a(1:ii),k);
end
% calculate the displacement d at each time interval
for jj = 1:11
d(jj) = trapz(v(1:jj),k);
end
% display a table of the velocity values
v = table(t,v,'VariableNames',{'Time(Sec)' 'Velocity(m/s)'})
% display a table of the displacement values
d = table(t',d','VariableNames',{'Time(Sec)' 'Displacement(m)'})
0 件のコメント
採用された回答
Star Strider
2019 年 8 月 24 日
You can solve the problem of varying numbers of elements in each iteration by using cell arrays for ‘v’ and ‘d’:
for ii = 1:11
v{ii} = trapz(a(1:ii),k);
end
% calculate the displacement d at each time interval
for jj = 1:11
d{jj} = trapz(v{jj},k);
end
The best solution would likely be to use the cumtrapz function instead, and avoid the loops entirely.
10 件のコメント
Star Strider
2019 年 8 月 25 日
As always, my pleasure.
I posted the correct code, and didn’t take a close look at yours. I just ran it to see if there were problems, and it ran without error.
その他の回答 (1 件)
Kudzanai Sekerere
2019 年 8 月 25 日
3 件のコメント
Star Strider
2019 年 8 月 26 日
The cumtrapz function is more efficient. I would use it and eliminate the loops.
参考
カテゴリ
Help Center および 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!