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.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 10월 19일

1 개 추천

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

Noa  Yelin
Noa Yelin 2020년 10월 19일
편집: Noa Yelin 2020년 10월 19일
ok, I wrote this code like this -
%%task number 1
x=linespace(-10,10,100);
yy=sin(x)+0.2*sin(2*x)-0.3*sin(3*x);
zz=1/0.1+abs(sin(x)+0.2*sin(2*x)-0.3*sin(3*x));
hold on
plot(x,yy,'--g')
plot(x,zz,'-r')
hold off
axis equal
grid on
legend('y(x)','g(x)')
and then, the note was -
Error in Untitled4 (line 2)
x=linespace(-10,10,100);
Walter Roberson
Walter Roberson 2020년 10월 19일
There is no MATLAB function named linespace, but there is a linspace ( lin not line )
Walter Roberson
Walter Roberson 2020년 10월 19일
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 2020년 10월 19일
편집: Noa Yelin 2020년 10월 19일
Thank you! I did it!

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2020년 10월 19일

편집:

2020년 10월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by