Infinite number of inputs and outputs in lopp
    조회 수: 3 (최근 30일)
  
       이전 댓글 표시
    
R = 150;
C = 20.*10^-3;
t = [0; 1; 2; 3; 4 ;5]; 
for Vin = input("Enter input V: ")
Vout = Vin.*(1-(exp(1).^(-t./(R.*C))))
end
This is the code that I've written. It will take in only one input for Vin and thus display only one output for Vout. 
I want to change the loop such that infinite number of inputs can be put in resulting in infinite outputs, and the output Vout can also be stored for each input value. Right now if I run the program a second time, the first Vin and Vout values are lost. 
댓글 수: 0
답변 (1개)
  Bryant Lash
      
 2021년 9월 30일
        so if you want infinite, then a for loop isn't the way to do it. You want to do a while loop. You can do something like this:
Though to be clear this is jsut about the least efficient way to do this.
allVIn = zeros(0,0);
allVOut = zeros(0,0);
userExited = false;
disp('leave empty to exit');
while ~userExited
    userResponse = input("Enter input V: ");
    if isempty(userResponse); 
        userExited = true;
    else
        allVIn(end + 1,1) = userResponse;
        allVOut(end + 1,1) = userResponse.*(1-(exp(1).^(-t./(R.*C))));
    end
end
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

