Reputation: 983
What is the problem with this code?
function F = Final_Project_2(x)
F = [(1/x(1)) + (1/x(2))- (2/(7*x(3)));
(x(3)+2*x(4))*(15*x(2))/((x(1)+x(2))*x(3)-0.7/x(3))-14;
(x(3)*((0.576*x(2)/(x(1)+x(2))*x(3)) - 0.27/x(3)))/(x(3)+(0.576*x(2)/(x(1)+x(2))*x(3)) - 0.27/x(3)) -20;
((0.576*x(2)*x(3)/(x(1)+x(2))*x(3)) - 0.27*x(4)/x(3))-100];
And here is the workspace code:
x0 = [1; 1; 1; 1]; % Make a starting guess at the solution
options=optimset('Display','iter'); % Option to display output
[x,fval] = fsolve(@Final_Project_2,x0,options) % Call optimizer
??? Error using ==> vertcat
CAT arguments dimensions are not consistent.
Error in ==> Final_Project_2 at 5
F = [(1/x(1)) + (1/x(2))- (2/(7*x(3)));
Error in ==> fsolve at 254
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot
continue.
Upvotes: 3
Views: 590
Reputation: 9317
Matlab fails to concatenate the following array:
F = [(1/x(1)) + (1/x(2))- (2/(7*x(3)));
(x(3)+2*x(4))*(15*x(2))/((x(1)+x(2))*x(3)-0.7/x(3))-14;
(x(3)*((0.576*x(2)/(x(1)+x(2))*x(3)) - 0.27/x(3)))/(x(3)+(0.576*x(2)/(x(1)+x(2))*x(3)) - 0.27/x(3)) -20;
((0.576*x(2)*x(3)/(x(1)+x(2))*x(3)) - 0.27*x(4)/x(3))-100];
The problem occurs in the third row where the last -20
is interpreted as a single value in a vector rather than a part of the term you are calculating. To demonstrate this, you can bracket each row of this array and have a look at the results:
x = [1 2 3 4]; % just for demonstration purpuse
[(1/x(1)) + (1/x(2))- (2/(7*x(3)));]
[ (x(3)+2*x(4))*(15*x(2))/((x(1)+x(2))*x(3)-0.7/x(3))-14;]
[ (x(3)*((0.576*x(2)/(x(1)+x(2))*x(3)) - 0.27/x(3)))/(x(3)+(0.576*x(2)/(x(1)+x(2))*x(3)) - 0.27/x(3)) - 20;]
[ ((0.576*x(2)*x(3)/(x(1)+x(2))*x(3)) - 0.27*x(4)/x(3))-100]
This results in
ans =
1.4048
ans =
23.6426
ans =
0.7843 -20.0000
ans =
-96.9040
Please note the 1x2 vector in the third row.
To solve your problem, either put more emphasis on placing spaces in your calculation (i.e., omit all spaces, or write - 20
instead of -20
in your example), or put everything that belongs to one term in parentheses.
Upvotes: 3