小编dus*_*tin的帖子

使用fsolve找到解决方案

import numpy as np
from scipy.optimize import fsolve

musun = 132712000000
T = 365.25 * 86400 * 2 / 3
e = 581.2392124070273


def f(x):
    return ((T * musun ** 2 / (2 * np.pi)) ** (1 / 3) * np.sqrt(1 - x ** 2)
        - np.sqrt(.5 * musun ** 2 / e * (1 - x ** 2)))


x = fsolve(f, 0.01)
f(x)

print x
Run Code Online (Sandbox Code Playgroud)

这段代码有什么问题?它似乎行不通.

python numpy scipy

6
推荐指数
2
解决办法
4万
查看次数

Matplotlib,添加多行文本.添加可以跟随曲线的文本

我已经将文本添加到绘图中,在每行中编码,然后调整它看起来像样,增加或减少宽度,或更改位置.但是,有没有办法让Python知道你想要文本的位置以及你想要它的设置方式?然后我可以添加文本,Python将计算出细节.

例如,看看下面的图片:

在此输入图像描述

在图中,我在左上角有3行文字,在图的线上有一行.

我不得不调整3条线以获得合适的间距.这不是一项艰巨的任务,但如果我能说这里是文本,这里就是位置,那将很容易,然后Python以适当的间距堆叠它.

对于单独的线路,我不得不进行调整,因此它不在线上并降低线路.对于这种情况,有可能告诉python我想在情节上方的文字和80%下线?

我习惯于LaTeX在没有硬编码坐标的情况下进行调整的地方.优点是

(1) if I want to change the location, I can change the percentage shift and not the coordinate.

(2) if the line is angled, the text will adjust to the line.
Run Code Online (Sandbox Code Playgroud)

(2)的优点是我试图将文字放在图中顶部向上倾斜的文本上.

可以这样做,还是我要求多少?如果是这样,我该怎么做?

以下是实现该图的代码:

import numpy as np
import pylab

r1 = 1  #  AU Earth                                                                 
r2 = 1.524  #  AU Mars                                                              
deltanu = 75 * np.pi / 180  #  angle in radians                                     
mu = 38.86984154054163

c = np.sqrt(r1 ** 2 + …
Run Code Online (Sandbox Code Playgroud)

python curve text matplotlib

5
推荐指数
1
解决办法
8323
查看次数

给定系列的总和

我试图找到系列的总和:1 ? 1 / 2 + 1 / 3 ? 1 / 4 + · · · + 1 / 99 ? 1 / 100 ** 2用python.

我的代码是 -

psum = 0
nsum = 0
for k in range(1,100):
    if k%2 == 0:
        nsum += 1.0/k
    else:
        psum += 1.0/k

print psum - nsum - 1.0/100**2
Run Code Online (Sandbox Code Playgroud)

输出为0.69807217931

我没有答案,只想验证我是否做得对.

这不是一个功课问题,而只是随机的Python练习.

python

4
推荐指数
1
解决办法
8194
查看次数

选择最大的奇数python

我试图用Python编写一个简单的程序来计算x,y,z值中最大的奇数.如何为用户提供选择x,y和z值的选项?

所以程序会询问x,y和z是什么,然后说"x,y,z是最大的奇数"或者数字都是偶数.

我到目前为止的内容如下.这至少是一个不错的开始吗?

  # This program exmamines variables x, y, and z 
  # and prints the largest odd number among them

  if x%2 !== 0 and x > y and y > z:
      print 'x is the largest odd among x, y, and z'
  elif y%2 !== 0 and y > z and z > x:
     print 'y is the largest odd among x, y, and z'
  elif z%2 !== 0 and z > y and y > x:
     print …
Run Code Online (Sandbox Code Playgroud)

python

4
推荐指数
3
解决办法
1万
查看次数

在python中绘制轨道轨迹

如何在python中设置三体问题?如何定义解决ODE的功能?

这三个方程是
x'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * x,
y'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * y和
z'' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * z.

写成6个第一顺序我们有

x' = x2,

y' = y2,

z' = z2,

x2' = -mu / np.sqrt(x ** 2 + y ** 2 …

python numpy matplotlib scipy differential-equations

4
推荐指数
1
解决办法
2525
查看次数

python:集成分段函数

我想要整合一个定义的函数,该函数乘以勒让德多项式.不幸的是,我在文档中找不到如何使用x的第n个勒让德多项式.我想,当x的每个勒让德多项式整合n = 1,..., 50,所以我已经设置n = np.arange(1, 51, 1).

import numpy as np
import pylab
from scipy import integrate

n = np.arange(1, 51, 1)                                                   


def f(x):
    if 0 <= x <= 1:
        return 1
    if -1 <= x <= 0:
        return -1
Run Code Online (Sandbox Code Playgroud)

我想我需要定义另一个函数u(x).

c = []


def u(x):
    c.append((2. * n + 1) / 2. * integrate.quad(f(x) * insert Legendre polynomials here, -1., 1.)) 
    return sum(c * Legendre poly, for nn in range(1, …
Run Code Online (Sandbox Code Playgroud)

python numpy scipy numerical-integration

4
推荐指数
1
解决办法
2608
查看次数

例如,从vim复制到stackoverflow

我一直试图crtl + V选择所有文本行然后使用"+y,"*y但是当我尝试粘贴到论坛时,这似乎不起作用.

另外,我去过这篇文章并尝试使用未命名的剪贴板选项更改Vims /.vimrc,但这也没有用.

如何在不做的情况下从Vim复制到论坛帖子中shift + crl + c,然后必须删除行号?

vim ubuntu-12.04

2
推荐指数
1
解决办法
173
查看次数

python中的极地情节

我正试图制作一个极地情节1/t.到目前为止我所拥有的是(可能是错误的).我该如何完成或使其工作?

from pylab import *
import matplotlib.pyplot as plt

theta = arange(0, 6 * pi, 0.01)


def f(theta):
    return 1 / theta
Run Code Online (Sandbox Code Playgroud)

Mathematica中的极地情节

python matplotlib

2
推荐指数
1
解决办法
2万
查看次数

Python:pip 找不到 setup.py

如何直接 pip 查找setup.py?我的setup.py文件位于~/setuptools-3.5.1.

我跑了

dustin@dustin:~$ python setuptools-3.5.1/setup.py egg_info
running egg_info
writing requirements to setuptools.egg-info/requires.txt
writing setuptools.egg-info/PKG-INFO
writing top-level names to setuptools.egg-info/top_level.txt
writing dependency_links to setuptools.egg-info/dependency_links.txt
writing entry points to setuptools.egg-info/entry_points.txt
reading manifest file 'setuptools.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'setuptools.egg-info/SOURCES.txt'
dustin@dustin:~$ 
Run Code Online (Sandbox Code Playgroud)

所以看起来一切都很好,但是当我运行 pip 时,我得到

Command python setup.py egg_info failed with error code 1 in /tmp/pip_build_root/matplotlib
Storing debug log for failure in /home/dustin/.pip/pip.log
Run Code Online (Sandbox Code Playgroud)

我的印象是,当我运行 pip 时,它没有找到setup.py

python pip setuptools

2
推荐指数
1
解决办法
3万
查看次数

使用gcc编译C程序

我正在关注CUNY的一个例子,我之前从未做过任何事情,所以我可能不知道自己在做什么.

考虑下面的程序.

  1. 我是否需要使用emacs编写的C代码的shebang行?
  2. 当我使用该行进行编译时,gcc -g -o forwardadding forwardadding.c 我遇到了以下消息:

    forwardadding.c:9:17: error: expected expression before ‘<’ token
    
    Run Code Online (Sandbox Code Playgroud)
  3. 一旦我得到代码编译,我可以使用gdb调试并运行代码更正?

代码:

#include <stdio.h>
#include <math.h>

main()
{
    float sum, term;
    int i;
    sum = 0.0;
    for( i = 1; < 10000000; i++)
    {
        term = (float) i;
        term = term * term;
        term = 1 / term;
        sum += term;
    }
    printf("The sum is %.12f\n", sum);
}
Run Code Online (Sandbox Code Playgroud)

c gcc shebang

0
推荐指数
2
解决办法
3125
查看次数

python:当一切正确时for循环上的语法无效

Python一直告诉我无效的语法

In [7]:   File "/tmp/ipython-4302iuN.py", line 26
    for n in N:
              ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

它出现在第26行并且我这里没有26行的原因是由于shebang线和注释.

L = 80.0                                               
N = 2 ** np.arange(0, 10, dtype = np.float64)
dt = 0.0002
tmax = 10
nmax = int(np.floor(tmax / dt))  #  also try ceil/floor                            

deltax = []
error = []
u = 2. * (2. / (np.exp(x + 20.) + np.exp(-x - 20.)) ** 2


for n in N:
    dx = L / n
Run Code Online (Sandbox Code Playgroud)

我尝试使用带整数的范围作为测试.我打开了一个单独的ipython并跑了

for n in range(1, 2):
    dx …
Run Code Online (Sandbox Code Playgroud)

python syntax for-loop

-1
推荐指数
1
解决办法
2254
查看次数