how can I write a multiplication of variables to graph?

조회 수: 16 (최근 30일)
Erwin Avendaño
Erwin Avendaño 2018년 3월 11일
댓글: John D'Errico 2018년 3월 11일
I want to graph x. ^ 2-4 * xy +5 but matlab does not let me say that it is badly written or that it declares variable "xy" but it is that "xy" is not a variable, it is two variables multiplying, what should I do? :(

답변 (2개)

John D'Errico
John D'Errico 2018년 3월 11일
편집: John D'Errico 2018년 3월 11일
MATLAB does not know that you have decided that xy is the product of two variables. Mind reading is not a strong point for computers. Should it know that xy is not just a variable, named xy? You knew that you have to multiply by 4. So why not an operator between x and y?
Multiplication between variables x and y in MATLAB is accomplished by x*y or x.*y, depending on what you are doing, and the shapes of x and y.
Therefore, when you write xy, MATLAB looks for a variable named xy. Failing that, it looks for a function named xy. Failing that, it throws up its virtual hands, and gives up, throwing an error. I suppose, they might have decided to see if variables named x and y were defined separately. But suppose you wrote an expression with something like abcdefgh? Should MATLAB try to look if ANY combination of existing variables and functions could be used here, inserting an implicit * where it wanted? That is surely a fast path to buggy code.
  댓글 수: 5
Erwin Avendaño
Erwin Avendaño 2018년 3월 11일
What happens is that X, and if they have values because I put [x, y] = (2: .2: 4,1: .2: 6) but even so it tells me an error that declares the variable "xy"
John D'Errico
John D'Errico 2018년 3월 11일
If you try to define x and y as
[x, y] = (2: .2: 4,1: .2: 6)
This is not in itself legal MATLAB syntax. So x and y are never defined in the first place as you want to write it.
You could never write reliable code that understands how to combine variables automatically, if parts of the name are written like that. Since it is completely valid to define a variable with the name xy, then all MATLAB can do is see if you have already defined a variable by that name. Otherwise, MATLAB would essentially be limited to only single character variable names, thus the 52 possible variables {a,b,c,...,z,A,B,...,Z}. Surely it should be possible to define more than 52 variables in a programming language?

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


Steven Lord
Steven Lord 2018년 3월 11일
MATLAB does not implicitly multiply variables whose names are next to one another. If you want to use the product of x and y in your expression, you must use either the * operator (for matrix multiplication) or the .* operator (for element-wise multiplication) between them.
x = 1:10
y = x+1
productOfxAndy = x.*y

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by