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)

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.

 採用された回答

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 件のコメント

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
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.

サインインしてコメントする。

その他の回答 (2 件)

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 件のコメント

I did what you said (the () were a stupid mistake on my part) so now I have a single function file:
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];
F = [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);];
end
I still receive the same error message:
Error using myfun325_1 (line 28) 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.
The argument of myfun should be a vector with all the variables inside it. So change the top part of myfun325_1 to:
function F = myfun325_1(xs)
x=xs(1);
y=xs(2);
z=xs(3);
d=xs(4);
Also remove x0=[1 1 1 1]; from inside myfun325_1.
Now I have:
function F = myfun325_1(xs)
x=xs(1); y=xs(2); z=xs(3); d=xs(4);
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;
F = [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);];
end
Still the same error message when running
>> x0=[1 1 1 1];
>> fsolve(@myfun325_1,x0)
Thanks for the help Paul.
Paul
Paul 2014 年 2 月 27 日
編集済み: Paul 2014 年 2 月 27 日
Are you sure you saved the file? I ran your code which is the same as I had and it runs without errors.
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 2014 年 2 月 27 日
編集済み: Paul 2014 年 2 月 27 日
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).

サインインしてコメントする。

Matt J
Matt J 2014 年 2 月 27 日
編集済み: Matt J 2014 年 2 月 27 日
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 件のコメント

Matt thanks for the answer. The problem I'm having is this is part of a project for and upper level Applied Mathematics course. The problem states:
Consider the scenario that four satellites positioned in the space with coordinates
Satellite 1 : (15600; 7540; 20140) Satellite 2 : (18760; 2750; 18610) Satellite 3 : (17610; 14630; 13480) Satellite 4 : (19170; 610; 18390)
in km are observing a receive position (x; y; z) near earth. Suppose that the measured time intervals are 0:07074; 0:07220; 0:07690; 0:07242 in seconds, respectively, with an unknown latency correction d. Solve the navigation equation r_i (x,y,z,d)=sqrt((x-x_i)^2 + (y-y_i)^2 + (z-z_i)^2)-c(t_i-d),i=1,2,3,4 for the position (x;y;z) of the receiver and the time correction d.
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 2014 年 2 月 28 日
編集済み: Matt J 2014 年 2 月 28 日
We've established that the equations have no solution. Re-organizing them into whatever equivalent form won't change that.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

タグ

質問済み:

2014 年 2 月 27 日

編集済み:

2014 年 2 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by