Effacer les filtres
Effacer les filtres

why the + sign get invalid use of operator?

4 vues (au cours des 30 derniers jours)
wenchong chen
wenchong chen le 7 Mar 2021
Commenté : Steven Lord le 7 Mar 2021
f=@(x)40*x.^1.5-875*x.+350000
f=@(x)40*x.^1.5-875*x.+350000
Error: Invalid use of operator.

Réponses (2)

Stephen23
Stephen23 le 7 Mar 2021
Modifié(e) : Stephen23 le 7 Mar 2021
Note the difference:
1 + 2 % what MATLAB actually supports
1 .+ 2 % what you used
Invalid use of operator.
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:

wenchong chen
wenchong chen le 7 Mar 2021
I can only put f=@(x)40.*x^1.5-875.*x+350000 in it
  1 commentaire
Steven Lord
Steven Lord le 7 Mar 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)
ans = 349165
f(1:10)
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers, use '.^'.

Error in solution (line 1)
f=@(x)40.*x^1.5-875.*x+350000;

Connectez-vous pour commenter.

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by