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)
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Star Strider
2014-2-27
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 个评论
Star Strider
2014-2-27
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 个)
Paul
2014-2-27
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 个评论
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Reference Applications 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!