How to have matrix values as a function of another variable

I have a matrix P (defined below) that has some values (h and u) that are a function of a variable t. t have values ranging from 1 to 50. When I define h and n as a function of t and insert them into the matrix I get an error saying "Dimensions of matrices being concatenated are not consistent." How to I fix this issue?
t = 1:50;
h = 0.9*exp(-2*t);
u = 0.9*(1-exp(-2*t));
N = 4;
P = [0.1 h 0 0 0 ; ...
u 0.1 h 0 0; ...
0 u 0.1 h 0; ...
0 0 u 0.1 h; ...
0 0 0 u 0.1];

 채택된 답변

Rahul K
Rahul K 2017년 2월 17일
편집: Rahul K 2017년 2월 17일

0 개 추천

h and u are vectors since t is also a vector, hence why you get the error when trying to put them into the matrix P.
Do you want a different P matrix for each value of t?

댓글 수: 4

Yes, I want a new P matrix for each value of t.
Rahul K
Rahul K 2017년 2월 17일
편집: Rahul K 2017년 2월 17일
If you want to write over the same P value,
t = 1:50;
O5 = ones(1,5);
O4 = ones(1,4);
h = 0.9*exp(-2*t);
u = 0.9*(1-exp(-2*t));
N = 4;
for i=1:50
P = diag(0.1*O5)+diag(h(i)*O4,1)+diag(u(i)*O4,-1);
end
Otherwise if you want to store everything in a 3D matrix,
P = zeros(5,5,50);
for i=1:50
P(:,:,i) = diag(0.1*O5)+diag(h(i)*O4,1)+diag(u(i)*O4,-1);
end
The number of P matrices are what I am looking for (not looking for 3D matrix). However, all of the values in the matrices are the same, even though the values of h and u should change with the iteration number (aka time). Do you know why the values are remaining the same?
nvm, the values just converge very soon in the iterations. thanks!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

제품

태그

질문:

2017년 2월 17일

댓글:

2017년 2월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by