MATLAB: Hi isnt do I get this error ‘ operator ‘*’ is not supported for operands of type struct’

for loopMATLAB

>> %script needed to find distance %given, Time.In = 0.7; rho =1.5; A =1; m = 1700; V.fstream = 96.639; a = 4.1202; v = 0; T = 7004.34; t.initial = 0; %use for loop to find for K = 1 : 50 v(K + 1) = v(K) + a(K) * Time.In; T(K + 1) = 1/2 * rho * A * (V.fstream ^ 2 – v(K) .^2); a(K + 1) = T(K) / m; b(K + 1) = v(K) * Time.In; t(K + 1) = t(K) * Time.In; end % the distance using the sum command distance = sum(b , 'all' )

Best Answer

The problem is with the line
t(K + 1) = t(K) * Time.In;
I have no idea what you're trying to even do here. Both t and Time are structs.
I don't know why you would even be trying to use structs for any of these variables. Do you mean to be using structs at all, or are you just using the . as a space?
TimeIn = 0.7;
rho =1.5;
A =1;
m = 1700;
Vfstream = 96.639;
a = 4.1202;
v = 0;
T = 7004.34;
t=0;
for K = 1 : 50
v(K + 1) = v(K) + a(K) * TimeIn;
T(K + 1) = 1/2 * rho * A * (Vfstream ^ 2 - v(K) .^2);
a(K + 1) = T(K) / m;
b(K + 1) = v(K) * TimeIn;
t(K + 1) = t(K) * TimeIn;
end
% the 'all' option is too recent for my version
distance = sum(b(:))
This at least gives an answer. Of course, I have no idea if it's correct. I've done no analysis of the math or anything.
Assuming that's right, since the loop is a fixed length, it would be good practice to preallocate the vectors instead of growing them like this.
Related Question