how can I write a multiplication of variables to graph?

77 visualizzazioni (ultimi 30 giorni)
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? :(

Risposte (2)

John D'Errico
John D'Errico il 11 Mar 2018
Modificato: John D'Errico il 11 Mar 2018
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 Commenti
Erwin Avendaño
Erwin Avendaño il 11 Mar 2018
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 il 11 Mar 2018
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?

Accedi per commentare.


Steven Lord
Steven Lord il 11 Mar 2018
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

Categorie

Scopri di più su Function Creation in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by