saving values with variables in a matix
1 view (last 30 days)
Show older comments
Rebecca Cizek
on 6 May 2019
Commented: Rebecca Cizek
on 6 May 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 :)
0 Comments
Accepted Answer
madhan ravi
on 6 May 2019
Edited: madhan ravi
on 6 May 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
More Answers (0)
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!