what's wrong with this code?

1 view (last 30 days)
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 Comment
Geoff Hayes
Geoff Hayes on 21 Dec 2022
@Mr. assassin hunter hunter - please explain what you mean by "this code doesn't work". What is happening? What do you expect to happen?

Sign in to comment.

Accepted Answer

Bora Eryilmaz
Bora Eryilmaz on 21 Dec 2022
Edited: Bora Eryilmaz on 21 Dec 2022
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 = 

More Answers (1)

John D'Errico
John D'Errico on 21 Dec 2022
Edited: John D'Errico on 21 Dec 2022
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.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!