saving values with variables in a matix

1 vue (au cours des 30 derniers jours)
Rebecca Cizek
Rebecca Cizek le 6 Mai 2019
Commenté : Rebecca Cizek le 6 Mai 2019
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 :)

Réponse acceptée

madhan ravi
madhan ravi le 6 Mai 2019
Modifié(e) : madhan ravi le 6 Mai 2019
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 commentaire
Rebecca Cizek
Rebecca Cizek le 6 Mai 2019
It works now !
Thank you very much :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by