MATLAB Answers

Can't solve for x with (a = y/x)

1 view (last 30 days)
GratefulLed
GratefulLed on 26 Mar 2021
Edited: John D'Errico on 27 Mar 2021
syms x rational positive integer
syms y rational positive integer
syms a
x = 3*y
eqn = a == y/x
s = solve (eqn, x)
The .mlx above seems simple enough, but the solution I'm getting for x is
s = struct with fields:
a: [0×1 sym]
y: [0×1 sym]
Shouldn't it simply be: s = y/a
Why am I getting this "struct with fields" solution? What does that mean in lamen's terms? Most importantly, how can I get Matlab to give me the actual solution instead of the struct with fields message?
  1 Comment
madhan ravi
madhan ravi on 26 Mar 2021
What? You define x = 3*y , doesn't the answer have to be a = 1/3 ?

Sign in to comment.

Answers (2)

madhan ravi
madhan ravi on 26 Mar 2021
Edited: madhan ravi on 26 Mar 2021
syms y a x
eqn = a == y/x;
s = solve(eqn, x)
s = 

John D'Errico
John D'Errico on 26 Mar 2021
Edited: John D'Errico on 27 Mar 2021
I'm not sure what a rational integer is. How is that distinct from an integer?
Anyway, assume x and y are integers, with a completely unspecified?
Lets see. You specify x and y, but then you overwrite x, as
syms x rational positive integer
syms y rational positive integer
syms a
x = 3*y
x = 
So now x is a function of y. It is NO longer an unknown integer. You overwriote the initial specification. People seem to fail to realize this, thinking the syms call is necessary to declare a variable. This is NOT Fortran, or some other language where you might do that.
Next, we see this:
eqn = a == y/x
eqn = 
eqn no longer has anything to do with x or y! We now simply have the relation that a = 1/3.
Using solve on it is irrelevant, since you cannot solve for something that has no relation to x anymore! Therefore, solve fails.
Now, what COULD you have done? This is difficult, since I have no idea what you really wanted to do. But let me start over.
syms x y positive integer
syms a
eqn = a == y/x
eqn = 
solve(eqn,x)
ans = Empty sym: 0-by-1
And this still fails, because all we know is that a is some number, but x and y are integers. While it seems that x = y/a, consider if a solution exists for any general value of a, and integer y? And the answer is no. So no solution was found.
I still have no clue what you really wanted to do. If I relax things further, ;like this:
syms a x y positive
eqn = a == y/x
eqn = 
solve(eqn,x)
ans = 
Then the answer is trivial. We get x, as a function of y and a. MATLAB is no longer worried that x and y MUST be positive integers, and therefore it finds a solution.
My guess is, you probably need to spend more time working through any tutorials with the symbolic toolbox and MATLAB in general, as it feels like you don't really understand variables and what they are, and why you use tools like syms.

Community Treasure Hunt

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

Start Hunting!