2017-05-28 1 views
0

Je sais à propos de la fonction pol2cart dans Matlab mais je ne pense pas que ce soit ce que je veux, ou peut-être que je ne sais pas comment l'utiliser pour le rendre ce que je veux.Convertir le vecteur de Matlab cylindrique en cartésien

Supposons que vous avez le vecteur suivant:

here.

Dans Wolfram Mathematica, par exemple, vous pouvez écrire quelque chose comme:

this

et renvoie les coordonnées cartésiennes du vecteur ci-dessus.

La question est: Comment réaliser quelque chose comme ça dans Matlab? Cela aurait été beaucoup plus facile si je devais travailler avec des nombres et les avoir comme entrées pour pol2cart, mais ici j'ai besoin de mon vecteur affiché de la même façon que la sortie de la fonction TransformedField.

Merci, Irina

Répondre

2

Il n'y a rien de magique dans le fonctionnement de Mathematica. C'est simplement en tirant parti du change-of-basis between cylindrical and Cartesian coordinates. Voici une mise en œuvre rapide et sale pour effectuer quelque chose de similaire en utilisant des variables symboliques:

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 

qui renvoie une expression similaire à celle de Mathematica:

>> 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