MATLAB Data from loop not saved

조회 수: 1 (최근 30일)
Patrick Allaglo
Patrick Allaglo 2013년 12월 6일
편집: Patrick Allaglo 2013년 12월 6일
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

채택된 답변

Simon
Simon 2013년 12월 6일
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?)
  댓글 수: 1
Patrick Allaglo
Patrick Allaglo 2013년 12월 6일
Hi thx for your answer, i just understood the mistake i made... Thx again

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Wayne King
Wayne King 2013년 12월 6일
편집: Wayne King 2013년 12월 6일
You can't have a for loop where the index is non-integers, like this
B = logspace(0,2,100);
because the 2nd element of B is already a non-integer value and you can't access a non-integer element of an array. Think about it. Does it make sense to ask what is the 2.5-th element of a finite-dimensional vector?
Why would you want to space filled elements of a vector logarithmically. even if you could do it with integer values? You would end up with zeros in your vector. For example, if you did
y(1) = 2;
y(3) = 1;
Note that MATLAB makes y a length-3 vector and puts a zero in for the 2nd element.
I'm not sure what you're trying to do here, but maybe something like:
Tens_stored = zeros(100,1);
Tens_S = 12;
Res_I = 10;
Res_E = logspace(0,2,100);
for nn = 1:100
Tens = (Res_E.*Tens_S)/(B+Res_I);
Tens_stored(nn) = Tens;
end
Although the above just gives the same value for Tens_stored at each element of the array, so I'm sure you probably don't want that.
  댓글 수: 1
Patrick Allaglo
Patrick Allaglo 2013년 12월 6일
편집: Patrick Allaglo 2013년 12월 6일
Hi , thx for answering im just beginning Matlab so im a little bit lost. I understand what you tried to say. Im trying to make a program which calculate the real voltage of an electric circuit with 100 of charge values evolving logarithmically between 1 and 100 ohms. :) thx

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by