why the + sign get invalid use of operator?
2 次查看(过去 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 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!