Reputation: 3
I can't find any equivalent of the Matlab function vpasolve, which numerically solves an equation. The following is my attempt
Python:
alfa = sympy.Symbol('alfa')
y = np.linspace(0, 100, 6)
angleR = np.zeros((1, np.size(y)))
i = 1
for x in range(0, 100, 20):
p = x/100
angleR[0, i] = np.rad2deg((sympy.solve(2*np.pi*(1-p) == np.sin(2*alfa), alfa)).astype(float))
i = i + 1
print(angleR)
Which produces the following error
TypeError: loop of ufunc does not support argument 0 of type Mul which has no callable sin method
Original Matlab code:
syms alfa
y = 0:20:100;
angleR = zeros(1, length(y));
i = 1;
for j = 0:20:100
p = j/100;
angleR(i) = rad2deg(double(vpasolve(2*pi*(1-p) == 2*alfa - sin(2*alfa), alfa)));
i = i+1
end
Upvotes: 0
Views: 220
Reputation: 9797
There are a few issues:
np.sin
tries to evaluate 2*alfa
and you get the error you mention. For a symbolic expression you need to use sympy.sin
instead.==
in python is used for object equality (boolean value) and has nothing to do with symbolic equality. For the latter you need sympy.Eq(lhs, rhs)
.sympy.nsolve
, with the usage sympy.nsolve(lhs-rhs, variable, initial_guess)
.All in all, the following code produces the same result as Matlab. Some minor liberties were taken, i.e. putting the result in a 1D array instead of a "column vector", which is actually a 2D array. But the spirit of the solution is there and you can modify the result format easily.
import numpy as np
import sympy as sym
alfa = sym.Symbol('alfa')
p_values = np.arange(0, 101, 20) / 100
angleR = [
np.rad2deg(float(sym.nsolve(2*np.pi*(1-p) - 2*alfa + sym.sin(2*alfa), alfa, 0)))
for p in p_values
]
angleR = np.array(angleR)
Upvotes: 1