# what's wrong with this code?

조회 수: 1 (최근 30일)
Mr. assassin hunter hunter . 2022년 12월 21일
편집: Bora Eryilmaz . 2022년 12월 21일
for some reason this code doesn't work I'm new to matlab and i copied this from my professor
>> syms x
>> syms y
>> [x,y] = solve('x^3 - x*y + 3 = 0', 'x^2 -2*y^2 - 5 = 0')
##### 댓글 수: 1없음 표시없음 숨기기
Geoff Hayes 2022년 12월 21일
@Mr. assassin hunter hunter - please explain what you mean by "this code doesn't work". What is happening? What do you expect to happen?

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

### 채택된 답변

Bora Eryilmaz 2022년 12월 21일
편집: Bora Eryilmaz 님. 2022년 12월 21일
You don't need quotes inside the solve() command and use '==' for equality instead of '=':
syms x
syms y
[x,y] = solve(x^3 - x*y + 3 == 0, x^2 -2*y^2 - 5 == 0);
vpa(x)
ans =
vpa(y)
ans =

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

### 추가 답변 (1개)

John D'Errico 2022년 12월 21일
편집: John D'Errico 님. 2022년 12월 21일
When you tell us that something does not work, you NEED to tell us what happens.
Was an error generated? SHOW THE COMPLETE ERROR MESSAGE. First, see that I have changed in what you wrote.
I used only one call to syms, since I'm lazy and there was no need for two.
Next, I did not put everything in quotes. There is no need for that. In fact, that will fail, at least in a current MATLAB release. It should work in older releases though. So you (or your professor) may be using an old release.
Finally, you used a single =, not a double == in what you wrote. A single = is used for assignment. So you write
x = 5;
to assign the number 5 to x. But you use
x == 5
to test if two values are the same.
In your case, you need to use the comparison operator. So lets try the subtly modified code, and see what happens.
syms x y
[x,y] = solve(x^3 - x*y + 3 == 0, x^2 -2*y^2 - 5 == 0)
x =
y =
Ok, so solve gave something that does not appear terribly useful. This is because what you wrote is equivalent to a degree 6 polynomial. And most polynomials of degree higher than 4 cannot be factored using algebraic manipulations. Sometimes, really simple cases are an exception. This is not one of those simple exceptions.
However, that does not mean no solution can be found, EVER. The vpasolve tool can solve polynomial problems like this. It is a numerical rootfinder.
syms x y
[x,y] = vpasolve(x^3 - x*y + 3 == 0, x^2 -2*y^2 - 5 == 0)
x =
y =
There are 6 solutions to the problem you wrote, all of which are complex. We can understand better why that is, if we just plot the two relations on the same set of axes. fimplicit is a truly great tool for this purpose.
syms x y
fimplicit(x^3 - x*y + 3,[-10 10])
hold on
fimplicit(x^2 -2*y^2 - 5,[-10,10])
xlabel X
ylabel Y
grid on
I can expand the axes as far as you like. However, it appears the red and the clue curves never intersect for real variables x and y. This is a conclusion you would arrive at, if you accept that the only solutions are all complex valued.
So, while your professor may have done something that you think you copied, I'm not sure what was there. But your code would fail for several good reasons.
Regardless, in the future, when you tell us that something does not work, tell us more than that. Tell us what it DOES do, and why there seems to be a problem.

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

### 카테고리

Help CenterFile Exchange에서 Common Operations에 대해 자세히 알아보기

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by