For loops step through time and update variable

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 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 3 月 13 日
編集済み: Dyuman Joshi 2023 年 3 月 13 日
You have not defined the loop index/counter nor the variable 'constant'. Please update your code.
Lucy Perry
Lucy Perry 2023 年 3 月 13 日
I forgot to include it but the idea is the same. The variable 'constant' doesn't matter it's just an equation and doesn't vary through L so not needed to answer the question.

サインインしてコメントする。

回答 (2 件)

Dyuman Joshi
Dyuman Joshi 2023 年 3 月 13 日
編集済み: Dyuman Joshi 2023 年 3 月 13 日

0 投票

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
i = 1
i = 21
%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
A = 1×21
300 0 299 -2 296 -6 291 -12 284 -20 275 -30 264 -42 251 -56 236 -72 219 -90 200
B
B = 1×21
0 299 -2 296 -6 291 -12 284 -20 275 -30 264 -42 251 -56 236 -72 219 -90 200 -110
As indexing in MATLAB starts at 1, k needs to start from 2, as (k-1) is used as an index.

5 件のコメント

Lucy Perry
Lucy Perry 2023 年 3 月 13 日
This worked thank you!
Lucy Perry
Lucy Perry 2023 年 3 月 13 日
This updates both variables but removes the intial value therefore making all my other values wrong... How can you avoid this?
Dyuman Joshi
Dyuman Joshi 2023 年 3 月 13 日
Which initial value is removed?
A(1) is still 300 and you have not initiated any value for B.
Lucy Perry
Lucy Perry 2023 年 3 月 14 日
Sorry- a better way to explain it:
Yes my A(1)=300 which is good
But B(1)= 0 which it should not be- thus messes up all other values as this becomes the new A
Is this to do with the (k-1)?
Dyuman Joshi
Dyuman Joshi 2023 年 3 月 14 日
編集済み: Dyuman Joshi 2023 年 3 月 14 日
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
A = 1×21
300 300 299 298 296 294 291 288 284 280 275 270 264 258 251 244 236 228 219 210 200
B
B = 1×21
300 299 298 296 294 291 288 284 280 275 270 264 258 251 244 236 228 219 210 200 190

サインインしてコメントする。

VBBV
VBBV 2023 年 3 月 14 日

0 投票

C=100;
dL=0.01
dL = 0.0100
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
A = 1×21
300 0 300 -1 298 -4 294 -9 288 -16 280 -25 270 -36 258 -49 244 -64 228 -81 210
B
B = 1×21
0 300 -1 298 -4 294 -9 288 -16 280 -25 270 -36 258 -49 244 -64 228 -81 210 -100
hold on
plot(A)
plot(B)

3 件のコメント

VBBV
VBBV 2023 年 3 月 14 日
As Matlab uses 1 based array indexing, you need the for loop in such a way, that arrays index nevver attain zeros.
Lucy Perry
Lucy Perry 2023 年 3 月 14 日
Something is wrong when I use this code. Yes, my inital A value is 300 but my first B value is now 0 which is not what it should produce and then makes my next A and B values wrong.
The first B value should = 300 at L=0
I think the (k+1) causes it to skip the first value?
VBBV
VBBV 2023 年 3 月 15 日
編集済み: VBBV 2023 年 3 月 15 日
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
dL = 0.0100
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
A = 1×21
300 300 299 298 296 294 291 288 284 280 275 270 264 258 251 244 236 228 219 210 200
B
B = 1×21
300 299 298 296 294 291 288 284 280 275 270 264 258 251 244 236 228 219 210 200 190
hold on
plot(A)
plot(B)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

リリース

R2022b

質問済み:

2023 年 3 月 13 日

編集済み:

2023 年 3 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by