ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 25 Oct 2021 16:55:41 +0200Evaluating gradient of numerically approximated functionhttps://ask.sagemath.org/question/59476/evaluating-gradient-of-numerically-approximated-function/Hello, I'd like to ask how to solve the following problem in Sage.
Let's suppose that I have a scalar function on R^3 (written in spherical coordinates) V(r,\theta,\phi), given by some tedious integral over a region in R^2: V(r,th,ph) = \int dx \int dy f(x,y,r,th,ph) (typical when calculating potentials by Green functions). The first question, how could I define such a function V in Sage? I guess it would involve approximating this function point by point by a 3-dim matrix containing results of numerical integration V(r0,th0,ph0) at different (r0,th0,ph0)...
Then, I would like to calculate the gradient of this V(r,th,ph) and plot streamlines or at least some plot of resulting vector field. I know how to do this when Sage is able to evaluate this gradient symbolically, but I don't know how to approximate it numerically and then make a plot.
I appreciate any help.
VonbatenBachMon, 25 Oct 2021 16:55:41 +0200https://ask.sagemath.org/question/59476/Gradient, Divergence, Curl and vector productshttps://ask.sagemath.org/question/10104/gradient-divergence-curl-and-vector-products/Are there implementations of vector product and the nabla operator yet? I can't find anything.KiMon, 21 Oct 2013 12:40:22 +0200https://ask.sagemath.org/question/10104/Define vector valued function of a vector of symbolic variables?https://ask.sagemath.org/question/24943/define-vector-valued-function-of-a-vector-of-symbolic-variables/ I have a gradient where each component may have a different number of symbolic arguments and get the following error when I run:
gradL<br>
(gradL[0]).arguments()<br>
(gradL[1]).arguments()<br>
(gradL[2]).arguments()<br>
s=[3,.5,.3]<br>
(gradL[0])(*s)<br>
(gradL[1])(*s)<br>
(gradL[2])(*s)<br>
(lam0*(x1 - 1.00000000000000) - log(x0) + log(-x0 + 1), lam0*x0 - log(x1) + log(-x1 + 1), x0*x1 - x0 + 0.500000000000000)<br>
(lam0, x0, x1)<br>
(lam0, x0, x1)<br>
(x0, x1)<br>
-2.10000000000000<br>
2.34729786038720<br>
Error in lines 8-8<br>
Traceback (most recent call last):
File "/projects/5511fe15-8085-4d1d-bdc7-c6bf6c99e693/.sagemathcloud/sage_server.py", line 865, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "sage/symbolic/expression.pyx", line 4382, in sage.symbolic.expression.Expression.__call__ (build/cythonized/sage/symbolic/expression.cpp:24669)
File "sage/symbolic/ring.pyx", line 760, in sage.symbolic.ring.SymbolicRing._call_element_ (build/cythonized/sage/symbolic/ring.cpp:8970)
ValueError: the number of arguments must be less than or equal to 2
I need to be able to to be able to define this as a vector valued function of a vector so that I can evaluate it using the entire argument set (lam0, x0, x1) where the missing arguments are ignored for the corresponding component. This is so I can def a function for scipy.optimize like:
def F(s):<br>
tmp=[]<br>
for ii in range(len(gradL)):<br>
tmp.append((gradL[ii])(*s))<br>
return tmp<br>
ssoln = scipy.optimize.broyden1(F, [.5,.5,.5], f_tol=1e-14)
I can't figure out how to accomplish this. Thanks for any help in advance.
-Steve
strangerlandSat, 22 Nov 2014 18:24:55 +0100https://ask.sagemath.org/question/24943/