MATLAB: I continue to receive an error message when I try to use fsolve. Can anyone tell me what I’m doing wrong? (I’m using MATLAB R2012a)

fsolve

First I run the following script.
x1 = 15600; y1 = 7540; z1 = 20140;
x2 = 18760; y2 = 2750; z2 = 18610;
x3 = 17610; y3 = 14630; z3 = 13480;
x4 = 19170; y4 = 610; z4 = 18390;
t1 = 0.07074; t2 = 0.07220; t3 = 0.07690; t4 = 0.07242;
c = 299792458;
x0 = [1 1 1 1];
Now my function to find x,y,z, and d.
function F = myfun325_1(x,y,z,d)
F = [sqrt(((x – x(1)).^2) + ((y – y(1)).^2) + ((z – z(1)).^2)) – c*(t(1) – d); sqrt(((x – x(2)).^2) + ((y – y(2)).^2) + ((z – z(2)).^2)) – c*(t(2) – d); sqrt(((x – x(3)).^2) + ((y – y(3)).^2) + ((z – z(3)).^2)) – c*(t(3) – d); sqrt(((x – x(4)).^2) + ((y – y(4)).^2) + ((z – z(4)).^2)) – c*(t(4) – d);];
end
I run the code: fsolve(@myfun325_1,x0) and am returned the following error:
Error using myfun325_1 (line 3) Not enough input arguments.
Error in fsolve (line 241) fuser = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.

Best Answer

You made some coding errors. Note that x1 is not the same as x(1). Also, if you look closely at the documentation for fsolve, all your unknown variables have to be members of the same vector. I reformatted your code:
x(1) = 15600; y(1) = 7540; z(1) = 20140;
x(2) = 18760; y(2) = 2750; z(2) = 18610;
x(3) = 17610; y(3) = 14630; z(3) = 13480;
x(4) = 19170; y(4) = 610; z(4) = 18390;
t(1) = 0.07074; t(2) = 0.07220; t(3) = 0.07690; t(4) = 0.07242;
c = 299792458;
x0 = [1 1 1 1];
% myfun325_1 = @(x,y,z,d) [sqrt(((x - x(1)).^2) + ((y - y(1)).^2) + ((z - z(1)).^2)) - c*(t(1) - d); sqrt(((x - x(2)).^2) + ((y - y(2)).^2) + ((z - z(2)).^2)) - c*(t(2) - d); sqrt(((x - x(3)).^2) + ((y - y(3)).^2) + ((z - z(3)).^2)) - c*(t(3) - d); sqrt(((x - x(4)).^2) + ((y - y(4)).^2) + ((z - z(4)).^2)) - c*(t(4) - d);];
% Redefine x, y, z, d as p(1) ... p(4)
myfun325_1 = @(p) [sqrt(((p(1) - x(1)).^2) + ((p(2) - y(1)).^2) + ((p(3) - z(1)).^2)) - c*(t(1) - p(4)); sqrt(((p(1) - x(2)).^2) + ((p(2) - y(2)).^2) + ((p(3) - z(2)).^2)) - c*(t(2) - p(4)); sqrt(((p(1) - x(3)).^2) + ((p(2) - y(3)).^2) + ((p(3) - z(3)).^2)) - c*(t(3) - p(4)); sqrt(((p(1) - x(4)).^2) + ((p(2) - y(4)).^2) + ((p(3) - z(4)).^2)) - c*(t(4) - p(4));];
estp = fsolve(myfun325_1, x0);
x = estp(1)
y = estp(2)
z = estp(3)
d = estp(4)
It ran without problems. (I did not change anything else in myfun325_1.)