I am using Julia v 0.3.10 with Juno. I am trying to extract the symbols present in a SymPy symbolic expression, but the program hangs on me.
The following lines execute without error:
Pkg.add("SymPy") (Ok)
using SymPy (Ok)
x, y, z = Sym("x", "y", "z") x,y,z)
ex = x + 2y + 10z x + 2*y + 10z)
symb = free_symbols(ex) PyObject set([x, y, z])
vekt = convert(Vector{Sym}, symb) Vector Sym,3
The following hangs the program, the "working" icon in the lower left shows up and never disappears.
vekt[1]
Why does this happen? Am I doing something wrong, if so, what? Is this a bug in Julia, if so: will it be fixed and is there a workaround?
Please help!
Related
I have been trying to work our my project using a Python code which needs integration within a sigma.
I get the following error and despite trying several ways, I was not able to solve it. Below you can find a shorter version of my code for error duplication.
The code can be run without any problem if the lower limit of the integral is zero or positive. If it's negative the code gives error...
File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\integrate\quadpack.py", line 341, in quad
points)
File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\integrate\quadpack.py", line 448, in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
TypeError: must be real number, not mpc
import numpy as np
from scipy.integrate import quad
from mpmath import besselk, besseli, nsum, inf, exp, log, cos, mp
mp.dps = 3; mp.pretty = True
tt = (np.logspace(0.0001, 10, num=10)).round(2)
lenght = len(tt)
k0 = lambda u: besselk(0,u)
f = lambda u: u*exp(-2)
Zwn = lambda n: 0.5*(cos(n)*cos(2*n))
Rn = lambda u, n, xD: (1/u)*k0(xD*((f(u) + (n)**2)**0.5))
Lap_Func = lambda u: nsum(lambda n: ((quad(lambda xD: Zwn(n)*Rn(u, n, xD), -10, 10))[0]), [1, 100])
print(Lap_Func((log(2))*1/tt[3]))
Quad only deals with floats, and does not understand mpmath objects. Either drop mpmath and use directly numpy/scipy functions, or convert mpmath expressions to floats at the end of computations.
The equation I am trying to evaluate: https://i.stack.imgur.com/W0HxL.jpg
I'm attempting to evaluate this integral (and equation) where 'a' and 'x' are just inputs, and x' is (x,y,z). also: x will have the form [a,b,c]. In my 'int_func' below, I have taken the dot product of the transpose and the original, to yield the ^2 power that is observable below, for example in: (X(:,1)-x).^2
The code I have so far is:
int_func = #(x,y,z) func([x,y,z]).*exp((-3/(2*a^2)).*((X(:,1)-x).^2 + (X(:,2)-y).^2 + (X(:,3)-z).^2));
val = integral3(int_func,-inf,inf,-inf,inf,-inf,inf);
disp(val * (3/(2*pi*a^2))^(3/2));
where:
X = [0, 0, 0];
a = 1;
With this code, I should be getting an answer of 0.0370, but instead, I am getting 0.5556.
Can someone suggest another form of evaluating this integral or spot what is wrong with what I have listed?
I am trying to solve a variable in an equation (syms x), I've simplified the equation. I am trying to store the value in P_9, a 1x1000 matrix by converting from a symbol to a double and am getting the error below. It is giving me a symbol of 0x0, which is where I think my error lies.
Please help me troubleshoot my code. Many thanks!
number = 1000;
P_9 = zeros(1,number);
A_t=0.67;
A_e = linspace(0,10,number);
for n=1:number
%% find p9
syms x
eqn = x + 5 == A_t/A_e(n);
solx = solve(eqn,x);
P_9(n) = double(solx);
end
Warning: Explicit solution could not be found.
In solve at 179
In HW4 at 74
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in HW4 (line 76)
P_9(n) = double(solx);
You certainly have an equation, where x can't be isolated.
For example it is impossible to isolate x in tan(x) + x == 1. So if you try to solve this equation analyticaly, matlab will tell you that x can't be isolated and therefore there is no explicit analytical solution.
So instead of using an analytical method to solve your equation, you need to use a numerical method, it's less "sexy" but this time you will be able to solve your equation.
Life is well done, matlab already integrate a numerical solver: vpasolve.
So your code will look like:
for n=1:number
%% find p9
syms x
eqn = x + 5 == A_t/A_e(n);
solx = vpasolve(eqn,x);
P_9(n) = double(solx);
end
I've written this simple function on Matlab:
function [A] = tries (a, b, c, d)
global S CdM AdA
D = #(Z, Vx, Vy, Vz) 0.5*S*(Vx.^2 + Vy.^2 + Vz.^2)*CdM(sqrt(Vx.^2 + Vy.^2 + Vz.^2))*AdA(Z);
A = D(a, b, c, d);
end
but I keep getting this error:
"Index exceeds matrix dimensions"
and it says that the error is in line 3.
When I put the same text in the main function (and not in a separate one) it works perfectly.
Anything in mind what the problem might be?
The only item with index in line 3 is
AdA(Z)
that reading the function should be equal to AdA(a). So Z is useless.
D2 = #(Vx, Vy, Vz) 0.5*S*(Vx.^2 + Vy.^2 + Vz.^2)*CdM(sqrt(Vx.^2 + Vy.^2 + Vz.^2));
A = D2(b, c, d)*AdA(a);
but, ignoring what AdA is and where Z (that is equal to a) comes from, try to force your function to display dimensions in order to see if they should match when the function is called.
disp(size(AdA));
disp(a);
I have a function
function y = testf(x,F,phi,M,beta,alpha)
y = -((F+(1 + phi.*cos(2.*pi.*x))).*M.^3.*(cosh((1 + phi.*cos(2.*pi.*x)).*M)+M.*beta.*sinh((1 + phi.*cos(2.*pi.*x)).*M)))./((1 + phi.*cos(2.*pi.*x)).*M.*cosh((1 + phi.*cos(2.*pi.*x)).*M)+(-1+(1 + phi.*cos(2.*pi.*x)).*M.^2.*beta).*sinh((1 + phi.*cos(2.*pi.*x)).*M))- (alpha.*(M.^2.*(F+(1 + phi.*cos(2.*pi.*x))).*(-1+2.*(1 + phi.*cos(2.*pi.*x)).^2.*M.^2+ cosh(2.*(1 + phi.*cos(2.*pi.*x)).*M)-2.*(1 + phi.*cos(2.*pi.*x)).*M.*sinh(2.*(1 + phi.*cos(2.*pi.*x)).*M)))./(8.*((1 + phi.*cos(2.*pi.*x)).*M.*cosh((1 + phi.*cos(2.*pi.*x)).*M)+(-1+(1 + phi.*cos(2.*pi.*x)).*M.^2.*beta).*sinh((1 + phi.*cos(2.*pi.*x)).*M)).^2));
integrating with
q = quad(#(x) testf (x, F, phi,M, beta, alpha), 0, h);
when q = 0 and x,F,phi,M,beta, how do I find alpha and draw the streamline?
It would be great if you gave some numbers, but here is how you would start this. It assumes you are using a version of Matlab that has MuPad as the Symbolic Engine.
First of all, I wouldn't use quad because symbolic expressions will be involved, use int instead.
If I understood you correctly, have the values of x, F, phi, M, beta and you would like to solve for alpha when q = 0
%define the known variables first
syms alpha %defining symbolic object
Now, the following may not work because it's a huge function:
q = int(y,x,0,h)
If it did, all you have to do is solve and evaluate the results for alpha (this may not work as well):
alpha = eval( solve( 'q' , alpha ) )
If the above didn't achieve anything, you might what to look at the 'IgnoreAnalyticConstraints' option.