saving values with variables in a matix

조회 수: 1 (최근 30일)
Rebecca Cizek
Rebecca Cizek 2019년 5월 6일
댓글: Rebecca Cizek 2019년 5월 6일
Hi all
I not really into Matlab, so ther might some mistakes in my code that are easy to solve.
that is my code so far
nodes=2
H_d=ones(nodes,nodes)
for N=1:nodes
for M=1:nodes
syms omega
Pos=sym('pos',[1 nodes]);
P=sym('p',[1 nodes]);
H_d=(1/2*P(N)^2+omega^2*Pos(M)^
2)
end
end
My problem is that I want an Matix H_d that is 2x2 large and consists of the values 'calculated' in the for loop.
These are the values created bey the loop, with the variables inside.
H_d =
(omega^2*pos1^2)/2 + p1^2/2
H_d =
(omega^2*pos2^2)/2 + p1^2/2
H_d =
(omega^2*pos1^2)/2 + p2^2/2
H_d =
(omega^2*pos2^2)/2 + p2^2/2
Now I'd like to put all these values in a Matix, like:
H_d=[(omega^2*pos1^2)/2+p1^2/2 (omega^2*pos2^2)/2+p1^2/2
(omega^2*pos1^2)/2+p2^2/2 (omega^2*pos2^2)/2+p2^2/2]
usually then I had thath problem I just changed H_d to H_d(N,M) and then I get I matrix with all values.
But that didn't work here.
Thats the error Matlab replys:
The following error occurred converting from sym to double:
Error using symengine (line 58)
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use VPA.
Error in split_Hamiltonians_diagonalisation (line 16)
H_d(P(N),Pos(M))=(1/2*(P(N)^2+omega^2*Pos(M)^2));
I am at my wit's end. Hope sopmeone here can help me :)

채택된 답변

madhan ravi
madhan ravi 2019년 5월 6일
편집: madhan ravi 2019년 5월 6일
nodes=2;
syms omega
Pos=sym('pos',[1 nodes]);
P=sym('p',[1 nodes]);
H_d=sym(ones(nodes,nodes)); % if your creating a square matrix it can also be written as sym(ones(noses))
for N=1:nodes
for M=1:nodes
H_d(N,M)=(1/2*P(N)^2+omega^2*Pos(M)^2);
end
end
  댓글 수: 1
Rebecca Cizek
Rebecca Cizek 2019년 5월 6일
It works now !
Thank you very much :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by