what's wrong with this code?
조회 수: 1 (최근 30일)
추가 답변 (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)
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)
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])
fimplicit(x^2 -2*y^2 - 5,[-10,10])
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.