Exemple #1
0
def main():
  x,y,c,f,rd= symbols('x y c f rd')
  c= x*4+y*3
  f= c**2
  radius=3
  r=5
  p=plot3d(f)
                            plot3d_parametric_surface, plot3d_parametric_line,
                            plot3d)

lx = range(5)
ly = [i**2 for i in lx]

x = Symbol('x')
y = Symbol('y')
u = Symbol('u')
v = Symbol('v')
expr = x**2 - 1

b = plot(expr, (x, 2, 4), show=False)  # cartesian plot
e = plot(exp(-x), (x, 0, 4), show=False)  # cartesian plot (and coloring, see below)
f = plot3d_parametric_line(sin(x), cos(x), x, (x, 0, 10), show=False)  # 3d parametric line plot
g = plot3d(sin(x)*cos(y), (x, -5, 5), (y, -10, 10), show=False)  # 3d surface cartesian plot
h = plot3d_parametric_surface(cos(u)*v, sin(u)*v, u, (u, 0, 10), (v, -2, 2), show=False)  # 3d parametric surface plot

# Some aesthetics
e[0].line_color = lambda x: x / 4
f[0].line_color = lambda x, y, z: z / 10
g[0].surface_color = lambda x, y: sin(x)

# Some more stuff on aesthetics - coloring wrt coordinates or parameters
param_line_2d = plot_parametric((x*cos(x), x*sin(x), (x, 0, 15)), (1.1*x*cos(x), 1.1*x*sin(x), (x, 0, 15)), show=False)
param_line_2d[0].line_color = lambda u: sin(u)  # parametric
param_line_2d[1].line_color = lambda u, v: u**2 + v**2  # coordinates
param_line_2d.title = 'The inner one is colored by parameter and the outher one by coordinates'

param_line_3d = plot3d_parametric_line((x*cos(x), x*sin(x), x, (x, 0, 15)),
                     (1.5*x*cos(x), 1.5*x*sin(x), x, (x, 0, 15)),
Exemple #3
0
def plot_and_save(name):
    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')

    ###
    # Examples from the 'introduction' notebook
    ###

    p = plot(x)
    p = plot(x*sin(x),x*cos(x))
    p.extend(p)
    p[0].line_color = lambda a : a
    p[1].line_color='b'
    p.title = 'Big title'
    p.xlabel = 'the x axis'
    p[1].label = 'straight line'
    p.legend = True
    p.aspect_ratio = (1,1)
    p.xlim = (-15,20)
    p.save(tmp_file('%s_basic_options_and_colors.png' % name))

    p.extend(plot(x+1))
    p.append(plot(x+3,x**2)[1])
    p.save(tmp_file('%s_plot_extend_append.png' % name))

    p[2] = plot(x**2, (x, -2, 3))
    p.save(tmp_file('%s_plot_setitem.png' % name))

    p = plot(sin(x),(x,-2*pi,4*pi))
    p.save(tmp_file('%s_line_explicit.png' % name))

    p = plot(sin(x))
    p.save(tmp_file('%s_line_default_range.png' % name))

    p = plot((x**2, (x, -5, 5)), (x**3, (x, -3, 3)))
    p.save(tmp_file('%s_line_multiple_range.png' % name))


    #parametric 2d plots.
    #Single plot with default range.
    plot_parametric(sin(x), cos(x)).save(tmp_file())

    #Single plot with range.
    p = plot_parametric(sin(x), cos(x), (x, -5, 5))
    p.save(tmp_file('%s_parametric_range.png' % name))

    #Multiple plots with same range.
    p = plot_parametric((sin(x), cos(x)), (x, sin(x)))
    p.save(tmp_file('%s_parametric_multiple.png' % name))

    #Multiple plots with different ranges.
    p = plot_parametric((sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5)))
    p.save(tmp_file('%s_parametric_multiple_ranges.png' % name))

    #depth of recursion specified.
    p = plot_parametric(x, sin(x), depth=13)
    p.save(tmp_file('%s_recursion_depth' % name))

    #No adaptive sampling.
    p = plot_parametric(cos(x), sin(x), adaptive=False, nb_of_points=500)
    p.save(tmp_file('%s_adaptive' % name))

    #3d parametric plots
    p = plot3d_parametric_line(sin(x),cos(x),x)
    p.save(tmp_file('%s_3d_line.png' % name))

    p = plot3d_parametric_line((sin(x), cos(x), x, (x, -5, 5)), (cos(x), sin(x), x, (x, -3, 3)))
    p.save(tmp_file('%s_3d_line_multiple' % name))

    p = plot3d_parametric_line(sin(x), cos(x), x, nb_of_points=30)
    p.save(tmp_file('%s_3d_line_points' % name))

    # 3d surface single plot.
    p = plot3d(x * y)
    p.save(tmp_file('%s_surface.png' % name))

    # Multiple 3D plots with same range.
    p = plot3d(-x * y, x * y, (x, -5, 5))
    p.save(tmp_file('%s_surface_multiple' % name))

    # Multiple 3D plots with different ranges.
    p = plot3d((x * y, (x, -3, 3), (y, -3, 3)), (-x * y, (x, -3, 3), (y, -3, 3)))
    p.save(tmp_file('%s_surface_multiple_ranges' % name))

    # Single Parametric 3D plot
    p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y)
    p.save(tmp_file('%s_parametric_surface' % name))

    # Multiple Parametric 3D plots.
    p = plot3d_parametric_surface((x*sin(z),x*cos(z),z, (x, -5, 5), (z, -5, 5)),
                (sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5)))
    p.save(tmp_file('%s_parametric_surface.png' % name))

    ###
    # Examples from the 'colors' notebook
    ###

    p = plot(sin(x))
    p[0].line_color = lambda a : a
    p.save(tmp_file('%s_colors_line_arity1.png' % name))

    p[0].line_color = lambda a, b : b
    p.save(tmp_file('%s_colors_line_arity2.png' % name))

    p = plot(x*sin(x), x*cos(x), (x, 0, 10))
    p[0].line_color = lambda a : a
    p.save(tmp_file('%s_colors_param_line_arity1.png' % name))

    p[0].line_color = lambda a, b : a
    p.save(tmp_file('%s_colors_param_line_arity2a.png' % name))

    p[0].line_color = lambda a, b : b
    p.save(tmp_file('%s_colors_param_line_arity2b.png' % name))

    p = plot3d_parametric_line(sin(x)+0.1*sin(x)*cos(7*x),
             cos(x)+0.1*cos(x)*cos(7*x),
             0.1*sin(7*x),
             (x, 0 , 2*pi))
    p[0].line_color = lambda a : sin(4*a)
    p.save(tmp_file('%s_colors_3d_line_arity1.png' % name))
    p[0].line_color = lambda a, b : b
    p.save(tmp_file('%s_colors_3d_line_arity2.png' % name))
    p[0].line_color = lambda a, b, c : c
    p.save(tmp_file('%s_colors_3d_line_arity3.png' % name))

    p = plot3d(sin(x)*y, (x, 0, 6*pi), (y, -5, 5))
    p[0].surface_color = lambda a : a
    p.save(tmp_file('%s_colors_surface_arity1.png' % name))
    p[0].surface_color = lambda a, b : b
    p.save(tmp_file('%s_colors_surface_arity2.png' % name))
    p[0].surface_color = lambda a, b, c : c
    p.save(tmp_file('%s_colors_surface_arity3a.png' % name))
    p[0].surface_color = lambda a, b, c : sqrt((a-3*pi)**2+b**2)
    p.save(tmp_file('%s_colors_surface_arity3b.png' % name))

    p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y,
             (x, -1, 1), (y, -1, 1))
    p[0].surface_color = lambda a : a
    p.save(tmp_file('%s_colors_param_surf_arity1.png' % name))
    p[0].surface_color = lambda a, b : a*b
    p.save(tmp_file('%s_colors_param_surf_arity2.png' % name))
    p[0].surface_color = lambda a, b, c : sqrt(a**2+b**2+c**2)
    p.save(tmp_file('%s_colors_param_surf_arity3.png' % name))

    ###
    # Examples from the 'advanced' notebook
    ###

    i = Integral(log((sin(x)**2+1)*sqrt(x**2+1)),(x,0,y))
    p = plot(i,(y, 1, 5))
    p.save(tmp_file('%s_advanced_integral.png' % name))

    s = summation(1/x**y,(x, 1, oo))
    p = plot(s, (y, 2, 10))
    p.save(tmp_file('%s_advanced_inf_sum.png' % name))

    p = plot(summation(1/x,(x,1,y)), (y, 2,10), show=False)
    p[0].only_integers = True
    p[0].steps = True
    p.save(tmp_file('%s_advanced_fin_sum.png' % name))


    ###
    # Test expressions that can not be translated to np and generate complex
    # results.
    ###


    plot(sin(x)+I*cos(x)).save(tmp_file())
    plot(sqrt(sqrt(-x))).save(tmp_file())
    plot(LambertW(x)).save(tmp_file())
    plot(sqrt(LambertW(x))).save(tmp_file())
import numpy as np
from engine.interpolate import splint2
from sympy.plotting import plot3d
from sympy.abc import x, y
from scipy import misc

image = misc.imread('bitmap_force_input.jpg')
image = image.astype(dtype=np.float)

grey = np.add.reduce(image, 2)/3.0
grey = np.fliplr(np.swapaxes(grey, 1, 0))

(m, n) = grey.shape

xs = np.linspace(0.0, m, num=m)
ys = np.linspace(0.0, n, num=n)
zs = grey

tolerance = 1e-6

print "starting build a spline"
spline_expression = splint2(xs, ys, zs, x, y)
print "end building a spline"

plot3d(spline_expression, (x, 0.0, m), (y, 0.0, n))
print('g ->', sym.factor(x**3 + 12 * x * y * z + 3 * y**2 * z))

print('h ->', sym.solveset(x - 4, x))

matrix1 = sym.Matrix([[5, 12, 40], [30, 70, 2]])
matrix2 = sym.Matrix([2, 1, 0])
print('i ->')
sym.pprint(matrix1 * matrix2)
print()

print('j ->')
plot(x**3 + 3, (x, -10, 10))
print()

print('k ->')
plot3d(x**2 * y**3, (x, -6, 6), (y, -6, 6))
#==============================================================================
print("_________________Question2_______________")
workbook = xlsxwriter.Workbook('sample1.xlsx')
worksheet = workbook.add_worksheet()

str1 = 'This is Example'
str2 = 'My first export examlpe'

format1 = workbook.add_format({
    'bold': True,
    'font_color': 'red',
    'font_size': 15
})

format2 = workbook.add_format({'font_size': 10})
Exemple #6
0
sho_potential_1D = m * omega**2 * x**2 / 2
sho_potential_3D = m * omega**2 * r_sq / 2

sho_ham_1D = hamiltonian_one_dim(func=psi_a,
                                 potential=sho_potential_1D,
                                 var=x,
                                 constants=True)
sho_ham_3D = hamiltonian_three_dim(func=psi_c,
                                   potential=sho_potential_3D,
                                   constants=True)

print(sho_ham_1D)
print(sho_ham_3D)

# Plotting
sho_ham_1D_plot = plot(sho_ham_1D, plot=False)
sho_ham_3D_plot = plot3d(sho_ham_3D, plot=False)
'''
Euler–Lagrange
'''


def lagrangian(kinetic, potential):
    l = kinetic - potential
    return l


def action(lagra, var, var_1, var_2):
    s = sym.integrate(lagra, (var, var_1, var_2))
    return s
from sympy import Point, Line, pi, symbols, plot
from sympy.plotting import plot3d, plot3d_parametric_surface
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits import mplot3d

p1, p2 = Point(0, 0), Point(1, 1)
l = Line(p1, p2)
t = symbols('t')
plot(3 + 5 * t, (t, -5, 5))

x, y = symbols('x y')
plot3d(3 + 5 * x, (x, -5, 5), (y, -5, 5))

z = symbols('x y z')
plot3d(x**2 + y**2, (x, -5, 5), (y, -5, 5))

xx, yy = np.meshgrid(range(10), range(10))

z = 0 * xx + 0 * yy + 475

plt3d = plt.figure().gca(projection='3d')
plt3d.plot_surface(xx, yy, z, alpha=0.2)
ar1, ar2, ar3 = t_df['x_kb'], t_df['y_kb'], t_df['z_kb']
#plt3d.plot(ar1, ar2, ar3)

# Ensure that the next plot doesn't overwrite the first plot
ax = plt.gca()
Exemple #8
0
def plot3d(*args, **kwargs):
    if "show" in kwargs:
        kwargs.pop("show")
    return plotter.plot3d(*plotArgs(args), show=False, **kwargs)
Exemple #9
0
def graph3d(calculation, **kwargs):
    result = plot3d(sympify(calculation), show=False, **kwargs)
    result.save(result_file)
from sympy import *
from sympy.plotting import plot3d

x, y = symbols('x y')
f = 2 * x + 3 * y
plot3d(f)
Exemple #11
0
from sympy import symbols
from sympy.plotting import plot3d

x1, x2, x3 = symbols('x1 x2 x3')

plot3d(x1 + x2 + x3 - 1, (x1, -5, 5), (x2, -5, 5), (x3, -5, 5))
Exemple #12
0
def plot_and_save(name):
    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')

    ###
    # Examples from the 'introduction' notebook
    ###

    p = plot(x)
    p = plot(x * sin(x), x * cos(x))
    p.extend(p)
    p[0].line_color = lambda a: a
    p[1].line_color = 'b'
    p.title = 'Big title'
    p.xlabel = 'the x axis'
    p[1].label = 'straight line'
    p.legend = True
    p.aspect_ratio = (1, 1)
    p.xlim = (-15, 20)
    p.save(tmp_file('%s_basic_options_and_colors.png' % name))

    p.extend(plot(x + 1))
    p.append(plot(x + 3, x**2)[1])
    p.save(tmp_file('%s_plot_extend_append.png' % name))

    p[2] = plot(x**2, (x, -2, 3))
    p.save(tmp_file('%s_plot_setitem.png' % name))

    p = plot(sin(x), (x, -2 * pi, 4 * pi))
    p.save(tmp_file('%s_line_explicit.png' % name))

    p = plot(sin(x))
    p.save(tmp_file('%s_line_default_range.png' % name))

    p = plot((x**2, (x, -5, 5)), (x**3, (x, -3, 3)))
    p.save(tmp_file('%s_line_multiple_range.png' % name))

    #parametric 2d plots.
    #Single plot with default range.
    plot_parametric(sin(x), cos(x)).save(tmp_file())

    #Single plot with range.
    p = plot_parametric(sin(x), cos(x), (x, -5, 5))
    p.save(tmp_file('%s_parametric_range.png' % name))

    #Multiple plots with same range.
    p = plot_parametric((sin(x), cos(x)), (x, sin(x)))
    p.save(tmp_file('%s_parametric_multiple.png' % name))

    #Multiple plots with different ranges.
    p = plot_parametric((sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5)))
    p.save(tmp_file('%s_parametric_multiple_ranges.png' % name))

    #depth of recursion specified.
    p = plot_parametric(x, sin(x), depth=13)
    p.save(tmp_file('%s_recursion_depth' % name))

    #No adaptive sampling.
    p = plot_parametric(cos(x), sin(x), adaptive=False, nb_of_points=500)
    p.save(tmp_file('%s_adaptive' % name))

    #3d parametric plots
    p = plot3d_parametric_line(sin(x), cos(x), x)
    p.save(tmp_file('%s_3d_line.png' % name))

    p = plot3d_parametric_line((sin(x), cos(x), x, (x, -5, 5)),
                               (cos(x), sin(x), x, (x, -3, 3)))
    p.save(tmp_file('%s_3d_line_multiple' % name))

    p = plot3d_parametric_line(sin(x), cos(x), x, nb_of_points=30)
    p.save(tmp_file('%s_3d_line_points' % name))

    # 3d surface single plot.
    p = plot3d(x * y)
    p.save(tmp_file('%s_surface.png' % name))

    # Multiple 3D plots with same range.
    p = plot3d(-x * y, x * y, (x, -5, 5))
    p.save(tmp_file('%s_surface_multiple' % name))

    # Multiple 3D plots with different ranges.
    p = plot3d((x * y, (x, -3, 3), (y, -3, 3)),
               (-x * y, (x, -3, 3), (y, -3, 3)))
    p.save(tmp_file('%s_surface_multiple_ranges' % name))

    # Single Parametric 3D plot
    p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y)
    p.save(tmp_file('%s_parametric_surface' % name))

    # Multiple Parametric 3D plots.
    p = plot3d_parametric_surface(
        (x * sin(z), x * cos(z), z, (x, -5, 5), (z, -5, 5)),
        (sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5)))
    p.save(tmp_file('%s_parametric_surface.png' % name))

    ###
    # Examples from the 'colors' notebook
    ###

    p = plot(sin(x))
    p[0].line_color = lambda a: a
    p.save(tmp_file('%s_colors_line_arity1.png' % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_line_arity2.png' % name))

    p = plot(x * sin(x), x * cos(x), (x, 0, 10))
    p[0].line_color = lambda a: a
    p.save(tmp_file('%s_colors_param_line_arity1.png' % name))

    p[0].line_color = lambda a, b: a
    p.save(tmp_file('%s_colors_param_line_arity2a.png' % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_param_line_arity2b.png' % name))

    p = plot3d_parametric_line(
        sin(x) + 0.1 * sin(x) * cos(7 * x),
        cos(x) + 0.1 * cos(x) * cos(7 * x), 0.1 * sin(7 * x), (x, 0, 2 * pi))
    p[0].line_color = lambda a: sin(4 * a)
    p.save(tmp_file('%s_colors_3d_line_arity1.png' % name))
    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_3d_line_arity2.png' % name))
    p[0].line_color = lambda a, b, c: c
    p.save(tmp_file('%s_colors_3d_line_arity3.png' % name))

    p = plot3d(sin(x) * y, (x, 0, 6 * pi), (y, -5, 5))
    p[0].surface_color = lambda a: a
    p.save(tmp_file('%s_colors_surface_arity1.png' % name))
    p[0].surface_color = lambda a, b: b
    p.save(tmp_file('%s_colors_surface_arity2.png' % name))
    p[0].surface_color = lambda a, b, c: c
    p.save(tmp_file('%s_colors_surface_arity3a.png' % name))
    p[0].surface_color = lambda a, b, c: sqrt((a - 3 * pi)**2 + b**2)
    p.save(tmp_file('%s_colors_surface_arity3b.png' % name))

    p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y,
                                  (x, -1, 1), (y, -1, 1))
    p[0].surface_color = lambda a: a
    p.save(tmp_file('%s_colors_param_surf_arity1.png' % name))
    p[0].surface_color = lambda a, b: a * b
    p.save(tmp_file('%s_colors_param_surf_arity2.png' % name))
    p[0].surface_color = lambda a, b, c: sqrt(a**2 + b**2 + c**2)
    p.save(tmp_file('%s_colors_param_surf_arity3.png' % name))

    ###
    # Examples from the 'advanced' notebook
    ###

    i = Integral(log((sin(x)**2 + 1) * sqrt(x**2 + 1)), (x, 0, y))
    p = plot(i, (y, 1, 5))
    p.save(tmp_file('%s_advanced_integral.png' % name))

    s = summation(1 / x**y, (x, 1, oo))
    p = plot(s, (y, 2, 10))
    p.save(tmp_file('%s_advanced_inf_sum.png' % name))

    p = plot(summation(1 / x, (x, 1, y)), (y, 2, 10), show=False)
    p[0].only_integers = True
    p[0].steps = True
    p.save(tmp_file('%s_advanced_fin_sum.png' % name))

    ###
    # Test expressions that can not be translated to np and generate complex
    # results.
    ###
    plot(sin(x) + I * cos(x)).save(tmp_file())
    plot(sqrt(sqrt(-x))).save(tmp_file())
    plot(LambertW(x)).save(tmp_file())
    plot(sqrt(LambertW(x))).save(tmp_file())
Exemple #13
0
 def plot_loss(self, expression, path):
     from sympy.plotting import plot3d
     graph = plot3d(expression, show=False)
     graph.save(f"{path}/loss_function")
     plt.close("all")
# assumed values
u0 = 1
v0 = 0
omegan = 4.
wd = omegan*sympy.sqrt(1-zeta**2)

ics = {u.subs(t, 0): u0, u.diff(t).subs(t, 0): v0}
sol = dsolve(u.diff(t, t) + 2*zeta*omegan*u.diff(t) + omegan**2*u, ics=ics)

#import matplotlib
#matplotlib.use('TkAgg')
from sympy.plotting import plot3d
p1 = plot3d(sol.rhs, (t, 0, 10), (zeta, 0.05, 0.7),
    show=False,
    nb_of_points_x=500,
    nb_of_points_y=10,
    xlabel='$t$',
    ylabel='$\zeta$',
    zlabel='$u(t)$',
    )
p1.show()









Exemple #15
0
def plot_and_save_3(name):
    tmp_file = TmpFileManager.tmp_file

    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')

    ###
    # Examples from the 'colors' notebook
    ###

    p = plot(sin(x))
    p[0].line_color = lambda a: a
    p.save(tmp_file('%s_colors_line_arity1' % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_line_arity2' % name))
    p._backend.close()

    p = plot(x*sin(x), x*cos(x), (x, 0, 10))
    p[0].line_color = lambda a: a
    p.save(tmp_file('%s_colors_param_line_arity1' % name))

    p[0].line_color = lambda a, b: a
    p.save(tmp_file('%s_colors_param_line_arity2a' % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_param_line_arity2b' % name))
    p._backend.close()

    p = plot3d_parametric_line(sin(x) + 0.1*sin(x)*cos(7*x),
             cos(x) + 0.1*cos(x)*cos(7*x),
        0.1*sin(7*x),
        (x, 0, 2*pi))
    p[0].line_color = lambdify_(x, sin(4*x))
    p.save(tmp_file('%s_colors_3d_line_arity1' % name))
    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_3d_line_arity2' % name))
    p[0].line_color = lambda a, b, c: c
    p.save(tmp_file('%s_colors_3d_line_arity3' % name))
    p._backend.close()

    p = plot3d(sin(x)*y, (x, 0, 6*pi), (y, -5, 5))
    p[0].surface_color = lambda a: a
    p.save(tmp_file('%s_colors_surface_arity1' % name))
    p[0].surface_color = lambda a, b: b
    p.save(tmp_file('%s_colors_surface_arity2' % name))
    p[0].surface_color = lambda a, b, c: c
    p.save(tmp_file('%s_colors_surface_arity3a' % name))
    p[0].surface_color = lambdify_((x, y, z), sqrt((x - 3*pi)**2 + y**2))
    p.save(tmp_file('%s_colors_surface_arity3b' % name))
    p._backend.close()

    p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y,
             (x, -1, 1), (y, -1, 1))
    p[0].surface_color = lambda a: a
    p.save(tmp_file('%s_colors_param_surf_arity1' % name))
    p[0].surface_color = lambda a, b: a*b
    p.save(tmp_file('%s_colors_param_surf_arity2' % name))
    p[0].surface_color = lambdify_((x, y, z), sqrt(x**2 + y**2 + z**2))
    p.save(tmp_file('%s_colors_param_surf_arity3' % name))
    p._backend.close()
Exemple #16
0
v0 = 0
omegaf = omegan * ratio
f0 = 10.

# solving ODE
f = f0 * sympy.cos(omegaf * t)
ics = {
    u.subs(t, 0): u0,
    u.diff(t).subs(t, 0): v0,
}
sol = dsolve(u.diff(t, t) + 2 * zeta * omegan * u.diff(t) + omegan**2 * u - f,
             ics=ics)

import matplotlib
matplotlib.use('TkAgg')
from sympy.plotting import plot3d

subs = {zeta: 0.2}
p1 = plot3d(
    sol.rhs.subs(subs),
    (t, 0, 10),
    (ratio, 0.1, 2),
    show=False,
    nb_of_points_x=250,
    nb_of_points_y=250,
    xlabel='$t$',
    ylabel='$\omega_f/\omega_n$',
    zlabel='$u(t)$',
)
p1.show()
print(sym.expand((x + y)**2))
print(sym.simplify((4 * x**3 + 21 * x**2 + 10 * x + 12)))
print(sym.limit(1 / (x**2), x, sym.oo))
i, n = sym.symbols('i n')
print(sym.summation(2 * i + i - 1, (i, 5, n)))
print(sym.integrate(sin(x) + exp(x) * cos(x) + tan(x), x))
print(sym.factor(x**3 + 12 * x * y * z + 3 * y**2 * z))
print(sym.solveset(x - 4, x))
m1 = sym.Matrix([[5, 12, 40], [30, 70, 2]])
m2 = sym.Matrix([2, 1, 0])
print(m1 * m2)

plot(x**3 + 3, (x, -10, 10))

f = x**2 * y**3
plot3d(f, (x, -6, 6), (y, -6, 6))

print("==========================Exercise two=====================")

workbook = xlsxwriter.Workbook('forEx.xlsx')
worksheet = workbook.add_worksheet()

text1 = 'This is Example'
text2 = 'My first export examlpe'

Fformat = workbook.add_format({
    'bold': True,
    'font_color': 'red',
    'font_size': 14
})
from sympy import symbols
import numpy as np
from engine.interpolate import splint2
from sympy.plotting import plot3d

x, y = symbols('x y')

m = 5
n = 4
xs = np.array([2.0, 3.0, 6.0, 8.0, 9.0], dtype=np.float)
ys = np.array([1.0, 2.0, 4.0, 8.0], dtype=np.float)
zs = np.random.rand(5, 4)*10.0

tolerance = 1e-6

spline_expression = splint2(xs, ys, zs, x, y)

plot3d(spline_expression, (x, 2.0, 9.0), (y, 1.0, 8.0))
Exemple #19
0
import sympy
from sympy import Function, dsolve, Symbol

# symbols
t = Symbol('t', positive=True)
wf = Symbol('wf', positive=True)

# unknown function
u = Function('u')(t)

# solving ODE with initial conditions
u0 = 0.4
v0 = 2
k = 150
m = 2
F0 = 10
wn = sympy.sqrt(k/m)
#wf = 2*sympy.sqrt(k/m)
F = F0*sympy.sin(wf*t)
ics = {u.subs(t, 0): u0, u.diff(t).subs(t, 0): v0}

sol = dsolve(m*u.diff(t, t) + k*u - F, ics=ics)

import matplotlib
matplotlib.use('TkAgg')
from sympy.plotting import plot3d
p1 = plot3d(sol.rhs, (t, 0, 10), (wf, 0.8*wn, 0.99*wn),
        xlabel='$t$', ylabel='$\omega_f$', zlabel='$u(t)$',
        nb_of_points_x=250, nb_of_points_y=25)
Exemple #20
0
def newton_multi(request):
    x, y, z, w = sympy.symbols('x y z w')

    valores = request.POST
    n = len(valores)

    plt.rcParams.update(plt.rcParamsDefault)
    plt.close('all')

    if n == 5:
        iteraciones = 10

        resul = {'titulos': ['n', 'Vector Solución', 'f₁(x, y), f₂(x, y)'], 'filas': []}
        f1 = sympy.sympify(valores['f1'])
        x0 = float(valores['x0'])

        f2 = sympy.sympify(valores['f2'])
        y0 = float(valores['y0'])

        # derivadas parciales
        f1x = sympy.diff(f1, x)
        f1y = sympy.diff(f1, y)

        f2x = sympy.diff(f2, x)
        f2y = sympy.diff(f2, y)

        # vector de las funciones iniciales
        v = sympy.Matrix([[f1], [f2]])

        # inversa,de la jacobiana
        j_inv = (sympy.Matrix([[f1x, f1y],
                               [f2x, f2y]])) ** -1

        # lamdify de las matrices
        jaco = sympy.lambdify([x, y], j_inv, 'numpy')
        fxfy = sympy.lambdify([x, y], v, 'numpy')

        solucion = np.array([[x0], [y0]])

        for n in range(1, iteraciones + 1):
            # Dandole formato a los valores
            sol = fxfy(solucion[0][0], solucion[1][0])
            xs = f'{solucion[0][0]:.6f}'
            ys = f'{solucion[1][0]:.6f}'
            fxn = f'{float(sol[0]):.6f}'
            fyn = f'{float(sol[1]):.6f}'

            resul['filas'].append([n, xs + ' | ' + ys, fxn + ' | ' + fyn])

            j = jaco(solucion[0][0], solucion[1][0]).dot(fxfy(solucion[0][0], solucion[1][0]))
            solucion = solucion - j

        context = {'context': resul}
        context["jaco"] = sympy.latex(sympy.Matrix([[f1x, f1y],
                                                    [f2x, f2y]])).replace("\left[", ' ').strip("\\right]").replace(
            "matrix", "bmatrix")
        # graficación
        plt.rc_context({'axes.edgecolor': 'w', 'xtick.color': 'w', 'ytick.color': 'w'})
        # plt.style.use("dark_background")

        xs = solucion[0][0]  # x solucion
        ys = solucion[1][0]  # y solucion

        titulo = '\n' + estiliza_string(valores['f1']) + ' and ' + estiliza_string(valores['f2']) + '\n'

        plt.rc_context({'axes.edgecolor': 'gray', 'xtick.color': 'gray', 'ytick.color': 'gray'})
        p = plot3d(f1, f2, (x, xs - 3, xs + 3), (y, ys - 3, ys + 3), title=titulo, nb_of_points_x=35, nb_of_points_y=35,
                   xlabel='X', ylabel='Y')
        buf = BytesIO()
        p._backend.fig.savefig(buf, format='jpg', quality=90, bbox_inches='tight', facecolor="#f3f2f1", dpi=150,
                               transparent=True)
        buf.seek(0)
        uri = 'data:image/png;base64,' + parse.quote(b64encode(buf.read()))
        context['image'] = uri
        p._backend.close()


    elif n == 7:
        iteraciones = 15

        resul = {'titulos': ['n', 'Vector Solución', 'f₁ | f₂ | f₃'], 'filas': []}
        f1 = sympy.sympify(valores['f1'])
        x0 = float(valores['x0'])

        f2 = sympy.sympify(valores['f2'])
        y0 = float(valores['y0'])

        f3 = sympy.sympify(valores['f3'])
        z0 = float(valores['z0'])

        # derivadas parciales
        f1x = sympy.diff(f1, x)
        f1y = sympy.diff(f1, y)
        f1z = sympy.diff(f1, z)

        f2x = sympy.diff(f2, x)
        f2y = sympy.diff(f2, y)
        f2z = sympy.diff(f2, z)

        f3x = sympy.diff(f3, x)
        f3y = sympy.diff(f3, y)
        f3z = sympy.diff(f3, z)

        # funciones iniciales
        v = sympy.Matrix([[f1], [f2], [f3]])

        # inversa jacobiana

        j_inv = (sympy.Matrix([[f1x, f1y, f1z],
                               [f2x, f2y, f2z],
                               [f3x, f3y, f3z]])) ** -1

        jaco = sympy.lambdify([x, y, z], j_inv, "numpy")
        fxfyfz = sympy.lambdify([x, y, z], v, "numpy")

        solucion = np.array([[x0], [y0], [z0]])

        for n in range(1, iteraciones + 1):
            # Dandole formato a los valores
            sol = fxfyfz(solucion[0][0], solucion[1][0], solucion[2][0])
            xs = f'{solucion[0][0]:.4f}'
            ys = f'{solucion[1][0]:.4f}'
            zs = f'{solucion[2][0]:.4f}'

            fxn = f'{float(sol[0]):.4f}'
            fyn = f'{float(sol[1]):.4f}'
            fzn = f'{float(sol[2]):.4f}'

            resul['filas'].append([n, xs + ' | ' + ys + ' | ' + zs, fxn + ' | ' + fyn + ' | ' + fzn])

            j = jaco(solucion[0][0], solucion[1][0], solucion[2][0]).dot(
                fxfyfz(solucion[0][0], solucion[1][0], solucion[2][0]))
            solucion -= j

        context = {'context': resul}
        context["jaco"] = sympy.latex(sympy.Matrix([[f1x, f1y, f1z],
                                                    [f2x, f2y, f2z],
                                                    [f3x, f3y, f3z]])).replace("\left[", ' ').strip("\\right]").replace(
            "matrix", "bmatrix")

    context["inv"] = sympy.latex(j_inv).replace("\left[", ' ').strip("\\right]").replace("matrix", "bmatrix")
    context["mat"] = sympy.latex(v).replace("\left[", ' ').strip("\\right]").replace("matrix", "bmatrix")
    return render(request, "newton_calculado_multi.html", context)
x = sym.Symbol('x')
y = sym.Symbol('y')

f = sy.Function('f')(x, y)

y, yp, N = sym.symbols('y_i, y^p_i, N')

MSE = 1 / n * sy.Sum((y - yp)**2, (i, 1, n))
MSE

sy.Derivative(MSE, y, yp, 1)

MSE = 1 / n * sy.summation((y - yp)**2, (i, 1, n))
MSE

MSE_diff = sy.diff((y**2 + yp**2)**0.5, y, yp, 1)
MSE_diff

plt.ion()
p1 = plot3d(sy.diff((y**2 + yp**2)**0.5, y, yp, 1), (y, -5, 5), (yp, -5, 5))
fg, ax = p1._backend.fig, p1._backend.ax
plt.rcParams["figure.figsize"] = (20, 10)
ax[0].axis('tight')
#ax[0].set_aspect('auto') # 'auto', 'equal' or a positive integer is allowed
ax[0].grid(True)
ax[0].view_init(100, 150)
plt.draw()
plt.ion()
plt.show()
Exemple #22
0
def plot_and_save(name):
    tmp_file = TmpFileManager.tmp_file

    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')

    ###
    # Examples from the 'introduction' notebook
    ###

    p = plot(x)
    p = plot(x * sin(x), x * cos(x))
    p.extend(p)
    p[0].line_color = lambda a: a
    p[1].line_color = 'b'
    p.title = 'Big title'
    p.xlabel = 'the x axis'
    p[1].label = 'straight line'
    p.legend = True
    p.aspect_ratio = (1, 1)
    p.xlim = (-15, 20)
    p.save(tmp_file('%s_basic_options_and_colors' % name))
    p._backend.close()

    p.extend(plot(x + 1))
    p.append(plot(x + 3, x**2)[1])
    p.save(tmp_file('%s_plot_extend_append' % name))

    p[2] = plot(x**2, (x, -2, 3))
    p.save(tmp_file('%s_plot_setitem' % name))
    p._backend.close()

    p = plot(sin(x), (x, -2 * pi, 4 * pi))
    p.save(tmp_file('%s_line_explicit' % name))
    p._backend.close()

    p = plot(sin(x))
    p.save(tmp_file('%s_line_default_range' % name))
    p._backend.close()

    p = plot((x**2, (x, -5, 5)), (x**3, (x, -3, 3)))
    p.save(tmp_file('%s_line_multiple_range' % name))
    p._backend.close()

    raises(ValueError, lambda: plot(x, y))

    p = plot(Piecewise((1, x > 0), (0, True)), (x, -1, 1))
    p.save(tmp_file('%s_plot_piecewise' % name))
    p._backend.close()

    #parametric 2d plots.
    #Single plot with default range.
    plot_parametric(sin(x), cos(x)).save(tmp_file())

    #Single plot with range.
    p = plot_parametric(sin(x), cos(x), (x, -5, 5))
    p.save(tmp_file('%s_parametric_range' % name))
    p._backend.close()

    #Multiple plots with same range.
    p = plot_parametric((sin(x), cos(x)), (x, sin(x)))
    p.save(tmp_file('%s_parametric_multiple' % name))
    p._backend.close()

    #Multiple plots with different ranges.
    p = plot_parametric((sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5)))
    p.save(tmp_file('%s_parametric_multiple_ranges' % name))
    p._backend.close()

    #depth of recursion specified.
    p = plot_parametric(x, sin(x), depth=13)
    p.save(tmp_file('%s_recursion_depth' % name))
    p._backend.close()

    #No adaptive sampling.
    p = plot_parametric(cos(x), sin(x), adaptive=False, nb_of_points=500)
    p.save(tmp_file('%s_adaptive' % name))
    p._backend.close()

    #3d parametric plots
    p = plot3d_parametric_line(sin(x), cos(x), x)
    p.save(tmp_file('%s_3d_line' % name))
    p._backend.close()

    p = plot3d_parametric_line((sin(x), cos(x), x, (x, -5, 5)),
                               (cos(x), sin(x), x, (x, -3, 3)))
    p.save(tmp_file('%s_3d_line_multiple' % name))
    p._backend.close()

    p = plot3d_parametric_line(sin(x), cos(x), x, nb_of_points=30)
    p.save(tmp_file('%s_3d_line_points' % name))
    p._backend.close()

    # 3d surface single plot.
    p = plot3d(x * y)
    p.save(tmp_file('%s_surface' % name))
    p._backend.close()

    # Multiple 3D plots with same range.
    p = plot3d(-x * y, x * y, (x, -5, 5))
    p.save(tmp_file('%s_surface_multiple' % name))
    p._backend.close()

    # Multiple 3D plots with different ranges.
    p = plot3d((x * y, (x, -3, 3), (y, -3, 3)),
               (-x * y, (x, -3, 3), (y, -3, 3)))
    p.save(tmp_file('%s_surface_multiple_ranges' % name))
    p._backend.close()

    # Single Parametric 3D plot
    p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y)
    p.save(tmp_file('%s_parametric_surface' % name))
    p._backend.close()

    # Multiple Parametric 3D plots.
    p = plot3d_parametric_surface(
        (x * sin(z), x * cos(z), z, (x, -5, 5), (z, -5, 5)),
        (sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5)))
    p.save(tmp_file('%s_parametric_surface' % name))
    p._backend.close()

    ###
    # Examples from the 'colors' notebook
    ###

    p = plot(sin(x))
    p[0].line_color = lambda a: a
    p.save(tmp_file('%s_colors_line_arity1' % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_line_arity2' % name))
    p._backend.close()

    p = plot(x * sin(x), x * cos(x), (x, 0, 10))
    p[0].line_color = lambda a: a
    p.save(tmp_file('%s_colors_param_line_arity1' % name))

    p[0].line_color = lambda a, b: a
    p.save(tmp_file('%s_colors_param_line_arity2a' % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_param_line_arity2b' % name))
    p._backend.close()

    p = plot3d_parametric_line(
        sin(x) + 0.1 * sin(x) * cos(7 * x),
        cos(x) + 0.1 * cos(x) * cos(7 * x), 0.1 * sin(7 * x), (x, 0, 2 * pi))
    p[0].line_color = lambdify_(x, sin(4 * x))
    p.save(tmp_file('%s_colors_3d_line_arity1' % name))
    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_3d_line_arity2' % name))
    p[0].line_color = lambda a, b, c: c
    p.save(tmp_file('%s_colors_3d_line_arity3' % name))
    p._backend.close()

    p = plot3d(sin(x) * y, (x, 0, 6 * pi), (y, -5, 5))
    p[0].surface_color = lambda a: a
    p.save(tmp_file('%s_colors_surface_arity1' % name))
    p[0].surface_color = lambda a, b: b
    p.save(tmp_file('%s_colors_surface_arity2' % name))
    p[0].surface_color = lambda a, b, c: c
    p.save(tmp_file('%s_colors_surface_arity3a' % name))
    p[0].surface_color = lambdify_((x, y, z), sqrt((x - 3 * pi)**2 + y**2))
    p.save(tmp_file('%s_colors_surface_arity3b' % name))
    p._backend.close()

    p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y,
                                  (x, -1, 1), (y, -1, 1))
    p[0].surface_color = lambda a: a
    p.save(tmp_file('%s_colors_param_surf_arity1' % name))
    p[0].surface_color = lambda a, b: a * b
    p.save(tmp_file('%s_colors_param_surf_arity2' % name))
    p[0].surface_color = lambdify_((x, y, z), sqrt(x**2 + y**2 + z**2))
    p.save(tmp_file('%s_colors_param_surf_arity3' % name))
    p._backend.close()

    ###
    # Examples from the 'advanced' notebook
    ###

    # XXX: This raises the warning "The evaluation of the expression is
    # problematic. We are trying a failback method that may still work. Please
    # report this as a bug." It has to use the fallback because using evalf()
    # is the only way to evaluate the integral. We should perhaps just remove
    # that warning.

    with warnings.catch_warnings(record=True) as w:
        i = Integral(log((sin(x)**2 + 1) * sqrt(x**2 + 1)), (x, 0, y))
        p = plot(i, (y, 1, 5))
        p.save(tmp_file('%s_advanced_integral' % name))
        p._backend.close()
        # Make sure no other warnings were raised
        assert len(w) == 1
        assert issubclass(w[-1].category, UserWarning)
        assert "The evaluation of the expression is problematic" in str(
            w[0].message)

    s = Sum(1 / x**y, (x, 1, oo))
    p = plot(s, (y, 2, 10))
    p.save(tmp_file('%s_advanced_inf_sum' % name))
    p._backend.close()

    p = plot(Sum(1 / x, (x, 1, y)), (y, 2, 10), show=False)
    p[0].only_integers = True
    p[0].steps = True
    p.save(tmp_file('%s_advanced_fin_sum' % name))
    p._backend.close()

    ###
    # Test expressions that can not be translated to np and generate complex
    # results.
    ###
    plot(sin(x) + I * cos(x)).save(tmp_file())
    plot(sqrt(sqrt(-x))).save(tmp_file())
    plot(LambertW(x)).save(tmp_file())
    plot(sqrt(LambertW(x))).save(tmp_file())

    #Characteristic function of a StudentT distribution with nu=10
    plot((meijerg(
        ((1 / 2, ), ()),
        ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(-I * pi) / 2) + meijerg(
            ((1 / 2, ), ()),
            ((5, 0, 1 / 2),
             ()), 5 * x**2 * exp_polar(I * pi) / 2)) / (48 * pi),
         (x, 1e-6, 1e-2)).save(tmp_file())
Exemple #23
0
def plot_and_save(name):
    tmp_file = TmpFileManager.tmp_file

    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')

    ###
    # Examples from the 'introduction' notebook
    ###

    p = plot(x)
    p = plot(x*sin(x), x*cos(x))
    p.extend(p)
    p[0].line_color = lambda a: a
    p[1].line_color = 'b'
    p.title = 'Big title'
    p.xlabel = 'the x axis'
    p[1].label = 'straight line'
    p.legend = True
    p.aspect_ratio = (1, 1)
    p.xlim = (-15, 20)
    p.save(tmp_file('%s_basic_options_and_colors' % name))
    p._backend.close()

    p.extend(plot(x + 1))
    p.append(plot(x + 3, x**2)[1])
    p.save(tmp_file('%s_plot_extend_append' % name))

    p[2] = plot(x**2, (x, -2, 3))
    p.save(tmp_file('%s_plot_setitem' % name))
    p._backend.close()

    p = plot(sin(x), (x, -2*pi, 4*pi))
    p.save(tmp_file('%s_line_explicit' % name))
    p._backend.close()

    p = plot(sin(x))
    p.save(tmp_file('%s_line_default_range' % name))
    p._backend.close()

    p = plot((x**2, (x, -5, 5)), (x**3, (x, -3, 3)))
    p.save(tmp_file('%s_line_multiple_range' % name))
    p._backend.close()

    raises(ValueError, lambda: plot(x, y))

    p = plot(Piecewise((1, x > 0), (0, True)),(x,-1,1))
    p.save(tmp_file('%s_plot_piecewise' % name))
    p._backend.close()

    #parametric 2d plots.
    #Single plot with default range.
    plot_parametric(sin(x), cos(x)).save(tmp_file())

    #Single plot with range.
    p = plot_parametric(sin(x), cos(x), (x, -5, 5))
    p.save(tmp_file('%s_parametric_range' % name))
    p._backend.close()

    #Multiple plots with same range.
    p = plot_parametric((sin(x), cos(x)), (x, sin(x)))
    p.save(tmp_file('%s_parametric_multiple' % name))
    p._backend.close()

    #Multiple plots with different ranges.
    p = plot_parametric((sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5)))
    p.save(tmp_file('%s_parametric_multiple_ranges' % name))
    p._backend.close()

    #depth of recursion specified.
    p = plot_parametric(x, sin(x), depth=13)
    p.save(tmp_file('%s_recursion_depth' % name))
    p._backend.close()

    #No adaptive sampling.
    p = plot_parametric(cos(x), sin(x), adaptive=False, nb_of_points=500)
    p.save(tmp_file('%s_adaptive' % name))
    p._backend.close()

    #3d parametric plots
    p = plot3d_parametric_line(sin(x), cos(x), x)
    p.save(tmp_file('%s_3d_line' % name))
    p._backend.close()

    p = plot3d_parametric_line(
        (sin(x), cos(x), x, (x, -5, 5)), (cos(x), sin(x), x, (x, -3, 3)))
    p.save(tmp_file('%s_3d_line_multiple' % name))
    p._backend.close()

    p = plot3d_parametric_line(sin(x), cos(x), x, nb_of_points=30)
    p.save(tmp_file('%s_3d_line_points' % name))
    p._backend.close()

    # 3d surface single plot.
    p = plot3d(x * y)
    p.save(tmp_file('%s_surface' % name))
    p._backend.close()

    # Multiple 3D plots with same range.
    p = plot3d(-x * y, x * y, (x, -5, 5))
    p.save(tmp_file('%s_surface_multiple' % name))
    p._backend.close()

    # Multiple 3D plots with different ranges.
    p = plot3d(
        (x * y, (x, -3, 3), (y, -3, 3)), (-x * y, (x, -3, 3), (y, -3, 3)))
    p.save(tmp_file('%s_surface_multiple_ranges' % name))
    p._backend.close()

    # Single Parametric 3D plot
    p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y)
    p.save(tmp_file('%s_parametric_surface' % name))
    p._backend.close()

    # Multiple Parametric 3D plots.
    p = plot3d_parametric_surface(
        (x*sin(z), x*cos(z), z, (x, -5, 5), (z, -5, 5)),
        (sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5)))
    p.save(tmp_file('%s_parametric_surface' % name))
    p._backend.close()

    ###
    # Examples from the 'colors' notebook
    ###

    p = plot(sin(x))
    p[0].line_color = lambda a: a
    p.save(tmp_file('%s_colors_line_arity1' % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_line_arity2' % name))
    p._backend.close()

    p = plot(x*sin(x), x*cos(x), (x, 0, 10))
    p[0].line_color = lambda a: a
    p.save(tmp_file('%s_colors_param_line_arity1' % name))

    p[0].line_color = lambda a, b: a
    p.save(tmp_file('%s_colors_param_line_arity2a' % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_param_line_arity2b' % name))
    p._backend.close()

    p = plot3d_parametric_line(sin(x) + 0.1*sin(x)*cos(7*x),
             cos(x) + 0.1*cos(x)*cos(7*x),
        0.1*sin(7*x),
        (x, 0, 2*pi))
    p[0].line_color = lambda a: sin(4*a)
    p.save(tmp_file('%s_colors_3d_line_arity1' % name))
    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_3d_line_arity2' % name))
    p[0].line_color = lambda a, b, c: c
    p.save(tmp_file('%s_colors_3d_line_arity3' % name))
    p._backend.close()

    p = plot3d(sin(x)*y, (x, 0, 6*pi), (y, -5, 5))
    p[0].surface_color = lambda a: a
    p.save(tmp_file('%s_colors_surface_arity1' % name))
    p[0].surface_color = lambda a, b: b
    p.save(tmp_file('%s_colors_surface_arity2' % name))
    p[0].surface_color = lambda a, b, c: c
    p.save(tmp_file('%s_colors_surface_arity3a' % name))
    p[0].surface_color = lambda a, b, c: sqrt((a - 3*pi)**2 + b**2)
    p.save(tmp_file('%s_colors_surface_arity3b' % name))
    p._backend.close()

    p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y,
             (x, -1, 1), (y, -1, 1))
    p[0].surface_color = lambda a: a
    p.save(tmp_file('%s_colors_param_surf_arity1' % name))
    p[0].surface_color = lambda a, b: a*b
    p.save(tmp_file('%s_colors_param_surf_arity2' % name))
    p[0].surface_color = lambda a, b, c: sqrt(a**2 + b**2 + c**2)
    p.save(tmp_file('%s_colors_param_surf_arity3' % name))
    p._backend.close()

    ###
    # Examples from the 'advanced' notebook
    ###

    i = Integral(log((sin(x)**2 + 1)*sqrt(x**2 + 1)), (x, 0, y))
    p = plot(i, (y, 1, 5))
    p.save(tmp_file('%s_advanced_integral' % name))
    p._backend.close()

    s = Sum(1/x**y, (x, 1, oo))
    p = plot(s, (y, 2, 10))
    p.save(tmp_file('%s_advanced_inf_sum' % name))
    p._backend.close()

    p = plot(Sum(1/x, (x, 1, y)), (y, 2, 10), show=False)
    p[0].only_integers = True
    p[0].steps = True
    p.save(tmp_file('%s_advanced_fin_sum' % name))
    p._backend.close()

    ###
    # Test expressions that can not be translated to np and generate complex
    # results.
    ###
    plot(sin(x) + I*cos(x)).save(tmp_file())
    plot(sqrt(sqrt(-x))).save(tmp_file())
    plot(LambertW(x)).save(tmp_file())
    plot(sqrt(LambertW(x))).save(tmp_file())

    #Characteristic function of a StudentT distribution with nu=10
    plot((meijerg(((1 / 2,), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(-I*pi)/2)
            + meijerg(((1/2,), ()), ((5, 0, 1/2), ()),
                5*x**2 * exp_polar(I*pi)/2)) / (48 * pi), (x, 1e-6, 1e-2)).save(tmp_file())
msg =  "\tMenu\n\n"
msg += "0. EXIT\n"
msg += "1. Show graph for f1\n"
msg += "2. Show graph for the first Taylor polynomial of f1\n"
msg += "3. Show graph for the second Taylor polynomial of f1\n"
msg += "4. Show graph for f1, T1 of f1 and T2 of f1 together.\n"
msg += "5. Show graph for f2\n"
msg += "6. Show graph for the first Taylor polynomial of f2\n"
msg += "7. Show graph for the second Taylor polynomial of f2\n"
msg += "8. Show graph for f1, T1 of f2 and T2 of f2 together.\n"

print msg
while True:	
	cmd = raw_input("Enter command: ").strip()
	if(cmd == "1"):
		plot3d(f1, (x, -1, 1), (y, -1, 1), title = "f1")
	elif(cmd == "2"):
		plot3d(T1f1, (x, -1, 1), (y, -1, 1), title ="T1(x,y)  of f1")
	elif(cmd == "3"):
		plot3d(T2f1, (x, -1, 1), (y, -1, 1), title ="T2(x,y) of f1")
	elif(cmd == "4"):
		plot3d(f1, T1f1, T2f1, (x, -1, 1), (y, -1, 1), title = "f1, T1(x,y) and T2(x,y)")
	elif(cmd == "5"):
		plot3d(f2, (x, -1, 1), (y, -1, 1), title = "f2")
	elif(cmd == "6"):
		plot3d(T1f2, (x, -1, 1), (y, -1, 1), title ="T1(x,y) of f2")
	elif(cmd == "7"):
		plot3d(T2f2, (x, -1, 1), (y, -1, 1), title ="T2(x,y) of f2")
	elif(cmd == "8"):
		plot3d(f2, T1f2, T2f2, (x, -1, 1), (y, -1, 1), title = "f2, T1(x,y) and T2(x,y)")
	elif(cmd == "0"):
lx = range(5)
ly = [i**2 for i in lx]

x = Symbol('x')
y = Symbol('y')
u = Symbol('u')
v = Symbol('v')
expr = x**2 - 1

b = plot(expr, (x, 2, 4), show=False)  # cartesian plot
e = plot(exp(-x), (x, 0, 4),
         show=False)  # cartesian plot (and coloring, see below)
f = plot3d_parametric_line(sin(x), cos(x), x, (x, 0, 10),
                           show=False)  # 3d parametric line plot
g = plot3d(sin(x) * cos(y), (x, -5, 5), (y, -10, 10),
           show=False)  # 3d surface cartesian plot
h = plot3d_parametric_surface(cos(u) * v,
                              sin(u) * v,
                              u, (u, 0, 10), (v, -2, 2),
                              show=False)  # 3d parametric surface plot

# Some aesthetics
e[0].line_color = lambda x: x / 4
f[0].line_color = lambda x, y, z: z / 10
g[0].surface_color = lambda x, y: sin(x)

# Some more stuff on aesthetics - coloring wrt coordinates or parameters
param_line_2d = plot_parametric(
    (x * cos(x), x * sin(x), (x, 0, 15)),
    (1.1 * x * cos(x), 1.1 * x * sin(x), (x, 0, 15)),
    show=False)
Exemple #26
0
def plot_and_save_2(name):
    tmp_file = TmpFileManager.tmp_file

    x = Symbol("x")
    y = Symbol("y")
    z = Symbol("z")

    # parametric 2d plots.
    # Single plot with default range.
    plot_parametric(sin(x), cos(x)).save(tmp_file())

    # Single plot with range.
    p = plot_parametric(sin(x), cos(x), (x, -5, 5))
    p.save(tmp_file("%s_parametric_range" % name))
    p._backend.close()

    # Multiple plots with same range.
    p = plot_parametric((sin(x), cos(x)), (x, sin(x)))
    p.save(tmp_file("%s_parametric_multiple" % name))
    p._backend.close()

    # Multiple plots with different ranges.
    p = plot_parametric((sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5)))
    p.save(tmp_file("%s_parametric_multiple_ranges" % name))
    p._backend.close()

    # depth of recursion specified.
    p = plot_parametric(x, sin(x), depth=13)
    p.save(tmp_file("%s_recursion_depth" % name))
    p._backend.close()

    # No adaptive sampling.
    p = plot_parametric(cos(x), sin(x), adaptive=False, nb_of_points=500)
    p.save(tmp_file("%s_adaptive" % name))
    p._backend.close()

    # 3d parametric plots
    p = plot3d_parametric_line(sin(x), cos(x), x)
    p.save(tmp_file("%s_3d_line" % name))
    p._backend.close()

    p = plot3d_parametric_line((sin(x), cos(x), x, (x, -5, 5)),
                               (cos(x), sin(x), x, (x, -3, 3)))
    p.save(tmp_file("%s_3d_line_multiple" % name))
    p._backend.close()

    p = plot3d_parametric_line(sin(x), cos(x), x, nb_of_points=30)
    p.save(tmp_file("%s_3d_line_points" % name))
    p._backend.close()

    # 3d surface single plot.
    p = plot3d(x * y)
    p.save(tmp_file("%s_surface" % name))
    p._backend.close()

    # Multiple 3D plots with same range.
    p = plot3d(-x * y, x * y, (x, -5, 5))
    p.save(tmp_file("%s_surface_multiple" % name))
    p._backend.close()

    # Multiple 3D plots with different ranges.
    p = plot3d((x * y, (x, -3, 3), (y, -3, 3)),
               (-x * y, (x, -3, 3), (y, -3, 3)))
    p.save(tmp_file("%s_surface_multiple_ranges" % name))
    p._backend.close()

    # Single Parametric 3D plot
    p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y)
    p.save(tmp_file("%s_parametric_surface" % name))
    p._backend.close()

    # Multiple Parametric 3D plots.
    p = plot3d_parametric_surface(
        (x * sin(z), x * cos(z), z, (x, -5, 5), (z, -5, 5)),
        (sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5)),
    )
    p.save(tmp_file("%s_parametric_surface" % name))
    p._backend.close()

    # Single Contour plot.
    p = plot_contour(sin(x) * sin(y), (x, -5, 5), (y, -5, 5))
    p.save(tmp_file("%s_contour_plot" % name))
    p._backend.close()

    # Multiple Contour plots with same range.
    p = plot_contour(x**2 + y**2, x**3 + y**3, (x, -5, 5), (y, -5, 5))
    p.save(tmp_file("%s_contour_plot" % name))
    p._backend.close()

    # Multiple Contour plots with different range.
    p = plot_contour(
        (x**2 + y**2, (x, -5, 5), (y, -5, 5)),
        (x**3 + y**3, (x, -3, 3), (y, -3, 3)),
    )
    p.save(tmp_file("%s_contour_plot" % name))
    p._backend.close()
Exemple #27
0
def plot_and_save_3(name):
    tmp_file = TmpFileManager.tmp_file

    x = Symbol("x")
    y = Symbol("y")
    z = Symbol("z")

    ###
    # Examples from the 'colors' notebook
    ###

    p = plot(sin(x))
    p[0].line_color = lambda a: a
    p.save(tmp_file("%s_colors_line_arity1" % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file("%s_colors_line_arity2" % name))
    p._backend.close()

    p = plot(x * sin(x), x * cos(x), (x, 0, 10))
    p[0].line_color = lambda a: a
    p.save(tmp_file("%s_colors_param_line_arity1" % name))

    p[0].line_color = lambda a, b: a
    p.save(tmp_file("%s_colors_param_line_arity2a" % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file("%s_colors_param_line_arity2b" % name))
    p._backend.close()

    p = plot3d_parametric_line(
        sin(x) + 0.1 * sin(x) * cos(7 * x),
        cos(x) + 0.1 * cos(x) * cos(7 * x),
        0.1 * sin(7 * x),
        (x, 0, 2 * pi),
    )
    p[0].line_color = lambdify_(x, sin(4 * x))
    p.save(tmp_file("%s_colors_3d_line_arity1" % name))
    p[0].line_color = lambda a, b: b
    p.save(tmp_file("%s_colors_3d_line_arity2" % name))
    p[0].line_color = lambda a, b, c: c
    p.save(tmp_file("%s_colors_3d_line_arity3" % name))
    p._backend.close()

    p = plot3d(sin(x) * y, (x, 0, 6 * pi), (y, -5, 5))
    p[0].surface_color = lambda a: a
    p.save(tmp_file("%s_colors_surface_arity1" % name))
    p[0].surface_color = lambda a, b: b
    p.save(tmp_file("%s_colors_surface_arity2" % name))
    p[0].surface_color = lambda a, b, c: c
    p.save(tmp_file("%s_colors_surface_arity3a" % name))
    p[0].surface_color = lambdify_((x, y, z), sqrt((x - 3 * pi)**2 + y**2))
    p.save(tmp_file("%s_colors_surface_arity3b" % name))
    p._backend.close()

    p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y,
                                  (x, -1, 1), (y, -1, 1))
    p[0].surface_color = lambda a: a
    p.save(tmp_file("%s_colors_param_surf_arity1" % name))
    p[0].surface_color = lambda a, b: a * b
    p.save(tmp_file("%s_colors_param_surf_arity2" % name))
    p[0].surface_color = lambdify_((x, y, z), sqrt(x**2 + y**2 + z**2))
    p.save(tmp_file("%s_colors_param_surf_arity3" % name))
    p._backend.close()
# -*- coding: utf-8 -*-
from sympy import Matrix, solve, det, latex
from sympy.abc import x,y,z
from sympy.plotting import plot3d

P1 = (1,2,3)
P2 = (0,-1,1)
P3 = (-2,1,-2)

M = Matrix([[x-P1[0]     , y-P1[1]     , z-P1[2]]    ,
		    [P2[0]-P1[0] , P2[1]-P1[1] , P2[2]-P1[2]],
		    [P3[0]-P1[0] , P3[1]-P1[1] , P3[2]-P1[2]]])

sol = solve(det(M), z)
print(u"Ecuación implícita: %s = 0"%det(M))
print(u"Ecuación explícita: z=%s"%(sol[0]))
h = plot3d(sol[0], (x,0,5), (y,0,5), title="$z = %s$"%(latex(sol[0])))
h.save("img_01.png")
h.show()
x3_2_plot = sym.plot(x3[1], (t, 0, 6),
                     label='om=2',
                     line_color='r',
                     legend=True,
                     show=False)
x3_1_plot.extend(x3_2_plot)

x3_3_plot = sym.plot(x3[2], (t, 0, 6),
                     label='om=3',
                     line_color='g',
                     legend=True,
                     show=False)
x3_1_plot.extend(x3_3_plot)

x3_1_plot.title = 'Зависимость координаты материальной точки x от времени t'
x3_1_plot.xlabel = 't, с'
x3_1_plot.ylabel = 'x, м'
x3_1_plot.show()

A_expr = 1 / (sym.sqrt((omega0**2 - om**2)**2 + 4 * delta0**2 * om**2))
fi_expr = sym.atan(2 * delta0 * om / (omega0**2 - om**2))
x3_expr = A_expr * sym.cos(t * om + fi_expr)

x3_plot3d = symplot.plot3d(x3_expr, (t, 0, 6),
                           (om, om_val[0], om_val[len(om_val) - 1]),
                           show=False)
x3_plot3d.title = ('Зависимость коорд. мат. точки x '
                   'от времени t и вынуждающей силы omega')
x3_plot3d.xlabel = 't, с'
x3_plot3d.ylabel = 'omega'
x3_plot3d.show()
Exemple #30
0
def plot_and_save_2(name):
    tmp_file = TmpFileManager.tmp_file

    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')

    #parametric 2d plots.
    #Single plot with default range.
    plot_parametric(sin(x), cos(x)).save(tmp_file())

    #Single plot with range.
    p = plot_parametric(sin(x), cos(x), (x, -5, 5))
    p.save(tmp_file('%s_parametric_range' % name))
    p._backend.close()

    #Multiple plots with same range.
    p = plot_parametric((sin(x), cos(x)), (x, sin(x)))
    p.save(tmp_file('%s_parametric_multiple' % name))
    p._backend.close()

    #Multiple plots with different ranges.
    p = plot_parametric((sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5)))
    p.save(tmp_file('%s_parametric_multiple_ranges' % name))
    p._backend.close()

    #depth of recursion specified.
    p = plot_parametric(x, sin(x), depth=13)
    p.save(tmp_file('%s_recursion_depth' % name))
    p._backend.close()

    #No adaptive sampling.
    p = plot_parametric(cos(x), sin(x), adaptive=False, nb_of_points=500)
    p.save(tmp_file('%s_adaptive' % name))
    p._backend.close()

    #3d parametric plots
    p = plot3d_parametric_line(sin(x), cos(x), x)
    p.save(tmp_file('%s_3d_line' % name))
    p._backend.close()

    p = plot3d_parametric_line(
        (sin(x), cos(x), x, (x, -5, 5)), (cos(x), sin(x), x, (x, -3, 3)))
    p.save(tmp_file('%s_3d_line_multiple' % name))
    p._backend.close()

    p = plot3d_parametric_line(sin(x), cos(x), x, nb_of_points=30)
    p.save(tmp_file('%s_3d_line_points' % name))
    p._backend.close()

    # 3d surface single plot.
    p = plot3d(x * y)
    p.save(tmp_file('%s_surface' % name))
    p._backend.close()

    # Multiple 3D plots with same range.
    p = plot3d(-x * y, x * y, (x, -5, 5))
    p.save(tmp_file('%s_surface_multiple' % name))
    p._backend.close()

    # Multiple 3D plots with different ranges.
    p = plot3d(
        (x * y, (x, -3, 3), (y, -3, 3)), (-x * y, (x, -3, 3), (y, -3, 3)))
    p.save(tmp_file('%s_surface_multiple_ranges' % name))
    p._backend.close()

    # Single Parametric 3D plot
    p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y)
    p.save(tmp_file('%s_parametric_surface' % name))
    p._backend.close()

    # Multiple Parametric 3D plots.
    p = plot3d_parametric_surface(
        (x*sin(z), x*cos(z), z, (x, -5, 5), (z, -5, 5)),
        (sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5)))
    p.save(tmp_file('%s_parametric_surface' % name))
    p._backend.close()

    # Single Contour plot.
    p = plot_contour(sin(x)*sin(y), (x, -5, 5), (y, -5, 5))
    p.save(tmp_file('%s_contour_plot' % name))
    p._backend.close()

    # Multiple Contour plots with same range.
    p = plot_contour(x**2 + y**2, x**3 + y**3, (x, -5, 5), (y, -5, 5))
    p.save(tmp_file('%s_contour_plot' % name))
    p._backend.close()

    # Multiple Contour plots with different range.
    p = plot_contour((x**2 + y**2, (x, -5, 5), (y, -5, 5)), (x**3 + y**3, (x, -3, 3), (y, -3, 3)))
    p.save(tmp_file('%s_contour_plot' % name))
    p._backend.close()
Exemple #31
0
def plot_and_save(name):
    tmp_file = TmpFileManager.tmp_file

    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')

    ###
    # Examples from the 'introduction' notebook
    ###

    p = plot(x)
    p = plot(x * sin(x), x * cos(x))
    p.extend(p)
    p[0].line_color = lambda a: a
    p[1].line_color = 'b'
    p.title = 'Big title'
    p.xlabel = 'the x axis'
    p[1].label = 'straight line'
    p.legend = True
    p.aspect_ratio = (1, 1)
    p.xlim = (-15, 20)
    p.save(tmp_file('%s_basic_options_and_colors' % name))
    p._backend.close()

    p.extend(plot(x + 1))
    p.append(plot(x + 3, x**2)[1])
    p.save(tmp_file('%s_plot_extend_append' % name))

    p[2] = plot(x**2, (x, -2, 3))
    p.save(tmp_file('%s_plot_setitem' % name))
    p._backend.close()

    p = plot(sin(x), (x, -2 * pi, 4 * pi))
    p.save(tmp_file('%s_line_explicit' % name))
    p._backend.close()

    p = plot(sin(x))
    p.save(tmp_file('%s_line_default_range' % name))
    p._backend.close()

    p = plot((x**2, (x, -5, 5)), (x**3, (x, -3, 3)))
    p.save(tmp_file('%s_line_multiple_range' % name))
    p._backend.close()

    raises(ValueError, lambda: plot(x, y))

    p = plot(Piecewise((1, x > 0), (0, True)), (x, -1, 1))
    p.save(tmp_file('%s_plot_piecewise' % name))
    p._backend.close()

    #parametric 2d plots.
    #Single plot with default range.
    plot_parametric(sin(x), cos(x)).save(tmp_file())

    #Single plot with range.
    p = plot_parametric(sin(x), cos(x), (x, -5, 5))
    p.save(tmp_file('%s_parametric_range' % name))
    p._backend.close()

    #Multiple plots with same range.
    p = plot_parametric((sin(x), cos(x)), (x, sin(x)))
    p.save(tmp_file('%s_parametric_multiple' % name))
    p._backend.close()

    #Multiple plots with different ranges.
    p = plot_parametric((sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5)))
    p.save(tmp_file('%s_parametric_multiple_ranges' % name))
    p._backend.close()

    #depth of recursion specified.
    p = plot_parametric(x, sin(x), depth=13)
    p.save(tmp_file('%s_recursion_depth' % name))
    p._backend.close()

    #No adaptive sampling.
    p = plot_parametric(cos(x), sin(x), adaptive=False, nb_of_points=500)
    p.save(tmp_file('%s_adaptive' % name))
    p._backend.close()

    #3d parametric plots
    p = plot3d_parametric_line(sin(x), cos(x), x)
    p.save(tmp_file('%s_3d_line' % name))
    p._backend.close()

    p = plot3d_parametric_line((sin(x), cos(x), x, (x, -5, 5)),
                               (cos(x), sin(x), x, (x, -3, 3)))
    p.save(tmp_file('%s_3d_line_multiple' % name))
    p._backend.close()

    p = plot3d_parametric_line(sin(x), cos(x), x, nb_of_points=30)
    p.save(tmp_file('%s_3d_line_points' % name))
    p._backend.close()

    # 3d surface single plot.
    p = plot3d(x * y)
    p.save(tmp_file('%s_surface' % name))
    p._backend.close()

    # Multiple 3D plots with same range.
    p = plot3d(-x * y, x * y, (x, -5, 5))
    p.save(tmp_file('%s_surface_multiple' % name))
    p._backend.close()

    # Multiple 3D plots with different ranges.
    p = plot3d((x * y, (x, -3, 3), (y, -3, 3)),
               (-x * y, (x, -3, 3), (y, -3, 3)))
    p.save(tmp_file('%s_surface_multiple_ranges' % name))
    p._backend.close()

    # Single Parametric 3D plot
    p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y)
    p.save(tmp_file('%s_parametric_surface' % name))
    p._backend.close()

    # Multiple Parametric 3D plots.
    p = plot3d_parametric_surface(
        (x * sin(z), x * cos(z), z, (x, -5, 5), (z, -5, 5)),
        (sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5)))
    p.save(tmp_file('%s_parametric_surface' % name))
    p._backend.close()

    ###
    # Examples from the 'colors' notebook
    ###

    p = plot(sin(x))
    p[0].line_color = lambda a: a
    p.save(tmp_file('%s_colors_line_arity1' % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_line_arity2' % name))
    p._backend.close()

    p = plot(x * sin(x), x * cos(x), (x, 0, 10))
    p[0].line_color = lambda a: a
    p.save(tmp_file('%s_colors_param_line_arity1' % name))

    p[0].line_color = lambda a, b: a
    p.save(tmp_file('%s_colors_param_line_arity2a' % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_param_line_arity2b' % name))
    p._backend.close()

    p = plot3d_parametric_line(
        sin(x) + 0.1 * sin(x) * cos(7 * x),
        cos(x) + 0.1 * cos(x) * cos(7 * x), 0.1 * sin(7 * x), (x, 0, 2 * pi))
    p[0].line_color = lambda a: sin(4 * a)
    p.save(tmp_file('%s_colors_3d_line_arity1' % name))
    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_3d_line_arity2' % name))
    p[0].line_color = lambda a, b, c: c
    p.save(tmp_file('%s_colors_3d_line_arity3' % name))
    p._backend.close()

    p = plot3d(sin(x) * y, (x, 0, 6 * pi), (y, -5, 5))
    p[0].surface_color = lambda a: a
    p.save(tmp_file('%s_colors_surface_arity1' % name))
    p[0].surface_color = lambda a, b: b
    p.save(tmp_file('%s_colors_surface_arity2' % name))
    p[0].surface_color = lambda a, b, c: c
    p.save(tmp_file('%s_colors_surface_arity3a' % name))
    p[0].surface_color = lambda a, b, c: sqrt((a - 3 * pi)**2 + b**2)
    p.save(tmp_file('%s_colors_surface_arity3b' % name))
    p._backend.close()

    p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y,
                                  (x, -1, 1), (y, -1, 1))
    p[0].surface_color = lambda a: a
    p.save(tmp_file('%s_colors_param_surf_arity1' % name))
    p[0].surface_color = lambda a, b: a * b
    p.save(tmp_file('%s_colors_param_surf_arity2' % name))
    p[0].surface_color = lambda a, b, c: sqrt(a**2 + b**2 + c**2)
    p.save(tmp_file('%s_colors_param_surf_arity3' % name))
    p._backend.close()

    ###
    # Examples from the 'advanced' notebook
    ###

    i = Integral(log((sin(x)**2 + 1) * sqrt(x**2 + 1)), (x, 0, y))
    p = plot(i, (y, 1, 5))
    p.save(tmp_file('%s_advanced_integral' % name))
    p._backend.close()

    s = Sum(1 / x**y, (x, 1, oo))
    p = plot(s, (y, 2, 10))
    p.save(tmp_file('%s_advanced_inf_sum' % name))
    p._backend.close()

    p = plot(Sum(1 / x, (x, 1, y)), (y, 2, 10), show=False)
    p[0].only_integers = True
    p[0].steps = True
    p.save(tmp_file('%s_advanced_fin_sum' % name))
    p._backend.close()

    ###
    # Test expressions that can not be translated to np and generate complex
    # results.
    ###
    plot(sin(x) + I * cos(x)).save(tmp_file())
    plot(sqrt(sqrt(-x))).save(tmp_file())
    plot(LambertW(x)).save(tmp_file())
    plot(sqrt(LambertW(x))).save(tmp_file())

    #Characteristic function of a StudentT distribution with nu=10
    plot((meijerg(
        ((1 / 2, ), ()),
        ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(-I * pi) / 2) + meijerg(
            ((1 / 2, ), ()),
            ((5, 0, 1 / 2),
             ()), 5 * x**2 * exp_polar(I * pi) / 2)) / (48 * pi),
         (x, 1e-6, 1e-2)).save(tmp_file())
Exemple #32
0
def plot_and_save(name):
    tmp_file = TmpFileManager.tmp_file

    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')

    ###
    # Examples from the 'introduction' notebook
    ###

    p = plot(x)
    p = plot(x*sin(x), x*cos(x))
    p.extend(p)
    p[0].line_color = lambda a: a
    p[1].line_color = 'b'
    p.title = 'Big title'
    p.xlabel = 'the x axis'
    p[1].label = 'straight line'
    p.legend = True
    p.aspect_ratio = (1, 1)
    p.xlim = (-15, 20)
    p.save(tmp_file('%s_basic_options_and_colors' % name))
    p._backend.close()

    p.extend(plot(x + 1))
    p.append(plot(x + 3, x**2)[1])
    p.save(tmp_file('%s_plot_extend_append' % name))

    p[2] = plot(x**2, (x, -2, 3))
    p.save(tmp_file('%s_plot_setitem' % name))
    p._backend.close()

    p = plot(sin(x), (x, -2*pi, 4*pi))
    p.save(tmp_file('%s_line_explicit' % name))
    p._backend.close()

    p = plot(sin(x))
    p.save(tmp_file('%s_line_default_range' % name))
    p._backend.close()

    p = plot((x**2, (x, -5, 5)), (x**3, (x, -3, 3)))
    p.save(tmp_file('%s_line_multiple_range' % name))
    p._backend.close()

    raises(ValueError, lambda: plot(x, y))

    #Piecewise plots
    p = plot(Piecewise((1, x > 0), (0, True)), (x, -1, 1))
    p.save(tmp_file('%s_plot_piecewise' % name))
    p._backend.close()

    p = plot(Piecewise((x, x < 1), (x**2, True)), (x, -3, 3))
    p.save(tmp_file('%s_plot_piecewise_2' % name))
    p._backend.close()

    # test issue 7471
    p1 = plot(x)
    p2 = plot(3)
    p1.extend(p2)
    p.save(tmp_file('%s_horizontal_line' % name))
    p._backend.close()

    # test issue 10925
    f = Piecewise((-1, x < -1), (x, And(-1 <= x, x < 0)), \
        (x**2, And(0 <= x, x < 1)), (x**3, x >= 1))
    p = plot(f, (x, -3, 3))
    p.save(tmp_file('%s_plot_piecewise_3' % name))
    p._backend.close()

    #parametric 2d plots.
    #Single plot with default range.
    plot_parametric(sin(x), cos(x)).save(tmp_file())

    #Single plot with range.
    p = plot_parametric(sin(x), cos(x), (x, -5, 5))
    p.save(tmp_file('%s_parametric_range' % name))
    p._backend.close()

    #Multiple plots with same range.
    p = plot_parametric((sin(x), cos(x)), (x, sin(x)))
    p.save(tmp_file('%s_parametric_multiple' % name))
    p._backend.close()

    #Multiple plots with different ranges.
    p = plot_parametric((sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5)))
    p.save(tmp_file('%s_parametric_multiple_ranges' % name))
    p._backend.close()

    #depth of recursion specified.
    p = plot_parametric(x, sin(x), depth=13)
    p.save(tmp_file('%s_recursion_depth' % name))
    p._backend.close()

    #No adaptive sampling.
    p = plot_parametric(cos(x), sin(x), adaptive=False, nb_of_points=500)
    p.save(tmp_file('%s_adaptive' % name))
    p._backend.close()

    #3d parametric plots
    p = plot3d_parametric_line(sin(x), cos(x), x)
    p.save(tmp_file('%s_3d_line' % name))
    p._backend.close()

    p = plot3d_parametric_line(
        (sin(x), cos(x), x, (x, -5, 5)), (cos(x), sin(x), x, (x, -3, 3)))
    p.save(tmp_file('%s_3d_line_multiple' % name))
    p._backend.close()

    p = plot3d_parametric_line(sin(x), cos(x), x, nb_of_points=30)
    p.save(tmp_file('%s_3d_line_points' % name))
    p._backend.close()

    # 3d surface single plot.
    p = plot3d(x * y)
    p.save(tmp_file('%s_surface' % name))
    p._backend.close()

    # Multiple 3D plots with same range.
    p = plot3d(-x * y, x * y, (x, -5, 5))
    p.save(tmp_file('%s_surface_multiple' % name))
    p._backend.close()

    # Multiple 3D plots with different ranges.
    p = plot3d(
        (x * y, (x, -3, 3), (y, -3, 3)), (-x * y, (x, -3, 3), (y, -3, 3)))
    p.save(tmp_file('%s_surface_multiple_ranges' % name))
    p._backend.close()

    # Single Parametric 3D plot
    p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y)
    p.save(tmp_file('%s_parametric_surface' % name))
    p._backend.close()

    # Multiple Parametric 3D plots.
    p = plot3d_parametric_surface(
        (x*sin(z), x*cos(z), z, (x, -5, 5), (z, -5, 5)),
        (sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5)))
    p.save(tmp_file('%s_parametric_surface' % name))
    p._backend.close()

    ###
    # Examples from the 'colors' notebook
    ###

    p = plot(sin(x))
    p[0].line_color = lambda a: a
    p.save(tmp_file('%s_colors_line_arity1' % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_line_arity2' % name))
    p._backend.close()

    p = plot(x*sin(x), x*cos(x), (x, 0, 10))
    p[0].line_color = lambda a: a
    p.save(tmp_file('%s_colors_param_line_arity1' % name))

    p[0].line_color = lambda a, b: a
    p.save(tmp_file('%s_colors_param_line_arity2a' % name))

    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_param_line_arity2b' % name))
    p._backend.close()

    p = plot3d_parametric_line(sin(x) + 0.1*sin(x)*cos(7*x),
             cos(x) + 0.1*cos(x)*cos(7*x),
        0.1*sin(7*x),
        (x, 0, 2*pi))
    p[0].line_color = lambdify_(x, sin(4*x))
    p.save(tmp_file('%s_colors_3d_line_arity1' % name))
    p[0].line_color = lambda a, b: b
    p.save(tmp_file('%s_colors_3d_line_arity2' % name))
    p[0].line_color = lambda a, b, c: c
    p.save(tmp_file('%s_colors_3d_line_arity3' % name))
    p._backend.close()

    p = plot3d(sin(x)*y, (x, 0, 6*pi), (y, -5, 5))
    p[0].surface_color = lambda a: a
    p.save(tmp_file('%s_colors_surface_arity1' % name))
    p[0].surface_color = lambda a, b: b
    p.save(tmp_file('%s_colors_surface_arity2' % name))
    p[0].surface_color = lambda a, b, c: c
    p.save(tmp_file('%s_colors_surface_arity3a' % name))
    p[0].surface_color = lambdify_((x, y, z), sqrt((x - 3*pi)**2 + y**2))
    p.save(tmp_file('%s_colors_surface_arity3b' % name))
    p._backend.close()

    p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y,
             (x, -1, 1), (y, -1, 1))
    p[0].surface_color = lambda a: a
    p.save(tmp_file('%s_colors_param_surf_arity1' % name))
    p[0].surface_color = lambda a, b: a*b
    p.save(tmp_file('%s_colors_param_surf_arity2' % name))
    p[0].surface_color = lambdify_((x, y, z), sqrt(x**2 + y**2 + z**2))
    p.save(tmp_file('%s_colors_param_surf_arity3' % name))
    p._backend.close()

    ###
    # Examples from the 'advanced' notebook
    ###

    # XXX: This raises the warning "The evaluation of the expression is
    # problematic. We are trying a failback method that may still work. Please
    # report this as a bug." It has to use the fallback because using evalf()
    # is the only way to evaluate the integral. We should perhaps just remove
    # that warning.

    with warnings.catch_warnings(record=True) as w:
        i = Integral(log((sin(x)**2 + 1)*sqrt(x**2 + 1)), (x, 0, y))
        p = plot(i, (y, 1, 5))
        p.save(tmp_file('%s_advanced_integral' % name))
        p._backend.close()
        # Make sure no other warnings were raised
        for i in w:
            assert issubclass(i.category, UserWarning)
            assert "The evaluation of the expression is problematic" in str(i.message)

    s = Sum(1/x**y, (x, 1, oo))
    p = plot(s, (y, 2, 10))
    p.save(tmp_file('%s_advanced_inf_sum' % name))
    p._backend.close()

    p = plot(Sum(1/x, (x, 1, y)), (y, 2, 10), show=False)
    p[0].only_integers = True
    p[0].steps = True
    p.save(tmp_file('%s_advanced_fin_sum' % name))
    p._backend.close()

    ###
    # Test expressions that can not be translated to np and generate complex
    # results.
    ###
    plot(sin(x) + I*cos(x)).save(tmp_file())
    plot(sqrt(sqrt(-x))).save(tmp_file())
    plot(LambertW(x)).save(tmp_file())
    plot(sqrt(LambertW(x))).save(tmp_file())

    #Characteristic function of a StudentT distribution with nu=10
    plot((meijerg(((1 / 2,), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(-I*pi)/2)
            + meijerg(((1/2,), ()), ((5, 0, 1/2), ()),
                5*x**2 * exp_polar(I*pi)/2)) / (48 * pi), (x, 1e-6, 1e-2)).save(tmp_file())
for i in range(0, len(delta)):
    x2.append(A * sym.cos(t * omega0) * sym.exp(-t * delta[i]))

x2_plot = sym.plot(legend=True, show=False)

for j in range(0, len(delta)):
    label = 'r=' + str(r_val[j])

    x2_ext_plot = sym.plot(x2[j], (t, 0, 6),
                           label=label,
                           line_color=line_colors[j],
                           legend=True,
                           show=False)
    x2_plot.extend(x2_ext_plot)

x2_plot.title = 'Зависимость координаты материальной точки x от времени t'
x2_plot.xlabel = 't, с'
x2_plot.ylabel = 'x, м'
x2_plot.show()

x2_expr = A * sym.cos(t * omega0) * sym.exp(-t * (r / (2 * m0)))

x2_plot3d = symplot.plot3d(x2_expr, (t, 0, 6),
                           (r, r_val[0], r_val[len(r_val) - 1]),
                           show=False)

x2_plot3d.title = ('Зависимость коорд. мат. точки x '
                   'от времени t и коэфф. вязкого трения r')
x2_plot3d.xlabel = 't, с'
x2_plot3d.ylabel = 'r'
x2_plot3d.show()