I'd like to change the name of a symbolic variable in each iteration of a loop, and then solve an equation using these symbolic variables e.g:
using SymPy
for i in 1:5
p{i} = symbols("p"{i}, real=true,positive=true)
solve(p{i}^2-i^2)
end
So I'm looking to create a series of scalar symbolic variables (since I don't think it is possible to create a vector valued symbolic variable) each with a different name - p1,p2,p3,p4 and p5 - and then use these in a equation solver. However the curly braces notation does not seem to work for naming in julia as per matlab. A quick google didn't suggest any obvious answers. Any ideas?
In julia, and in most computer languages, if you find yourself needing a bunch of number variables x1, x2, x3, ... , you probably want an array. In julia this might look like this, (but note that I have no idea what I'm doing with SymPy)
using SymPy
pp=Sym[]
for i in 1:5
p = symbols("x$i", real=true,positive=true)
push!(pp,p)
solve(pp[i]^2-i^2)
end
Here we start with pp empty, but of the right type; we push each symbol onto the end of pp; finally we can fish out the i'th item of the pp with pp[i]
, which is almost your code, but without the shift key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With