个人笔记--python用tanh画圆形,正方形,长方形(epsilon界面宽度)

本文主要是介绍个人笔记--python用tanh画圆形,正方形,长方形(epsilon界面宽度),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

用tanh函数画图

圆形

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt# 创建一个二维网格
xx = np.linspace(-1, 1, 1000)
yy = np.linspace(-1, 1, 1000)
x_i, y_i = np.meshgrid(xx, yy)# 圆的半径和中心
r = 0.4
center_x, center_y = 0, 0  # 假设圆心在(0, 0)# 计算每个网格点到圆心的距离
distance = np.sqrt((x_i - center_x) ** 2 + (y_i - center_y) ** 2)# 使用tanh函数来近似表示半圆区域
# 注意:tanh函数不能直接用于表示半圆,但我们可以通过设置阈值来近似表示
epsilon = 0.01 # 控制tanh函数的“陡峭度”
u0 = 0.5 * (1 + np.tanh((r - distance) / epsilon))
u0[distance > r] = -1  # 强制圆外的值为-1# 绘制结果
fig, ax = plt.subplots()
p = ax.pcolormesh(x_i, y_i, u0, cmap='viridis', shading='auto')
ax.set_aspect('equal', 'box')  # 保持x和y轴的比例相同
fig.colorbar(p, ax=ax, label='u0 Value')# 设置标题和坐标轴标签
plt.title('Approximate Half Circle using tanh')
plt.xlabel('X')
plt.ylabel('Y')# 显示图形
plt.show()

半圆

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt# 创建一个二维网格
xx = np.linspace(-1, 1, 1000)
yy = np.linspace(-1, 1, 1000)
x_i, y_i = np.meshgrid(xx, yy)# 圆的半径和中心
r = 0.4
center_x, center_y = 0, -1  # 假设圆心在(0, -1)# 计算每个网格点到圆心的距离
distance = np.sqrt((x_i - center_x) ** 2 + (y_i - center_y) ** 2)# 使用tanh函数来近似表示半圆区域
# 注意:tanh函数不能直接用于表示半圆,但我们可以通过设置阈值来近似表示
epsilon = 0.01 # 控制tanh函数的“陡峭度”
u0 = 0.5 * (1 + np.tanh((r - distance) / epsilon))
u0[distance > r] = -1  # 强制圆外的值为-1# 绘制结果
fig, ax = plt.subplots()
p = ax.pcolormesh(x_i, y_i, u0, cmap='viridis', shading='auto')
ax.set_aspect('equal', 'box')  # 保持x和y轴的比例相同
fig.colorbar(p, ax=ax, label='u0 Value')# 设置标题和坐标轴标签
plt.title('Approximate Half Circle using tanh')
plt.xlabel('X')
plt.ylabel('Y')# 显示图形
plt.show()

两个圆形

在这里插入图片描述
在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt# 创建一个二维网格
xx = np.linspace(0, 1, 1000)
yy = np.linspace(0, 1, 1000)
x_i, y_i = np.meshgrid(xx, yy)# 圆的半径和中心
r1 = 0.15  # 如果要分开点,就设为0.14
r2 = 0.15  # 如果要分开点,就设为0.14
center_x1, center_y1 = 0.35, 0.5  # 圆心1
center_x2, center_y2 = 0.65, 0.5  # 圆心2# 计算每个网格点到圆心的距离
distance1 = np.sqrt((x_i - center_x1) ** 2 + (y_i - center_y1) ** 2)
distance2 = np.sqrt((x_i - center_x2) ** 2 + (y_i - center_y2) ** 2)# 使用tanh函数来近似表示半圆区域
# 注意:tanh函数不能直接用于表示半圆,但我们可以通设置阈值来近似表示
epsilon = 0.01 # 控制tanh函数的“陡峭度”
phi1 = np.tanh((r1 - distance1) / (2 * epsilon))
phi2 = np.tanh((r2 - distance2) / (2 * epsilon))u0 = np.maximum(phi1, phi2)
# u0 = 1 * (1 + phi1 + phi2)  # 两种都可以生成两个圆# 分别找出距离两个圆心都大于半径的点的索引
outside_circle1 = distance1 > r1
outside_circle2 = distance2 > r2# 使用逻辑与来找出两个条件都满足的点
outside_both_circles = np.logical_and(outside_circle1, outside_circle2)# 将这些点对应的u0值设置为-1
u0[outside_both_circles] = -1# 绘制结果
fig, ax = plt.subplots()
p = ax.pcolormesh(x_i, y_i, u0, cmap='viridis', shading='auto')
ax.set_aspect('equal', 'box')  # 保持x和y轴的比例相同
fig.colorbar(p, ax=ax, label='u0 Value')# 设置标题和坐标轴标签
plt.title('Approximate Half Circle using tanh')
plt.xlabel('X')
plt.ylabel('Y')# 显示图形
plt.show()

正方形(不带界面过渡)

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt# 创建一个二维网格
xx = np.linspace(-1, 1, 1000)
yy = np.linspace(-1, 1, 1000)
x_i, y_i = np.meshgrid(xx, yy)# 正方形的中心和边长
center_x, center_y = 0, 0  # 假设正方形中心在(0, 0),为了简化计算
side_length = 0.5  # 正方形边长的一半# 使用tanh函数来近似正方形的四个边界
# 注意:这不是一个完美的正方形,只是一个近似
epsilon = 0.01  # 控制tanh函数的“陡峭度”# 近似上边界
u_top = 0.5 * (1 + np.tanh((side_length - np.abs(y_i - center_y)) / epsilon))
# 近似下边界
u_bottom = 0.5 * (1 + np.tanh((side_length - np.abs(y_i + center_y)) / epsilon))
# 近似左边界
u_left = 0.5 * (1 + np.tanh((side_length - np.abs(x_i - center_x)) / epsilon))
# 近似右边界
u_right = 0.5 * (1 + np.tanh((side_length - np.abs(x_i + center_x)) / epsilon))# 四个边界的交集(即正方形内部)应取得高值
u0 = np.minimum(np.minimum(u_top, u_bottom), np.minimum(u_left, u_right))
u0[u0 < 0.99] = -1  # 强制非正方形内部的值为-1(可调整阈值)
u0[u0 >= 0.99] = 1  # 强制正方形内部的值为1(可调整阈值)# 绘制结果
fig, ax = plt.subplots()
p = ax.pcolormesh(x_i, y_i, u0, cmap='viridis', shading='auto')
ax.set_aspect('equal', 'box')  # 保持x和y轴的比例相同
fig.colorbar(p, ax=ax, label='u0 Value')# 设置标题和坐标轴标签
plt.title('Approximate Square using tanh')
plt.xlabel('X')
plt.ylabel('Y')# 显示图形
plt.show()

长方形(不带界面过渡)

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt# 创建一个二维网格
xx = np.linspace(-1, 1, 1000)
yy = np.linspace(-1, 1, 1000)
x_i, y_i = np.meshgrid(xx, yy)# 长方形的中心和尺寸
center_x, center_y = 0, 0  # 假设长方形中心在(0, 0)
width = 0.6  # 长方形的宽度
height = 0.4  # 长方形的高度
half_width = width / 2
half_height = height / 2# 使用tanh函数来近似长方形的四个边界
epsilon = 0.01  # 控制tanh函数的“陡峭度”# 近似上边界
u_top = 0.5 * (1 + np.tanh((half_height - (y_i - center_y)) / epsilon))
# 近似下边界
u_bottom = 0.5 * (1 + np.tanh((half_height - (center_y - y_i)) / epsilon))
# 近似左边界
u_left = 0.5 * (1 + np.tanh((half_width - (x_i - center_x)) / epsilon))
# 近似右边界
u_right = 0.5 * (1 + np.tanh((half_width - (center_x - x_i)) / epsilon))# 长方形的内部是四个边界的交集,取四个边界中的最小值
u0 = np.minimum.reduce([u_top, u_bottom, u_left, u_right])
u0[u0 < 0.99] = -1  # 强制非长方形内部的值为-1(可调整阈值)
u0[u0 >= 0.99] = 1  # 强制长方形内部的值为1(可调整阈值)# 绘制结果
fig, ax = plt.subplots()
p = ax.pcolormesh(x_i, y_i, u0, cmap='viridis', shading='auto')
ax.set_aspect('equal', 'box')  # 保持x和y轴的比例相同
fig.colorbar(p, ax=ax, label='u0 Value')# 设置标题和坐标轴标签
plt.title('Approximate Rectangle using tanh')
plt.xlabel('X')
plt.ylabel('Y')# 显示图形
plt.show()

边界上的长方形(不带界面过渡)

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt# 创建一个二维网格
xx = np.linspace(-0.5, 0.5, 1000)
yy = np.linspace(0, 0.5, 1000)
x_i, y_i = np.meshgrid(xx, yy)# 长方形的中心和尺寸
center_x, center_y = 0, 0.0625  # 假设长方形中心在(0, 0)
width = 0.5  # 长方形的宽度(长)
height = 0.125  # 长方形的高度
half_width = width / 2
half_height = height / 2# 使用tanh函数来近似长方形的四个边界
epsilon = 0.01  # 控制tanh函数的“陡峭度”# 近似上边界
u_top = 1 * (1 + np.tanh((half_height - (y_i - center_y)) / epsilon))
# 近似下边界
u_bottom = 1 * (1 + np.tanh((half_height - (center_y - y_i)) / epsilon))
# 近似左边界
u_left = 1 * (1 + np.tanh((half_width - (x_i - center_x)) / epsilon))
# 近似右边界
u_right = 1 * (1 + np.tanh((half_width - (center_x - x_i)) / epsilon))# 长方形的内部是四个边界的交集,取四个边界中的最小值
u0 = np.minimum.reduce([u_top, u_bottom, u_left, u_right])
u0[u0 < 0.99] = -1  # 强制非长方形内部的值为-1(可调整阈值)
u0[u0 >= 0.99] = 1  # 强制长方形内部的值为1(可调整阈值)# # 绘制结果
# fig, ax = plt.subplots()
# p = ax.pcolormesh(x_i, y_i, u0, cmap='viridis', shading='auto')
# ax.set_aspect('equal', 'box')  # 保持x和y轴的比例相同
# fig.colorbar(p, ax=ax, label='u0 Value')
#
# # 设置标题和坐标轴标签
# plt.title('Approximate Rectangle using tanh')
# plt.xlabel('X')
# plt.ylabel('Y')
#
# # 显示图形
# plt.grid()
# plt.show()# 估算角点位置(这里只是估算,不是精确值)
# 左上角
x_left_top, y_left_top = center_x - half_width, center_y + half_height
# 右上角
x_right_top, y_right_top = center_x + half_width, center_y + half_height
# 左下角
x_left_bottom, y_left_bottom = center_x - half_width, center_y - half_height
# 右下角
x_right_bottom, y_right_bottom = center_x + half_width, center_y - half_height# 绘制结果
fig, ax = plt.subplots()
p = ax.pcolormesh(x_i, y_i, u0, cmap='viridis', shading='auto')
ax.set_aspect('equal', 'box')  # 保持x和y轴的比例相同
fig.colorbar(p, ax=ax, label='u0 Value')# 设置标题和坐标轴标签
plt.title('Approximate Rectangle using tanh')
plt.xlabel('X')
plt.ylabel('Y')# 显示长方形四个角的坐标数字
plt.text(x_left_top, y_left_top, f'({x_left_top:.2f}, {y_left_top:.2f})', va='top', ha='right', color='red')
plt.text(x_right_top, y_right_top, f'({x_right_top:.2f}, {y_right_top:.2f})', va='top', ha='left', color='red')
plt.text(x_left_bottom, y_left_bottom, f'({x_left_bottom:.2f}, {y_left_bottom:.2f})', va='bottom', ha='right', color='red')
plt.text(x_right_bottom, y_right_bottom, f'({x_right_bottom:.2f}, {y_right_bottom:.2f})', va='bottom', ha='left', color='red')# 显示网格和图形
plt.grid()
plt.show()

边界上的长方形(带界面宽度)

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt# 创建一个二维网格
xx = np.linspace(-0.5, 0.5, 1000)
yy = np.linspace(0, 0.5, 1000)
x_i, y_i = np.meshgrid(xx, yy)# 长方形的中心和尺寸
center_x, center_y = 0, 0.0625  # 长方形中心
width = 0.5  # 长方形的宽度(长)
height = 0.125  # 长方形的高度
half_width = width / 2
half_height = height / 2# 使用tanh函数来近似长方形的四个边界
epsilon = 0.01  # 控制tanh函数的“陡峭度”# 近似上边界
u_top = np.tanh((half_height - (y_i - center_y)) / (2 * epsilon))
# # 近似下边界
# u_bottom = np.tanh((half_height - (center_y - y_i)) / (2 * epsilon))
u_bottom = np.ones((1000, 1000))
# 近似左边界
u_left = np.tanh((half_width - (x_i - center_x)) / (2 * epsilon))
# 近似右边界
u_right = np.tanh((half_width - (center_x - x_i)) / (2 * epsilon))# 长方形的内部是四个边界的交集,取四个边界中的最小值
u0 = np.minimum.reduce([u_top, u_bottom, u_left, u_right])# # 绘制结果
fig, ax = plt.subplots()
p = ax.pcolormesh(x_i, y_i, u0, cmap='viridis', shading='auto')
ax.set_aspect('equal', 'box')  # 保持x和y轴的比例相同
fig.colorbar(p, ax=ax, label='u0 Value')# 设置标题和坐标轴标签
plt.title('Approximate Rectangle using tanh')
plt.xlabel('X')
plt.ylabel('Y')# 显示图形
plt.grid()
plt.show()

这篇关于个人笔记--python用tanh画圆形,正方形,长方形(epsilon界面宽度)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1028729

相关文章

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文