个人笔记--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进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

从入门到进阶讲解Python自动化Playwright实战指南

《从入门到进阶讲解Python自动化Playwright实战指南》Playwright是针对Python语言的纯自动化工具,它可以通过单个API自动执行Chromium,Firefox和WebKit... 目录Playwright 简介核心优势安装步骤观点与案例结合Playwright 核心功能从零开始学习

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

Python自动化批量重命名与整理文件系统

《Python自动化批量重命名与整理文件系统》这篇文章主要为大家详细介绍了如何使用Python实现一个强大的文件批量重命名与整理工具,帮助开发者自动化这一繁琐过程,有需要的小伙伴可以了解下... 目录简介环境准备项目功能概述代码详细解析1. 导入必要的库2. 配置参数设置3. 创建日志系统4. 安全文件名处

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数