Effacer les filtres
Effacer les filtres

Getting the error "Dimensions of arrays being concatenated are not consistent" but I can't tell why. (See my code in the body of this post)

2 vues (au cours des 30 derniers jours)
I've been working on a coding assignment and keep receiving the error "Dimensions of arrays being concatenated are not consistent." I've narrowed down the location of the error to the following lines of code:
y = [1;1;1]; % this is our initial guess.
% the vector function g:
g = @(y) [2*(y(1)^2) - 4*y(1) + (y(2)^2) + 3*(y(3)^2) + 6*y(3) + 2; (y(1))^2 + (y(2))^2 -2*y(2)+2 (y(3)^2) - 5; 3*(y(1)^2)-12*y(1) + (y(2))^2 + 3*(y(3)^2) + 8];
%calling the vector function g with y = [1;1;1]
g(y)
The code for the vector function g defined above is written as one line in my program (just saying this in case it showed up as two lines in this post). I've been scratching my head with this for a while, I don't see how I can fix the error.

Réponses (2)

Rex
Rex le 10 Mar 2024
Nevermind guys, it seems it had to do with my order of operations.

Walter Roberson
Walter Roberson le 10 Mar 2024
(y(1))^2 + (y(2))^2 -2*y(2)+2 (y(3)^2) - 5
Inside [] that is interpreted as
(y(1))^2 + (y(2))^2 -2*y(2)+2, (y(3)^2) - 5
You are missing an operation between the 2 and the (y(3)^2)

Catégories

En savoir plus sur Creating and Concatenating Matrices 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