Can't solve for x with (a = y/x)
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Antworten (2)
madhan ravi
am 26 Mär. 2021
Bearbeitet: madhan ravi
am 26 Mär. 2021
syms y a x
eqn = a == y/x;
s = solve(eqn, x)
0 Kommentare
John D'Errico
am 26 Mär. 2021
Bearbeitet: John D'Errico
am 27 Mär. 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
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 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
solve(eqn,x)
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
solve(eqn,x)
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!