saving values with variables in a matix

1 次查看(过去 30 天)
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

更多回答(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