Converting between x,y cartesian variables to angle in symbolic variables

Is there some way to write "Cartesian"-like constraints around symbolic variables? For example, is there any way to tell the toolbox that it should interpret x and y as Cartesian in the following code:
clear all;clc;close all
syms x y real
t=x/sqrt(x^2 + y^2);
And write "t" to be the cos of an angle between the (x,y) vector and the x-axis?

8 Comments

What is a Cartesian-like constraint? It seems like you want the toolbox to somehow understand from a line of code, what that code represents to you in terms of a model of some process. I fear that is a big jump to make. It could also be a mistake, where some expression arises from an entirely different system. And I think you do not want the software to be making that sort of a decision in general.
Thanks for your comment @John D'Errico - I'm actually struggling myself to understand what exactly the toolbox would need to do in order for me to get the output I want.
I have a variable point (x,y) in a plane and I have a function f(x,y | xa,ya) involving that variable point, given another fixed point (xa,ya). The function is arrived at via some complicated steps involving norm, derivatives, an integral etc. I would like the toolbox to be able to carry out all those operations and give me an output. It currently is doing that, but it is giving me a long complicated output equation in terms of x and y (and who can blame it, that is all it knows about what I want!). I'd like it to somehow involve trigonometric functions to simplify that long equation, because I know for some special cases that the equation simplifies to very simple expressions of the trigonometric functions. I guess what I need to do is somehow tell it the relation between "the angle" and x and y, because this is the model I have in mind of what x and y mean.
Trying to solve the simpler problem - with the addition of theta and d now:
clear all;clc;close all
syms x y real
theta=atan2(x,y);
d=sqrt(x^2 + y^2;)
t=x/sqrt(x^2 + y^2);
Can the toolbox understand that I mean x and y are coordinates of a point on a plane, and theta is the angle between them, d is the distance between them, and "understand" that t is the cos of theta? Is there any way to introduce the Cartesian structure to the toolbox?
Can the toolbox understand that I mean x and y are coordinates of a point on a plane, and theta is the angle between them, d is the distance between them, and "understand" that t is the cos of theta? Is there any way to introduce the Cartesian structure to the toolbox?
No. You should think about how to simplify calculation within f to get a simpler output. Of course, you can try to apply "simplify" on the output or substitute expressions in the output by using the "subs" command, but usually it's better to do simplifications beforehand.
Thanks for your feedback @Torsten - a bit disappointing to know that this can't be done in symmath. I think that some way of establishing a Cartesian structure between variables (even if through user defined expressions and substitutions, etc), would be very useful to get symmath to assist in analysis, as there are countless applications for it, i.e. anything involving spatial variables.
Thanks @John D'Errico. I think a coordinate change like you have done can be helpful, but for my particular case will be cumbersome due to the complexity of the expression and figuring out when to do it - I'll probably have to write in some control logic. Incorporating relations between variables (that's essentially what it is I think, x and y being related through functions of secondary variables r and theta) would be a wonderful future feature to have in symmath, it could be useful with common relations like Cartesian, etc. I can only think of "assume" which allows some sort of relation building between variables (but I suspect "under-the-hood" in code, this is just boolean T/F).
Thanks again.
BTW @John D'Errico, if you'd like to add your comment as an answer, then I can accept it? It is answering my original question.
I suppose one day, maybe sooner than I really expect to see, AI tools will be smart enough to do as you want. I'm not sure that would make me happy, but then, I grew up using a slide rule, using pencil and paper to do my work.
@John D'Errico, I'm not sure if this might require an AI approach. Perhaps I'm missing something further, but I think it may only require the addition of user-defined relations between the variables with the intermediate variables r,phi - your above code snippet is nearly there. Symmath already has simplify (and "IgnoreAnalyticConstraints" tells me that it does keep some identities "in mind" when simplifying, and decides when to use them too), and it already uses some default identities in rewrite too. There just needs to be a way to say "here, keep these user-defined relations in mind too and play around with them in simplifying". So adding such a feature that initializes the problem should be alright - it needs to figure out when to stop. Maybe an AI tool would be useful in that.

Sign in to comment.

 Accepted Answer

No, the toolbox will never try to understand your equations, and then infer what the variables mean to you, and then perform substitutions that you think right.
A problem is that even though you think of x and y as living in a cartesian coordinate system, to syms, it just sees variables. There is no understanding there, at least not yet. Maybe in the future. And certainly, it cannot automatically perform transformations like that.
In SOME circumstances, you can get rewrite to simplify some expressions, but that often just takes just trial and error in my experience. Or you can use simplify to give you many different alternate simplifications, then picking the one you like.
Of course, if you want it to make the transformations you are discussing here, you can do things like this:
syms x y
t=x/sqrt(x^2 + y^2);
syms theta
syms r real positive
simplify(subs(t,[x,y],[r*cos(theta),r*sin(theta)]))
ans = 
But here I have explicitly told it to do the transformations you seem to want.
Getting symbolic algebra tools to do as you wish can sometimes be a skill, sometimes an art, and sometimes a bit of magic is need to get the result you wish to see.

More Answers (1)

So instead of the Cartesian coordinate form of those points, you want a polar coordinate form?
syms x y
[th, r] = cart2pol(x, y)
th = 
r = 
Now you want to work with r and th instead of x and y?

Products

Release

R2022a

Asked:

on 26 May 2024

Edited:

on 27 May 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!