フィルターのクリア

How to use previous answer in new calculation n times

4 ビュー (過去 30 日間)
Chris
Chris 2024 年 6 月 10 日
回答済み: Paul 2024 年 6 月 12 日
Hi all
I am pretty new to Matlab and have been struggling with loops and arrays.
My issue is I am attempting to calculate yn where x in the original calculation becomes y1 in caclulation for y2 and so on.
ie:
x = [1;2] %orginal value
it then is multiplied by A= [0.3 -0.2; -0.6 -0.8]
and B=[-14;2] is added to it to become y1, then calculation is repeated and rather than using x, y1 used to calculate y2 and so on.
Any help would be greatly appreciated.
clear; clc; close all;
A=[0.3 -0.2; -0.6 0.8];
B=[-14;2];
x=[1;2];
y1=A*x+B
y2=A*y1+B
y3=A*y2+B
y4=A*y3+B
y5=A*y4+B
y6=A*y5+B
y7=A*y6+B
y8=A*y7+B
y9=A*y8+B
y10=A*y9+B

採用された回答

Star Strider
Star Strider 2024 年 6 月 10 日
Perhaps something like this —
A=[0.3 -0.2; -0.6 0.8];
B=[-14;2];
x=[1;2];
y = x;
for k = 1:10
y = A*y+B;
ym(:,k) = y; % Y-Matrix: 'ym'
end
ym
ym = 2x10
-14.1000 -18.8300 -22.2210 -25.3835 -28.4553 -31.4548 -34.3857 -37.2497 -40.0484 -42.7832 3.0000 12.8600 23.5860 34.2014 44.5912 54.7462 64.6698 74.3673 83.8436 93.1039
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
plot((1:10), ym)
grid
legend('y(1)','y(2)', 'Location','best')
.
  4 件のコメント
David Goodmanson
David Goodmanson 2024 年 6 月 11 日
編集済み: David Goodmanson 2024 年 6 月 12 日
Hi Chris,
A key feature of the answer is that there are not nine different variables y1,y2, ... using all those variables is called dynamic variable naming and it is highly discouraged. Instead you have one matrix variable. Any of the previous variables, for example y4, can easily be accessed since it is ym(:,4). (The use of a colon here means every row in column 4).
Chris
Chris 2024 年 6 月 12 日
thanks David

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

その他の回答 (1 件)

Paul
Paul 2024 年 6 月 12 日
A=[0.3 -0.2; -0.6 0.8];
B=[-14;2];
x=[1;2];
sys = ss(A,B,eye(2),0,-1);
y = lsim(sys,ones(11,1),0:10,x); % y0 = x
%y = step(sys,10) + initial(sys,x,10); % y0 = x; alternative method
figure
plot(0:10,y)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by