plot with equals in matlab

5 visualizzazioni (ultimi 30 giorni)
Noa  Yelin
Noa Yelin il 19 Ott 2020
Modificato: Noa Yelin il 19 Ott 2020
hey, what is the problem with this code -
x=linespace(-10,10,2);
yy=sin(x)+0.2*sin(2x)-0.3*sin(3x);
zz=1/0.1+abs(sin(x)+0.2*sin(2x)-0.3*sin(3x));
hold on
plot(x,yy,'--g')
plot(x,zz,'-r')
hold off
axis equal
grid on
legend('y(x)','g(x)')
i get this note -
Error: File: Untitled4 Line: 3 Column: 20
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax
error. To construct matrices, use brackets instead of parentheses.

Risposte (1)

Walter Roberson
Walter Roberson il 19 Ott 2020
yy=sin(x)+0.2*sin(2x)-0.3*sin(3x);
MATLAB has absolutely no implied multiplication anywhere. Not even in the symbolic toolbox. 2x is not a valid expression in MATLAB.
The only cases in which a number can be directly followed by a letter in MATLAB are:
  • in the middle of an identifier (which would have to start with a letter, not a number or underscore)
  • in a single-quoted character vector or double-quoted string object
  • in a comment
  • A number may be directly followed by i or j to indicate multiplication by sqrt(-1), such as 5i meaning 5*sqrt(-1). i and j are case-sensitive for this purpose
  • A number may be directly followed by e or E or d or D provided that is followed by an optional + or - that is then followed by an integer; this indicates an exponent. For example 5.2e3
  • A 0 at the beginning of a number may be immediately followed by a b that is then followed by a series of 0 and 1 to indicate binary numbers. The sequence of binary numbers may be followed by s or u and then the integer 8, 16, 32, or 64
  • A 0 at the beginning of a number may be immediately followed by a x that is then followed by a series of hex digits 0 to 9, A-F or a-f, to indicate hexadecimal numbers. The sequence of hexadecimal numbers may be followed by s or u and then the integer 8, 16, 32, or 64
In all other cases, you need an operator between the number and the letter, such as 2*x intead of 2x.
  4 Commenti
Walter Roberson
Walter Roberson il 19 Ott 2020
Is there a point in calculationg 1/0.1 instead of just writing 10 instead?
Or is zz supposed to be 1 divided by a long expression? Remember to use () as needed.
Noa  Yelin
Noa Yelin il 19 Ott 2020
Modificato: Noa Yelin il 19 Ott 2020
Thank you! I did it!

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by