【问题标题】:Printing polar and trigonometric coordinates in Matlab在 Matlab 中打印极坐标和三角坐标
【发布时间】:2021-12-04 03:03:14
【问题描述】:

我正在尝试编写一个函数,它将笛卡尔坐标中的 x 和 y 值作为输入并输出极坐标和三角函数形式。我希望输出包含指数和 sin/cos 而不是实际值。例如,如果笛卡尔是 z=1i,我希望函数输出 z=sqrt(2)e^(ipi/4) 和 z=sqrt(2) (cos(pi/4)+Isin(pi/4)。我该怎么做?

函数坐标(x,y)

r=sqrt(x.^2+y.^2);

theta=atan(y./x);

polarcoord=rexp(itheta)

trigcoord=r*(cos(theta)+i*sin(theta))

结束

这给了我以下输出:

极坐标 =

1.0000 + 1.0000i

trigcoord =

1.0000 + 1.0000i

谢谢

【问题讨论】:

    标签: matlab polar-coordinates cartesian-coordinates


    【解决方案1】:

    欢迎来到 SO!您正在使用函数(sqrt(x) 计算 x 的实际平方根,exp(x) 指数,...这就是为什么您得到实际值,而不是公式表达式。

    因此,输出公式的一种可能方法是使用字符串打印函数而不调用它们。然后,您需要做的是计算 sqrt() 中所需的值和 * pi 的分数并将它们添加到字符串数组中。我们可以使用方括号[ ]、函数num2string() 和运算符+ 在同一个字符串数组中添加字符串:

    function coordinates(x,y)
    
    [num,dem] = rat(atan(y./x)/pi);  
        % rat converts theta to fraction
        % divide by pi to extract pi from theta    
    
    root = ["sqrt(" + num2str(x^2+y^2) + ")"]; % i.e "sqrt(2)"
    pi_value = ["(" + num2str(num) + "*pi/" + num2str(dem) + ")"]; 
        % i.e "(1*pi/4)" o "(2*pi/5)"...
    
    polarcoord = ["z = " + root + "e^(i" + pi_value]
    
    trigcoord = ["z = " + root + "(cos" + pi_value + ")+i*sin" + pi_value]
       
    end
    

    例子:

    coordinates(1,1)
    

    输出:

    极坐标 = "z = sqrt(2)e^(i(1*pi/4)"

    trigcoord = "z = sqrt(2)(cos(1pi/4))+isin(1*pi/4)"

    为了计算分数,我们使用rat() 函数从atan(y./x) 的双重输出中获取分子和分母,然后除以 pi 以从 theta 值中提取其值(我们表示pi 已经在字符串中)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多