import matplotlib.pyplot as plt
import numpy as np
#Return evenly spaced values within an interval with step (step = spacing between values, default 1)
#start, stop, step=1
print(np.arange(1, 5))
#Return evenly spaced numbers over an interval (num = mumber of samples, default 50)
#start, stop, num=50
print(np.linspace(1,4,4))
[1 2 3 4] [1. 2. 3. 4.]
#Return coordinate matrices from coordinate vectors.
#meshgrid
X = np.array([1,2,3,4,5])
Y = np.array([11, 12, 13, 14, 15])
xx, yy = np.meshgrid(X,Y)
#Generate random data
#Generate random integer from 0 to 100
rnd1 = np.random.randint(100)
#Geberate random float between 0 and 1
rnd2 = np.random.rand()
#Generate a 2-D array of 3 x 5 dimensions (rows x cols) with random integers from 0 to 100
arr1 = np.random.randint(100, size=(3, 5))
#Generate a 2-D array with 3 X 5 dimensions (rows x cols) with random float numbers:
arr2 = np.random.rand(3, 5)
#10 x 5 random matrix data
m = np.random.rand(10,5)
#mask data
x,y = np.ogrid[0:10, 0:5]
print(x,y)
mask = (x>5)|(y<3)
print(mask)
m[mask]=0
print(m)
print("")
#convert boolean to decimal if needed
print(mask*1)
[[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]] [[0 1 2 3 4]] [[ True True True False False] [ True True True False False] [ True True True False False] [ True True True False False] [ True True True False False] [ True True True False False] [ True True True True True] [ True True True True True] [ True True True True True] [ True True True True True]] [[0. 0. 0. 0.42484734 0.148615 ] [0. 0. 0. 0.96204364 0.01109302] [0. 0. 0. 0.85049584 0.88776109] [0. 0. 0. 0.28209821 0.96709326] [0. 0. 0. 0.22774129 0.97801169] [0. 0. 0. 0.30172618 0.04260937] [0. 0. 0. 0. 0. ] [0. 0. 0. 0. 0. ] [0. 0. 0. 0. 0. ] [0. 0. 0. 0. 0. ]] [[1 1 1 0 0] [1 1 1 0 0] [1 1 1 0 0] [1 1 1 0 0] [1 1 1 0 0] [1 1 1 0 0] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]]
def graph(formula, x):
y = eval(formula)
plt.plot(x, y)
plt.show()
graph('4*x+3', np.arange(-10, 11))
graph('4*x+3', np.linspace(-10,11,100))
def graph(formula, x):
y = eval(formula)
plt.plot(x, y)
#grid and origin
ax = plt.gca()
ax.grid(True)
ax.spines['left'].set_position('zero')
#ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
#ax.spines['top'].set_color('none')
plt.xlim(-10,11)
plt.show()
graph('4*x+3', np.arange(-10, 11))
graph('4*x+3', np.linspace(-10,11,100))
Method 1
x = np.linspace(-10,11,50)
y = 4*x + 3
xx, yy = np.meshgrid(x,y)
#to display all points with property: y <= 4*x + 3
#we will mask all the other points
yy = np.ma.masked_where(yy > 4*xx + 3, yy)
print(yy)
plt.scatter(xx,yy, color="blue", marker="+");
[[-37.0 -37.0 -37.0 ... -37.0 -37.0 -37.0] [-- -35.285714285714285 -35.285714285714285 ... -35.285714285714285 -35.285714285714285 -35.285714285714285] [-- -- -33.57142857142857 ... -33.57142857142857 -33.57142857142857 -33.57142857142857] ... [-- -- -- ... 43.57142857142857 43.57142857142857 43.57142857142857] [-- -- -- ... -- 45.28571428571428 45.28571428571428] [-- -- -- ... -- -- 47.0]]
Method 2
x = np.linspace(-10,11,50)
y = 4*x + 3
xx, yy = np.meshgrid(x,y)
#if not using np.Nan instead of 0, we will have an extra line plotted y = 0 for all excluded x's
mask = np.where(yy <= 4*xx + 3,1,np.NaN)
print(mask*yy)
plt.scatter(xx,mask*yy, color="blue", marker="+");
[[-37. -37. -37. ... -37. -37. -37. ] [ nan -35.28571429 -35.28571429 ... -35.28571429 -35.28571429 -35.28571429] [ nan nan -33.57142857 ... -33.57142857 -33.57142857 -33.57142857] ... [ nan nan nan ... 43.57142857 43.57142857 43.57142857] [ nan nan nan ... nan 45.28571429 45.28571429] [ nan nan nan ... nan nan 47. ]]
Method 3
x = np.linspace(-10,11,50)
y = 4*x + 3
xx, yy = np.meshgrid(x,y)
#defining False / True mask for plotting
mask = (yy <= 4*xx + 3)
#change to 0 values from yy not for plotting
yy = mask*yy
#change 0 values to NaN
yy[yy==0]=np.NaN
plt.scatter(xx,yy, color="blue", marker="+");
import re
def surface(formula, x):
#define line from inequality
ret1 = re.findall('(?<=\<=).*', formula)
ret2 = re.findall('(?<=\<).*',formula)
ret3 = re.findall('(?<=\>=).*',formula)
ret4 = re.findall('(?<=\>).*',formula)
if(ret1):
line = ret1[0]
elif(ret2):
line = ret2[0]
elif(ret3):
line = ret3[0]
elif(ret4):
line = ret4[0]
print(f"Inequality is: {formula}")
print(f"Function is: {line}")
y = eval(line)
xx, yy = np.meshgrid(x,y)
formula = formula.replace('x','xx')
formula = formula.replace('y','yy')
mask = np.where(eval(formula),1,np.NaN)
plt.scatter(xx,mask*yy, color="blue", marker="+");
surface('y <= 4*x + 3' , np.linspace(-10,11,50))
Inequality is: y <= 4*x + 3 Function is: 4*x + 3
surface('y >= 4*x + 3' , np.linspace(-10,11,50))
Inequality is: y >= 4*x + 3 Line is: 4*x + 3
graph('3*x**2-4*x+3', np.arange(-10, 11))
graph('3*x**2-4*x+3', np.linspace(-10,11,100))
surface('y <= 3*x**2-10*x+3' , np.linspace(-10,11,50))
Inequality is: y <= 3*x**2-10*x+3 Function is: 3*x**2-10*x+3
surface('y >= 3*x**2-10*x+3' , np.linspace(-10,11,50))
Inequality is: y >= 3*x**2-10*x+3 Function is: 3*x**2-10*x+3
For a better definition of surface, we are using more points
surface('y >= 3*x**2-10*x+3' , np.linspace(-10,11,500))
Inequality is: y >= 3*x**2-10*x+3 Function is: 3*x**2-10*x+3
from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
solve(3*x**2-10*x+3, x)
[1/3, 3]
$ f(x) = 3{ x }^{ 2 }-4x+3 \\ f'(x)= \frac { d }{ dx } (3{ x }^{ 2 }-4x+3)=6x-4 $
import matplotlib.pyplot as plt
import numpy as np
import sympy as sym
def graph(formula, x):
y = eval(formula)
plt.plot(x, y)
#plt.show()
def tangent(x_axe,f,x_range):
x = sym.Symbol('x')
f_prime = sym.diff(f)
print(f"Derivative of {f} is {f_prime}")
#the slope of tangent line to graph
m = f_prime.subs(x,x_axe)
#tangent point
f = eval(f)
y_axe = f.subs(x,x_axe)
plt.plot(x_axe, y_axe, 'o')
plt.text(x_axe, y_axe, '('+str(x_axe)+','+str(y_axe)+')', dict(size=10))
#y = mx+n => n = y - mx
n = y_axe - m*x_axe
tangent_line_expr = str(eval('m*x + n'))
print(f"Tangent line at point ({x_axe},{y_axe}): {tangent_line_expr}")
#print(tangent_point)
graph(tangent_line_expr,x_range)
x_range = np.linspace(-20,21,100)
f = '3*x**2-4*x+3'
graph(f, x_range)
tangent(9,f,x_range)
tangent(-5,f,x_range)
#oy axe range
plt.ylim(-10,400)
plt.title(r"$f(x) = 3{ x }^{ 2 }-4x+3$",)
plt.show()
Derivative of 3*x**2-4*x+3 is 6*x - 4 Tangent line at point (9,210): 50*x - 240 Derivative of 3*x**2-4*x+3 is 6*x - 4 Tangent line at point (-5,98): -34*x - 72