MATLAB: Problem in defining vector variable using ellipsis when code requires more than one line

breaking lines of codingellipsisMATLAB

I have a function which requires a vector variable y to be defined. The vector has 3 rows – y(1), y(2) & y(3), each defined by long equations which require more than one line of coding. When I use ellipsis at the end of a line I get an error message:- "unable to perform assignment because the size of the left side is the size of the right hand side did not match the size of the indexing on the left hand side". I am sure this is because the use of ellipsis adds spaces and this is interpreted as Y having a greater number of rows than 3. For example, if I define y(1), y(2) and y(3) each using 2 lines of coding using ellipsis then the vector y is probably interpreted as having 6 rows, which is incorrect.
I get the error message with the following simple example where I split the first line of code. Note that it is not necessary to split the first line of coding in this example but it is done to show the problem that results.
%Nested function that computes the objective function
function y = nestedfun(x)
y(1) = [(a*x(1))+(b*(x(2))^2)...
-(a*(x(3))^2)+14];
y(2)=[(a*(x(1))^2)+(b*x(2))-(b*(x(3))^2)+24];
y(3)=[(b*(x(1))^2)-(c*(x(2))^2)+(a*x(3))-19];

Best Answer

y(1) = [(a*x(1))+(b*(x(2))^2)...
-(a*(x(3))^2)+14];
is not the same as
y(1) = [(a*x(1))+(b*(x(2))^2)-(a*(x(3))^2)+14];
Instead it is the same as
y(1) = [(a*x(1))+(b*(x(2))^2) -(a*(x(3))^2)+14];
which defines two array elements.
Spacing matters.
The easiest way to repair this is to change the code to
y(1) = [(a*x(1))+(b*(x(2))^2)...
- (a*(x(3))^2)+14];