My loop returns imaginary results!

Illustration
tavneesh - 2020-12-15T12:49:55+00:00
Question: My loop returns imaginary results!

I write this simple code in a loop, and I obtain unwabted imaginary results. Could you please tell me where my mistake is:   SO = 0.5 .* ones(10,1); A(1,:) = 5.02 * (16.6 / 42) ^ 0.33 * SO(1,:) ^ 0.38; for i = 2 : 10 A(i,:) = 5.02 * ((16.6 - sum(A(1:i-1,:)))/ 42) ^ 0.33 * SO(i,:) ^ 0.38; end

Expert Answer

Profile picture of John Williams John Williams answered . 2025-11-20

Evaluate the part

 

((16.6 - sum(A(1:i-1,:)))/ 42)
and see what happens when the sum(...) is greater than 16.6
Note: No loop needed here sum(...) could be replaced by cumsum(...) and linear indexing is sufficient, subscript indexing is not needed here since it’s a vector.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!