why the + sign get invalid use of operator?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
f=@(x)40*x.^1.5-875*x.+350000
f=@(x)40*x.^1.5-875*x.+350000
↑
Error: Invalid use of operator.
0 Kommentare
Antworten (2)
Stephen23
am 7 Mär. 2021
Bearbeitet: Stephen23
am 7 Mär. 2021
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 Kommentare
wenchong chen
am 7 Mär. 2021
1 Kommentar
Steven Lord
am 7 Mär. 2021
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)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!