Commit ad7372a6 authored by Thibaut.Lunet's avatar Thibaut.Lunet

Merge branch 'master' of gitlab.unige.ch:Thibaut.Lunet/python-math

parents f3277d98 2b8dbaf1
......@@ -17,4 +17,4 @@ f(u) = c_x \frac{\partial u}{\partial x}
```
It compute it in 1D, 2D, 3D, and extract the eigenvalues for each cases.
in particular, it uses the [eigvals](https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.eigvals.html) of the scipy package.
\ No newline at end of file
In particular, it uses the [eigvals](https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.eigvals.html) of the scipy package.
# Sympy examples
This directory contains several script that show how sympy can be used for particular scientific applications.
Also, you can look at an [extended list of Sympy Tutorials](http://docs.sympy.org/latest/tutorial/index.html) to go further ...
## [solve-diff-eqns.py](solve-diff-eqns.py)
This script is a basic example of how to solve a differential equation using sympy.
In particular, the equation u''(t) - u(t) = exp(t) is solved, using the sympy function dsolve [minimize](http://docs.sympy.org/latest/modules/solvers/ode.html).
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Sympy example: How to solve a differential equation using sympy
@author: a.perez
"""
import sympy as sy
# Define a generic function u and its variable, t
u = sy.Function('u')
t = sy.symbols('t')
# We want to solve the differential equation u''(t) - u(t) = exp(t)
u_tt = sy.diff(u(t), t, t) # Second derivative of u wrt t
# (symbolic, since u is a generic function)
lhs = u_tt - u(t) # Left hand-side of the equation
rhs = sy.exp(t) # Right hand-side of the equation
eq = sy.Eq(lhs, rhs) # We create the equation
sol = sy.dsolve(eq, u(t)) # And we solve it using the function dsolve,
# wrt the function u(t)
# Now sol is the equation:
# u(t) = C2*exp(-t) + (C1 + t/2)*exp(t)
......@@ -3,7 +3,8 @@
# Exercise 4.1: Numpy chessboard - Solution
#
# Create a 8x8 matrix with a chessboard pattern with numbers 1 and 0.
# Create a 8x8 matrix with a chessboard pattern with numbers 1 and 0,
# then visualize it with the function imshow from matplotlib.
import numpy as np
import matplotlib.pyplot as plt
......@@ -19,5 +20,4 @@ def chessboard():
M = chessboard()
print(M)
plt.imshow(M, cmap='binary')
......@@ -3,12 +3,16 @@
# Exercise 4.1: Numpy chessboard
#
# Create a 8x8 matrix with a chessboard pattern with numbers 1 and 0.
# Create a 8x8 matrix with a chessboard pattern with numbers 1 and 0,
# then visualize it with the function imshow from matplotlib.
import numpy as np
import matplotlib.pyplot as plt
def chessboard():
pass # Your code here
print(chessboard())
M = chessboard()
plt.imshow(M, cmap='binary')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment