For loops step through time and update variable
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I'm trying to create a for loop that updates a variable that can then be used as the new input to get the next variable
My current code is:
% initialise
C=100;
dL=0.01
L=0:dL:0.2; % size 21
A=zeros(size(L))
B=zeros(size(L))
A(1)=300 % initial value for A
for i=size(L)
B(i)=A(i-1)-(C*L(i)) % using old A to get current B
A(i)=B(i-1) % updating current A to equal old B answer
end
It works for the inital value, then rest are zeros. I'm confused because they both depend on eachother
2 comentarios
Dyuman Joshi
el 13 de Mzo. de 2023
Editada: Dyuman Joshi
el 13 de Mzo. de 2023
You have not defined the loop index/counter nor the variable 'constant'. Please update your code.
Respuestas (2)
Dyuman Joshi
el 13 de Mzo. de 2023
Editada: Dyuman Joshi
el 13 de Mzo. de 2023
The problem is in your loop indexing. size returns a row vector, and when you use a row vector as a loop index, it considers the elements of the row vectors as loop indices, see below -
constant=100;
dL=0.01;
L=0:dL:0.2; % size 1x21
A=zeros(size(L));
B=zeros(size(L));
A(1)=300; % initial value for A
for i=size(L)
i
end
%Use numel to get number of elements on an array
%and initialise a vector to use as indices
for k=2:numel(L)
B(k)=A(k-1)-(constant*L(k)); % using old A to get current B
A(k)=B(k-1); % updating current A to equal old B answer
end
A
B
As indexing in MATLAB starts at 1, k needs to start from 2, as (k-1) is used as an index.
5 comentarios
Dyuman Joshi
el 14 de Mzo. de 2023
Editada: Dyuman Joshi
el 14 de Mzo. de 2023
As you stated in another comment, B(1) should be 300.
Is this what you are looking for?
If not, please provide the expected output, so that it is easier to formulate the loop.
constant=100;
dL=0.01;
L=0:dL:0.2; % size 1x21
A=zeros(size(L));
B=zeros(size(L));
A(1)=300;
B(1)=300;
for k=2:numel(L)
B(k)=A(k-1)-(constant*L(k)); % using old A to get current B
A(k)=B(k-1); % updating current A to equal old B answer
end
A
B
VBBV
el 14 de Mzo. de 2023
C=100;
dL=0.01
L=0:dL:0.2; % size 21
A=zeros(size(L));
B=zeros(size(L));
A(1)=300; % initial value for A
for k= 1:length(L)-1
B(k+1)=A(k)-(C*L(k)); % using old A to get current B
A(k+1)=B(k); % updating current A to equal old B answer
end
A
B
hold on
plot(A)
plot(B)
3 comentarios
VBBV
el 15 de Mzo. de 2023
Editada: VBBV
el 15 de Mzo. de 2023
Now you introduce a new condition, the first B value should = 300 at L = 0,
In such case, B(1) = 300 must be initialized like you did for A(1) = 300
Please clarify what is the expected output
C=100;
dL=0.01
L=0:dL:0.2; % size 21
A=zeros(size(L));
B=zeros(size(L));
A(1)=300; % initial value for A
B(1) = 300; % according to your new comment
for k = 2:length(L)
B(k)=A(k-1)-(C*L(k)); % using old A to get current B
A(k)=B(k-1); % updating current A to equal old B answer
end
A
B
hold on
plot(A)
plot(B)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!