MATLAB: MATLAB Data from loop not saved

MATLAB

Hi everybody, im actually making a little program wich must give me a graph.
Tens_stored = zeros(100,1);
Tens_S = 12;
Res_I = 10;
Res_E = logspace(0,2,100);
for B = logspace(0,2,100);
Tens = (B.*Tens_S)/(B+Res_I);
disp(Tens)
Tens_stored(B) = Tens;
semilogx(Res_E,Tens_stored)
end
But when i launch it, it make an error : Attempted to access Tens_stored(1.04762); index must be a positive integer or logical.
Error in TEST1 (line 8) Tens_stored(B) = Tens; And no one of the values calculated are in the array Tens_Stored. Can somebody help me plz 🙂 Thx

Best Answer

Hi!
You can not use doubles as indices. Do instead:
Res_E = logspace(0,2,100);
for B = 1:length(Res_E);
Tens = (Res_E(B).*Tens_S)/(Res_E(B)+Res_I);
disp(Tens)
Tens_stored(B) = Tens;
end
semilogx(Res_E,Tens_stored)
Plot after the loop! (Is it right that B and Res_E are the same?)