2017-05-28 42 views
0

我知道Matlab中的pol2cart函數,但我不認爲這是我想要的,或者我不知道如何使用它來返回我想要的。將矢量從圓柱轉換到笛卡爾Matlab

假設你有以下的載體:

here

在沃爾弗拉姆的Mathematica,例如,你可以寫這樣的:

this

,並返回上述矢量的直角座標系。

問題是:我如何在Matlab中實現這樣的功能?如果我只需處理數字並將其作爲pol2cart的輸入,但在這裏我需要我的矢量顯示與TransformedField函數的輸出類似,那將會容易得多。

感謝, 伊琳娜

回答

2

沒有什麼神奇的數學操作。這僅僅是利用change-of-basis between cylindrical and Cartesian coordinates。這裏是一個快速和骯髒的執行使用符號變量進行類似的東西:

function vcar = cyl2car(vcyl) 
    % 
    % The elements of vcyl are expected to be order [v_r ; v_theta ; v_z] such that 
    % vcyl = v_r * rhat + v_theta * thetahat + v_z * zhat. 
    % 
    % The element of vcar will then be in the order: [v_x ; v_y ; v_z] such that 
    % vcar = v_x * xhat + v_y * yhat + v_z * zhat. 
    % 
    % For symbolic input, the expected symbolic symbols are [r,theta,z] for cylindrical 
    % and [x,y,z] for Cartesian. 
    % 

    % Declarations and change-of-basis 
    syms x y z r theta xhat yhat zhat rhat thetahat; 
    rhat  = x/sqrt(x^2 + y^2) * xhat + y/sqrt(x^2 + y^2) * yhat; 
    thetahat = -y/sqrt(x^2 + y^2) * xhat + x/sqrt(x^2 + y^2) * yhat; 

    % Substitute and simplify 
    temp = simplify(subs(vcyl,[r,theta],[sqrt(x^2+y^2),atan(y/x)])); 
    temp = expand(sum(temp .* [rhat ; thetahat ; zhat])); 

    % Assign 
    vcar = sym(0)*vcyl; 
    [c,t] = coeffs(temp,[xhat,yhat,zhat]); 
    if (length(t)>=1) 
     vcar(1) = c(1); 
    end 
    if (length(t)>=2) 
     vcar(2) = c(2); 
    end 
    if (length(t)>=3) 
     vcar(3) = c(3); 
    end  
end 

它返回從數學類似的表述到:

>> vcyl = [0 ; sym(64)*sym(10^-7)*sym(pi)/(sym(2)*r) ; 0]; 
>> vcarsym = cyl2car(vcyl) 
vcarsym = 
-(pi*y)/(312500*(x^2 + y^2)) 
    (pi*x)/(312500*(x^2 + y^2)) 
          0