why the + sign get invalid use of operator?
조회 수: 4 (최근 30일)
이전 댓글 표시
f=@(x)40*x.^1.5-875*x.+350000
f=@(x)40*x.^1.5-875*x.+350000
↑
Error: Invalid use of operator.
댓글 수: 0
답변 (2개)
Stephen23
2021년 3월 7일
편집: Stephen23
2021년 3월 7일
Note the difference:
1 + 2 % what MATLAB actually supports
1 .+ 2 % what you used
There is no separate array version of the plus operator, it always operates array-wise.
Rather than guessing and inventing operators, it is much more reliable to follow the MATLAB documentation:
댓글 수: 0
wenchong chen
2021년 3월 7일
댓글 수: 1
Steven Lord
2021년 3월 7일
Yes, that looks correct to me, at least for scalar values of x. If you want f to accept non-scalar values of x you need to use elementwise matrix power rather than matrix power as the error message indicates.
f=@(x)40.*x^1.5-875.*x+350000;
f(1)
f(1:10)
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!