Unable to perform assignment because the left and right sides have a different number of elements.

1 次查看(过去 30 天)
I am trying to calculate all values of velocity and displacement for the times between 0 and 20 in increments of 0.01. Im struggling to add on the last value of velocity and displacement as i need to be able to use the previous iteration values where i have tried doing this using the variable 'i'. I think the problem is due to the different sizes of matrices. Im quite new to coding so any help would be appreciated, thanks.

回答(1 个)

Walter Roberson
Walter Roberson 2021-1-20
dt = 0.01;
i0 = 1;
i(1) = i0;
i = (1:dt:21);
Note that overwrites all of i
It is not recommended to use i as a variable name, as it can lead to confusion because i is used as sqrt(-1)
i will be a vector of length 2001 I think.
for t = [0:0.01:20.0]
v(i+1) = a*0.01 + a(i)*t;
You ask to index a(i) but a appears to be a scalar at that point, and i contains non-integer values that are not valid as indices.
You also multiply all of a by 0.01 so if a is a non-scalar (due to code you did not show us) then it might not be the same size as a(i) .
If somehow (that is, if the code you posted is not the code that generated the error) i were a scalar integer at that point but a were a vector, then v(i+1) would hypothetically designate a scalar, but a*0.01 would be a vector and you would be trying to store a vector result into a scalar field.
... But the code you posted would have failed before that, failing when trying to index a at the non-integer i
  3 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by