Hello
I have 2 nonlinear equations. I figure out that it can be solved by fsolve. The outputs of the 2 nonlinear equations are the [x,y] position of a point. The inputs of the system are initial estimate of the point [x0,y0] and some constants. I first tried to solve the nonlinear equations for one point, with the following code:
options = optimoptions('fsolve', 'Display', 'off');x0 = [ud(1,1);vd(1,1)]; % Initial guess of undistorted point [x,y]
[x, fval] = fsolve(@myfunction,x0,options, kc, x0);function F = myfunction(x, kc, x0)r = sqrt(x(1)^2 + x(2)^2);F = [ -x0(1) + (1 + kc(1)* r.^2 + kc(2)* r.^4 + kc(5)* r.^6 ).*x(1) + 2*kc(3).*x(1).*x(2) + kc(4)*(r.^2+2*x(1).^2), ... -x0(2) + (1 + kc(1)* r.^2 + kc(2)* r.^4 + kc(5)* r.^6 ).*x(2) + kc(3)*(r.^2 + 2*x(2).^2) + 2*kc(4) .*x(1) .*x(2)];end
The above code runs perfectly fine with one point [x,y]. However, I would like to apply it to large set of points. The set of points are stored in two m x n arrays, ud and vd, where [ ud(row,col), vd(row,col) ] forms the initial estimiate [x0,y0] for each array element. I tried to use nested for loop to run the above fsolve code for each array element. While it works, it takes decades to complete because m and n are large (>1500×1500). Is there any efficient way to apply the above code to each array element? Or is there any other way to do the same thing? Thanks!
Best Answer