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 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
Walter Roberson
2024-3-10
(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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!