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)

3 views (last 30 days)
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.

Answers (2)

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

Walter Roberson
Walter Roberson on 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)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!