【问题标题】:MATLAB Heart CurveMATLAB 心形曲线
【发布时间】:2014-02-10 08:09:30
【问题描述】:

我试图在 MATLAB 中得到这个,但是我没有成功!那里有任何 MATLAB 大师吗?

alt text http://www.freeimagehosting.net/uploads/94020b921d.gif

我想简单的命令应该可以工作,可能不需要程序。

我得到的错误是;

>> t = -pi:0.1:pi;
>> r = ((sin(t)*sqrt(cos(t)))*(sin(t) + (7/5))^(-1)) - 2*sin(t) + 2 ;
??? Error using ==> mtimes
Inner matrix dimensions must agree.

更新

即使这样也没有用!

>> t = -pi:0.1:pi;
>> r = ((sin(t).*sqrt(cos(t))).*(sin(t) + (7/5)).^(-1)) - 2*sin(t) + 2 ;
>> plot(r,t)
??? Error using ==> plot
Vectors must be the same lengths.

【问题讨论】:

  • 不应该是plot(r,t)吗?
  • @Andreas Brinck:是的! .....对不起!

标签: matlab


【解决方案1】:

这是一个可行的解决方案:

t = -pi:0.1:pi;
r = ((sin(t).*sqrt(abs(cos(t))))./(sin(t) + (7/5))) - 2*sin(t) + 2 ;
polar(t,r)

编辑

polarezpolar 在情节的可定制性方面非常有限。如果OP想要重现问题中的图片,他们应该转换为笛卡尔坐标并使用绘图,就像这样

t = -pi:0.1:pi;
r = ((sin(t).*sqrt(abs(cos(t))))./(sin(t) + (7/5))) - 2*sin(t) + 2 ;
[x,y] = pol2cart(t,r);
plot(x,y)

【讨论】:

  • @Jonas : 很好,可惜我只能选择一个正确的答案:)
  • @Jonas:这是绘制此类曲线的通用方法,非常感谢!
【解决方案2】:

我想知道plot 是否是正确的命令。我试过这个:

ezpolar('((sin(t).*sqrt(cos(t))).*(sin(t) + (7/5)).^(-1)) - 2*sin(t) + 2')

几乎得到了 OP 正在寻找的图表。我怀疑 OP 使用polar 可能会做得更好。尝试plot(r,t) 在 (x,y) 空间中给了我一个波浪线和一个警告:

Warning: Imaginary parts of complex X and/or Y arguments ignored

【讨论】:

  • 虚部是由sqrt(cos(t))引起的。 cos(t) 可以是负数。
  • 我试过 ezpolar('((sin(t)*sqrt(abs(cos(t))))*(sin(t) + (7/5))^(-1)) - 2*sin(t) + 2')。它完美匹配。
  • @High Performance Mark : 已接受答案,您先于其他人推荐了 ezpolar!
【解决方案3】:

尝试将 * 替换为 .* 以进行组件智能乘法。也可以试试plot(r,t) 而不是plot(s,t)

【讨论】:

    【解决方案4】:

    试试这个:

    >> t = -pi:0.1:pi;
    >> r = ((sin(t).*sqrt(cos(t))).*(sin(t) + (7/5)).^(-1)) - 2*sin(t) + 2 ;
    

    您需要使用 .*.^ 告诉 MATLAB 进行逐分量乘法和求幂运算。否则,MATLAB 将尝试进行矩阵乘法(.*)或矩阵求逆(.^(-1))。

    【讨论】:

      【解决方案5】:

      如果您想对矩阵中的每个项目运行一个复杂的公式,一个简单的方法是使用 arrayfun 函数。

      % Define the formula that is applied to each element
      f = @(t) ((sin(t)*sqrt(abs(cos(t))))*(sin(t) + (7/5))^(-1)) - 2*sin(t) + 2;
      
      t = -pi:0.1:pi;
      % Apply the formula
      r = arrayfun(f, t);
      plot(abs(t), r);
      

      这为您提供了思路,但您可能需要修正公式。例如,不应该是 sqrt(abs(cos(t))) 吗?

      【讨论】:

        【解决方案6】:
        x1=linspace(-1,0,50);
        y1=-x1+sqrt(3-3*x1.^2);
        y2=-x1-sqrt(3-3*x1.^2);
        plot(x1,y1,'r');hold on;
        plot(-x1,y1,'r');
        title ('MY HEART');
        plot(-x1,y2,'r');
        plot(x1,y2,'r');hold off;
        

        【讨论】:

        • 如果帖子被格式化以便代码显示为代码,则更容易阅读帖子。编辑答案时,使用 {} 按钮偏移代码。您可以使用反引号将某些内容标记为内联代码。
        【解决方案7】:
        theta = 0:.1:2*pi;
        
        r = (sin(theta).*sqrt(abs(cos(theta))))./(sin(theta) + (7/5))-2.*sin(theta)+2;
        
        polar(theta, r,'-r');
        

        【讨论】:

          猜你喜欢
          • 2012-02-07
          • 2013-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多