Undefined function or variable 'x' - Never seen before, mega frustrating!

So I've been using MATLAB for one of my University subjects this whole year without fault. It really is some great software. Fairly easy to use, and incredibly in-depth.
But yesterday, when I sat down to start work on a small assignment I had, I ran into a bit of a snag that I haven't been able to solve until now. It is frustrating me to the point of wanting to jump off a cliff.
Long story short, the simple command window line
>> eqn1 = 5*x + 3*y == 2
gives me an error reading
Undefined function or variable 'x'.
This is frustrating because just the other week I was able to do exactly that without any problem at all. MATLAB quite gladly ran
eqn1 = 5*x + 3*y == 2
eqn2 = 3*x + 2*y == 10
[A,B] = equationsToMatrix ([eqn1,eqn2],[x,y])
...meaning I had two matrices
A
5 3
3 2
and B
2
10
But now it doesn't even want to assign any values to my equation-variables.

1 件のコメント

Stephen23
Stephen23 2016 年 5 月 30 日
編集済み: Stephen23 2016 年 5 月 30 日
@Adebayo Akinfenwa: there are several possible reasons for why it worked before, but all of them come under the category of x was already defined before your ran that code.
However that is not really the point of MATLAB: it is not required to predefine any variables or specify them as variables in a function. The basic (easiest and fastest) way to use MATLAB is to simply define numeric values (scalar/vector/matrix) and perform operations on them: this is how all numeric calculation software works:

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2016 年 5 月 30 日
編集済み: Matt J 2016 年 5 月 30 日

1 投票

You need to declare x and y as symbolic variables
syms x y
Why you didn't encounter it before? My guess is that it was done in the base workspace by some script that you forgot about, and then the symbolic variables were never cleared, making them available to later code.

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 5 月 30 日

1 投票

Before you had previously defined x and y, though perhaps you did it without knowing it for forgot. Try defining them:
x = 1; % Whatever....
y = -1; % Whatever...
eqn1 = (5*x + 3*y == 2) % This will be a logical (true or false) result.
eqn2 = 3*x + 2*y == 10

カテゴリ

製品

質問済み:

2016 年 5 月 30 日

編集済み:

2016 年 5 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by