努力再努力zZ 2023-12-07 20:44 采纳率: 33.3%
浏览 11
已结题

q从常量变成sin函数,怎么改写python代码?

q从常量改变成随时间变化的sin函数,怎么改写python代码?如题,将q变成q=sin(ommiga*t),t取四个值,ommiga取一个定值,都可以随意取值,没有要求

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sympy.solvers import solve
from sympy import Symbol
from matplotlib import patches
import matplotlib.patches as mpatches
import scipy.io as sio

# plotting configuration
ratio = 1.5
figure_len, figure_width = 15*ratio, 12*ratio
font_size_1, font_size_2 = 36*ratio, 36*ratio
legend_size = 18*ratio
line_width, tick_len = 3*ratio, 10*ratio
marker_size = 15*ratio
plot_line_width = 5*ratio
hfont = {'fontname': 'Arial'}

l_color = ['#D6EAF8', '#85C1E9', '#3498DB', '#2874A6']

a = 1.8
b = 1.0
c = 1.0
d = 0.6
n = 2

l_q = [0.75, 1.5, 2.25, 3]
p = 2

plt.figure(figsize=(figure_len, figure_width))
ax = plt.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.spines['left'].set_visible(True)
for axis in ['top', 'bottom', 'left', 'right']:
    ax.spines[axis].set_linewidth(line_width)
plt.tick_params(width=line_width, length=tick_len)


for q_idx in range(len(l_q)):
    q = l_q[q_idx]

    x = np.arange(0, 10, 0.001)

    detM = -a * d + b * c
    C_p = -(1 / b) * d * q + p
    N = detM * (1 / b) * np.power(x, 2) + (1 / b) * d * x + C_p
    y = a * np.power(x, 2) - b * np.power(N * (N > 0), 2) - x + q

    plt.plot(x, y, color=l_color[q_idx], linewidth=plot_line_width)

plt.hlines(y=0, xmin=0, xmax=3, colors='k', linestyles=[(0, (6, 6, 6, 6))], linewidth=line_width)
plt.xlabel(r'$x$', fontsize=font_size_1, **hfont)
plt.ylabel(r'$y$', fontsize=font_size_1, **hfont)
plt.xticks([0, 1, 2, 3], fontsize=font_size_1, **hfont)
plt.yticks([-5, 0, 5, 10, 15], fontsize=font_size_1, **hfont)
plt.xlim([0, 3])
plt.ylim([-5, 15])
plt.legend([r'$q$: 0.75', r'$q$: 1.5', r'$q$: 2.25', r'$q$: 3.0'], prop={"family": "Arial", 'size': font_size_1}, loc='upper right')
plt.savefig('1.png')
plt.savefig('2.pdf')

  • 写回答

16条回答 默认 最新

  • weixin_44407773 2023-12-08 13:29
    关注

    为了让q变成变量,那就需要将计算和绘图部分放入一个循环。每一轮循环,我们都更新t,再计算出q。

    这样您可以根据不同的时间t得到不同的q值,并对应创建不同的图像。以下是修改后的代码:

    
    import numpy as np
    import matplotlib.pyplot as plt
    from math import sin, pi
    
    # 绘图配置
    ratio = 1.5
    figure_len, figure_width = 15*ratio, 12*ratio
    font_size_1, font_size_2 = 36*ratio, 36*ratio
    legend_size = 18*ratio
    line_width, tick_len = 3*ratio, 10*ratio
    
    # 参数设置
    a = 1.8
    b = 1.0
    c = 1.0
    d = 0.6
    n = 2
    p = 2
    l_ommiga = 0.5 # 假设你的 omega 为 0.5
    l_t = [1, 2, 3, 4]  # t取四个值
    
    for t in l_t:
    
        plt.figure(figsize=(figure_len, figure_width))
        ax = plt.gca()
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.spines['bottom'].set_visible(True)
        ax.spines['left'].set_visible(True)
        ax.spines[axis].set_linewidth(line_width)
        plt.tick_params(width=line_width, length=tick_len)
    
        q = sin(l_ommiga * t)  # q = sin(ommiga * t)
    
        x = np.arange(0, 10, 0.001)
        detM = -a * d + b * c
        C_p = -(1 / b) * d * q + p
        N = detM * (1 / b) * np.power(x, 2) + (1 / b) * d * x + C_p
        y = a * np.power(x, 2) - b * np.power(N * (N > 0), 2) - x + q
    
        plt.plot(x, y, label=f"q: {q:.2f}", linewidth=plot_line_width)
    
        plt.hlines(y=0, xmin=0, xmax=3, colors='k', linestyles=[(0, (6, 6, 6, 6))], linewidth=line_width)
        plt.xlabel(r'$x$', fontsize=font_size_1)
        plt.ylabel(r'$y$', fontsize=font_size_1)
        plt.xticks([0, 1, 2, 3], fontsize=font_size_1)
        plt.yticks([-5, 0, 5, 10, 15], fontsize=font_size_1)
        plt.xlim([0, 3])
        plt.ylim([-5, 15])
        plt.legend(prop={"family": "Arial", 'size': font_size_1}, loc='upper right')
        plt.savefig(f"{t}theta.png")
    
    plt.show()
    
    
    

    注意,这个脚本将为每个t值生成一个单独的图像,并保存为像“1theta.png”,“2theta.png”等等这样的文件。这些图像会在你运行该脚本的同一目录下。
    注意,l_ommiga和l_t值是我随意设定的,你需要根据你的需求自行设定。同时,由于sin函数的范围是-1到1,出于清晰度的考虑,你可能需要调整y轴的范围以适应不同的q值。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(15条)

报告相同问题?

问题事件

  • 系统已结题 12月19日
  • 已采纳回答 12月11日
  • 创建了问题 12月7日