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)

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

Accepted Answer

Star Strider
Star Strider on 27 Feb 2014
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.)
  2 Comments
Jarrett White
Jarrett White on 27 Feb 2014
I ran the code, without all the ():
function F = myfun325_1(x,y,z,d)
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];
%myfun325_1 = @(x,y,z,d) [sqrt(((x - x1).^2) + ((y - y1).^2) + ((z - z1).^2)) - c*(t1 - d); sqrt(((x - x2).^2) + ((y - y2).^2) + ((z - z2).^2)) - c*(t2 - d); sqrt(((x - x3).^2) + ((y - y3).^2) + ((z - z3).^2)) - c*(t3 - d); sqrt(((x - x4).^2) + ((y - y4).^2) + ((z - z4).^2)) - c*(t4 - d);];
%Redefine x, y, z, d as p(1) ... p(4) myfun325_1 = @(p) [sqrt(((p(1) - x1).^2) + ((p(2) - y1).^2) + ((p(3) - z1).^2)) - c*(t1 - p(4)); sqrt(((p(1) - x2).^2) + ((p(2) - y2).^2) + ((p(3) - z2).^2)) - c*(t2 - p(4)); sqrt(((p(1) - x3).^2) + ((p(2) - y3).^2) + ((p(3) - z3).^2)) - c*(t3 - p(4)); sqrt(((p(1) - x4).^2) + ((p(2) - y4).^2) + ((p(3) - z4).^2)) - c*(t4 - p(4));];
estp = fsolve(myfun325_1, x0);
x = estp(1) y = estp(2) z = estp(3) d = estp(4)
end
I didn't get an error message but MATLAB returned:
>>myfun325_1
No solution found.
fsolve stopped because the relative size of the current step is less than the default value of the step size tolerance squared, but the vector of function values is not near zero as measured by the default value of the function tolerance.
x =
3.6317e+03
y =
400.3288
z =
3.5781e+04
d =
0.0730
Star Strider
Star Strider on 27 Feb 2014
I get close to those same values (and a similar notification when the solver finishes) even when I add:
opts = optimoptions(@fsolve, 'FinDiffType','central', 'MaxFunEvals',50000, 'MaxIter',10000, 'TolFun',1E-10, 'TolX',1E-10);
estp = fsolve(myfun325_1, x0, opts);
The problem may be that your function has at least one complex zero. The optimization functions only return real values.
From the documentation: fsolve only handles real variables. When x has complex variables, the variables must be split into real and imaginary parts.
I haven’t done that in a while, so one way I suggest you might go about coding it would be to refer to the imaginary parts of your variables as p(5) ... p(8), so x becomes p(1)+j*p(5) and so on for the other variables. Beyond that, I have no suggestions.

Sign in to comment.

More Answers (2)

Paul
Paul on 27 Feb 2014
change all x(1), y(1) etc to x1,y1 etc. Also put:
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;
inside the function file myfun325_1.
  7 Comments
Jarrett White
Jarrett White on 27 Feb 2014
Yes I saved it but when I run it now it returns:
No solution found.
fsolve stopped because the relative size of the current step is less than the default value of the step size tolerance squared, but the vector of function values is not near zero as measured by the default value of the function tolerance.
ans =
1.0e+04 *
0.3632 0.0400 3.5781 0.0000
Paul
Paul on 27 Feb 2014
Edited: Paul on 27 Feb 2014
Yes, either your equations dont have a solution (check if they are correct) or you should try other initial guesses (although I don't think this will help in this case).

Sign in to comment.


Matt J
Matt J on 27 Feb 2014
Edited: Matt J on 27 Feb 2014
Below is my suggestion for how to rewrite the problem. It basically converts things to more manageable units and gets rid of the sqrt() operations. Without it, you will have points of non-differentiability and also have to worry about the algorithm knowing that c*(t(1) - d) is supposed to be positive.
The rewrite also makes it pretty clear, I think, why the problem has no solution with the data given. The distance of (x,y,z) from (x1,y1,z1) is supposed to be D=c*(t(1) - d). Then the triangle inequality says that the distance from (x2,y2,z2) is at most
D+norm([x1,y1,z1]-[x2,y2,z2]) = D+5.9389
However, your second inequality insists that it be D+q2 = D+437.6970
function F = myfun325_1(p)
x1 = 15.600; y1 = 7.540; z1 = 20.140;
x2 = 18.760; y2 = 2.750; z2 = 18.610;
x3 = 17.610; y3 = 14.630; z3 = 13.480;
x4 = 19.170; y4 = .610; z4 = 18.390;
t1 = 0.07074; t2 = 0.07220; t3 = 0.07690; t4 = 0.07242;
c = 299792.458;
q2=c*(t2-t1);
q3=c*(t3-t1);
q4=c*(t4-t1);
x=p(1); y=p(2); z=p(3); D=p(4);
F = [((x - x1).^2) + ((y - y1).^2) + ((z - z1).^2) - D^2;
((x - x2).^2) + ((y - y2).^2) + ((z - z2).^2) - (D+q2)^2;
((x - x3).^2) + ((y - y3).^2) + ((z - z3).^2) - (D+q3)^2;
((x - x4).^2) + ((y - y4).^2) + ((z - z4).^2) - (D+q4)^2];
  3 Comments
Jarrett White
Jarrett White on 27 Feb 2014
Since there are four equations in four unknowns, subtracting the first equation from the last three would lead to three linear equations in (x,y,z). A solution could be found using Gaussian elimination. Then I could obtain a quadratic equation in d upon substitution. Should I modify the code to go this route?
Matt J
Matt J on 28 Feb 2014
Edited: Matt J on 28 Feb 2014
We've established that the equations have no solution. Re-organizing them into whatever equivalent form won't change that.

Sign in to comment.

Categories

Find more on Configure and View Diagnostics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!