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

조회 수: 2(최근 30일)
GratefulLed 2021년 3월 26일
편집: John D'Errico 2021년 3월 27일
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표시 없음숨기기 없음
What? You define x = 3*y , doesn't the answer have to be a = 1/3 ?

댓글을 달려면 로그인하십시오.

### 답변(2개)

편집: madhan ravi 2021년 3월 26일
syms y a x
eqn = a == y/x;
s = solve(eqn, x)
s =
##### 댓글 수: 0표시 이전 댓글 수: -1숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

John D'Errico 2021년 3월 26일
편집: John D'Errico 2021년 3월 27일
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.

댓글을 달려면 로그인하십시오.

### 범주

Find more on Common Operations in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by