博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 和 MATLAB 的心心相印表白码
阅读量:4230 次
发布时间:2019-05-26

本文共 2185 字,大约阅读时间需要 7 分钟。

% 这个并没有什么神奇的,数学函数加上简单的参数调整就可以了x=-2:0.01:2;y1=sqrt(1-(abs(x)-1).^2);plot(x,y1,'r','LineWidth',5);y2=acos(1-abs(x))-pi;hold onplot(x,y2,'r','LineWidth',5);x=-2:0.01:5;y3=0.45*x-1.4;hold onplot(x,y3,'m','LineWidth',2.5);hold onx=4.5:0.01:5;y4=0.3*x-0.65;y5=0.6*x-2.15;plot(x,y4,'m','LineWidth',2.5);hold onplot(x,y5,'m','LineWidth',2.5);axis([-2.2 5 -3 2]);x=1.18:0.01:4.85;y1=sqrt(0.81-(abs(x-3)-0.9).^2)-0.2;plot(x,y1,'LineWidth',5);y2=acos(0.81-abs(x-3))-pi-0.2;hold onplot(x,y2,'LineWidth',5);

当然,Python也是可以这么搞的,虽然本质上也是MATLAB的身影:

# 这个并没有什么神奇的,数学函数加上简单的参数调整就可以了import matplotlib.pyplot as pltimport numpy as np# x = np.arange(-2, 2.01, 0.01)# x = np.arange(-2, 2, 0.01)x = np.linspace(-2, 2, num=401, endpoint=True)y1 = np.sqrt(1 - (abs(x) - 1) ** 2)y2 = np.arccos(1 - abs(x)) - np.piplt.plot(x, y1, 'r', LineWidth=5)plt.plot(x, y2, 'r', LineWidth=5)x3 = np.arange(-2, 5, 0.01)y3 = 0.45 * x3 - 1.4plt.plot(x3, y3, 'm', LineWidth=2.5)x4 = np.arange(4.5, 5, 0.01)y4 = 0.3 * x4 - 0.65y5 = 0.6 * x4 - 2.15plt.plot(x4, y4, 'm', LineWidth=2.5)plt.plot(x4, y5, 'm', LineWidth=2.5)# x = np.arange(1.18, 4.85, 0.01)x = np.linspace(1.18, 4.85, num=368, endpoint=True)y1 = np.sqrt(0.81 - (abs(x - 3) - 0.9) ** 2) - 0.2y2 = np.arccos(0.81 - abs(x - 3)) - np.pi - 0.2plt.plot(x, y1, 'b', LineWidth=5)plt.plot(x, y2, 'b', LineWidth=5)plt.show()

还有,我发现一个有趣的问题,在Python程序里边,如果范围取值使用的是 np.arange的话,会出现“心”不完整的情况(断层),欢迎大家讨论原因和解决方案:

# 断层import matplotlib.pyplot as pltimport numpy as np# x = np.arange(-2, 2.01, 0.01)x = np.arange(-2, 2, 0.01)# x = np.linspace(-2, 2, num=401, endpoint=True)y1 = np.sqrt(1 - (abs(x) - 1) ** 2)y2 = np.arccos(1 - abs(x)) - np.piplt.plot(x, y1, 'r', LineWidth=5)plt.plot(x, y2, 'r', LineWidth=5)x3 = np.arange(-2, 5, 0.01)y3 = 0.45 * x3 - 1.4plt.plot(x3, y3, 'm', LineWidth=2.5)x4 = np.arange(4.5, 5, 0.01)y4 = 0.3 * x4 - 0.65y5 = 0.6 * x4 - 2.15plt.plot(x4, y4, 'm', LineWidth=2.5)plt.plot(x4, y5, 'm', LineWidth=2.5)x = np.arange(1.18, 4.85, 0.01)# x = np.linspace(1.18, 4.85, num=368, endpoint=True)y1 = np.sqrt(0.81 - (abs(x - 3) - 0.9) ** 2) - 0.2y2 = np.arccos(0.81 - abs(x - 3)) - np.pi - 0.2plt.plot(x, y1, 'b', LineWidth=5)plt.plot(x, y2, 'b', LineWidth=5)plt.show()

 

转载地址:http://rdjqi.baihongyu.com/

你可能感兴趣的文章
UML Xtra-Light: How to Specify Your Software Requirements
查看>>
Fundamentals of OOP and Data Structures in Java
查看>>
C++ Network Programming, Vol. 1: Mastering Complexity with ACE and Patterns
查看>>
The Ethical Hack: A Framework for Business Value Penetration Testing
查看>>
Agile Development with ICONIX Process: People, Process, and Pragmatism
查看>>
Practical Guide to Software Quality Management
查看>>
Real Process Improvement Using the CMMI
查看>>
GDI+ Programming in C# and VB .NET
查看>>
Implementing and Integrating Product Data Management and Software Configuration Management
查看>>
Software Configuration Management
查看>>
Agile Project Management: How to Succeed in the Face of Changing Project Requirements
查看>>
MySQL Bible
查看>>
Palm OS Programming Bible
查看>>
XSLT Quickly
查看>>
Inside XSLT
查看>>
Python & XML
查看>>
Java Cryptography
查看>>
J2EE Best Practices: Java Design Patterns, Automation, and Performance
查看>>
Mastering AspectJ: Aspect-Oriented Programming in Java
查看>>
Mastering Jakarta Struts
查看>>