Matlab confuses variable with matrix
Mostrar comentarios más antiguos
So I declare my function as: f=@(x) 54*(x^6) + 45*(x^5) - 102*(x^4) - 69*(x^3) + 35*(x^2) + 16*x - 4 and I get these errors:
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 Bisection2>@(x)54*(x^6)+45*(x^5)-102*(x^4)-69*(x^3)+35*(x^2)+16*x-4 (line 6)
f=@(x) 54*(x^6) + 45*(x^5) - 102*(x^4) - 69*(x^3) + 35*(x^2) + 16*x - 4
Respuestas (1)
Star Strider
el 15 de Dic. de 2019
Apparently, ‘x’ is a vector (or array). Use element-wise exponentiation (.^) in that event:
f=@(x) 54*(x.^6) + 45*(x.^5) - 102*(x.^4) - 69*(x.^3) + 35*(x.^2) + 16*x - 4
3 comentarios
Christina Mil
el 15 de Dic. de 2019
Walter Roberson
el 15 de Dic. de 2019
Your f is being passed a vector in a context where the surrounding code expects a scalar.
If you are writing typical bisection code you probably have something like
if f(a)*f(b) < 0
That code would be incorrect for the case where a and b are vectors.
I speculate that you are trying to keep track of all of the a and b values in vectors but forgot to index them to only get the current ones when you call f
Star Strider
el 15 de Dic. de 2019
@Walter — Thank you!
(There were no multiplications in the original Question.)
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!