[High school mathematics + python] Logarithmic problem

Introduction

I tried the study supplement as a review of high school math and it was amazing. In particular, Professor Yoshiaki Sakai's class and Professor Masao Seki's class in English are at a level that would have changed my life if I had taken them in high school. (Actually, I only took these two courses.) However, it is said that time cannot be rewound (I think I can do it personally ...), so I would like to study high school mathematics by dropping it into a program.

Regarding study supplements, it has nothing to do with studying, and I would like to see a beautiful teacher's chemistry class, but when I was in high school I was only interested in physics, and I didn't study chemistry at all. .. .. If you have the physical strength to study a new subject unrelated to work, I will take a look.

High school 1-2 years Top level mathematics IAIIB Lecture 03

\displaystyle \log_{\sqrt{a}}\left(3-x\right)-\frac{1}{\log_{2}a}>\log_{a}\left(x+2\right)+1

\Leftrightarrow\log_{a}\left(3-x\right)^{2}-\log_{a}2>\log_{a}\left(x+2\right)+\log_{a}a

\displaystyle \Leftrightarrow\log_{a}\frac{\left(3-x\right)^{2}}{2}>\log_{a}a\left(x+2\right)

$ \ displaystyle \ text {①} 0 <a <1 when a \ left (x + 2 \ right) <\ frac {\ left (3-x \ right) ^ {2}} {2} $

$ \ displaystyle \ text {②} 1 When <a \ frac {\ left (3-x \ right) ^ {2}} {2} <a \ left (x + 2 \ right) $

$ \ text {In addition, it is described in the condition as a positive integer that is not} a \ neq 1 \ text {from the problem statement. } $

Please learn about the transformation of the above formula in the study supplement course.

Drawing with matplotlib

f\left(x\right)=\left(x-3\right)^{2}

g\left(x\right)=2\left(x+2\right)

Illustrate the graph of $ \ text {. } $

$ \ left (\ text {Formula transformation is not the subject of this article, so I've omitted it a bit} \ right) $

Preparation

The execution environment is as follows.

Building a virtual environment for python

First, build a program creation environment.

python -m venv math
.\math\Scripts\activate

I don't use anaconda, ipthon, or jupyter unless it's necessary. I know it's very convenient, but for some reason this method is the most suitable.

Check the installation environment of pip

pip list
Execution result

Package Version ---------- ------- pip 19.0.3 setuptools 40.8.0 You are using pip version 19.0.3, however version 19.3.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.

pip upgrade and matplotlib installation

python -m pip install --upgrade pip
pip install matplotlib
pip install sympy

Check the installation environment of pip ②

pip list
Execution result

Package Version ---------- ------- cycler 0.10.0 kiwisolver 1.1.0 matplotlib 3.1.1 mpmath 1.1.0 numpy 1.17.3 pip 19.3.1 pyparsing 2.4.4 python-dateutil 2.8.1 setuptools 40.8.0 six 1.13.0 sympy 1.4

For now, you're ready to program.

Expression drawing ①

First of all f\left(x\right)=\left(x-3\right)^{2} Let's write a program that illustrates.

sample01.py


from sympy import Symbol
from sympy.plotting import plot
import numpy as np
import matplotlib.pyplot as plt

#Define variable x
x = Symbol('x')

#Define a given formula
f_x = (x-3)**2

#Since the axis of the formula is 3,-Draw a range of 2-8
x_val = np.linspace(-2, 8, 100)

# f_Store x values in a list
y_val = []
for x_data in x_val :
	y_val.append(f_x.subs({x:x_data})) 
# y_Convert val to numpy format
y_val = np.array(y_val)

#Check variable information
print("x_type of val: ")
print(type(x_val))
print("y_type of val: ")
print(type(y_val))
print("x_Number of elements in val: ")
print(len(x_val))
print("y_Number of elements in val: ")
print(len(y_val))

#Creating a graphing area
fig = plt.figure()

#Drawing a graph
plt.plot(x_val, y_val, label="f_x")

#Specifying and saving the file name of the graph
outputFileName = "sample01.png "
plt.savefig(outputFileName)
plt.close()

Execution result

type of x_val: <class 'numpy.ndarray'> type of y_val: <class 'numpy.ndarray'> Number of elements in x_val 100 Number of elements in y_val 100

sample01.png

excuse

I know it's a forced code, but at this point (as of November 10, 2019), this is all I can do. If anyone knows how to write beautifully, please give me some advice.

plot(f_x, (x,-2,8))

It's easy to illustrate with, but the plt.savefig function doesn't work, right? So, knowing that it is dirty code, if you write a program within the knowledge you know, it will be the above content. .. ..

Expression drawing ②

See below

Finally

I would like to summarize the python grammar and matplotlib formatting on another page.

Recommended Posts

[High school mathematics + python] Logarithmic problem
[Python] ABC159D (High School Mathematics nCr) [At Coder]
AtCoder C problem summary that can be solved in high school mathematics
Try to solve a set problem of high school math with Python
#Python DeepLearning Basics (Mathematics 1/4)
Python Mathematics Series ① Transpose
ABC163 C problem with python3
[Python] Dynamic programming knapsack problem
Python Mathematics Series ③ Determinant (replacement)
ambisonics simulation (external problem) Python
Python # Snap7 Libray Import problem
ABC188 C problem with python3
ABC187 C problem with python
Let's create an automatic factorization device / Junior High School Mathematics Vol.1