How can I solve non-linear equations with more equations than unknown variables using objective functions such as nonlinear least square (lsqnonlin)?

Hi, I am currently doing a project which requires fitting.
Suppose that I have two 3x1 vectors (P1 and P2) and two 3x4 matrices (M1 and M2). There are also two unknowns, C1 and C2.
They are related by: P1=M1 C1 and P2=M2 C2.
C1 is in the form of [x*k1 ; y*k1 ; z*k1 ; k1] while C2 is in the form of [x*k2 ; y*k2 ; z*k2 ; k2].
How can I find the most optimal x,y,z,k1, and k2?
I'm not too familiar with fit optimization functions or how to use them so any help would be much appreciated.
Thank you in advance for your help.
Sincerely,
Daniel

 Accepted Answer

rng("default")
P1 = rand(3,1);
P2 = rand(3,1);
M1 = rand(3,4);
M2 = rand(3,4);
x0 = [1 1 1 1 1];
fun1 = @(x) norm(P1-M1*[x(1)*x(4);x(2)*x(4);x(3)*x(4);x(4)])^2 + norm(P2-M2*[x(1)*x(5);x(2)*x(5);x(3)*x(5);x(5)]);
fun1(x0)
ans = 18.8722
sol1 = fmincon(fun1,x0)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
sol1 = 1×5
-0.8274 3.0550 -4.5344 -0.5156 -0.3000
fun1(sol1)
ans = 0.4078
fun2 = @(x)[P1-M1*[x(1)*x(4);x(2)*x(4);x(3)*x(4);x(4)];P2-M2*[x(1)*x(5);x(2)*x(5);x(3)*x(5);x(5)]]
fun2 = function_handle with value:
@(x)[P1-M1*[x(1)*x(4);x(2)*x(4);x(3)*x(4);x(4)];P2-M2*[x(1)*x(5);x(2)*x(5);x(3)*x(5);x(5)]]
sol2 = lsqnonlin(fun2,x0,[],[],[],[],[],[],[],optimset('MaxFunEvals',10000))
Local minimum possible. lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
sol2 = 1×5
1.0e+03 * -0.0654 -0.9974 1.6022 0.0000 0.0000
norm(fun2(sol2))^2
ans = 0.1887

2 Comments

Thank you for your help.
I've generated a random x,y, z, k1, and k2 put a random modifier on each of them. I then multiplied C1 and C2 with a random 3x4 matrix to produce P1 and P2. For this case specifically, sol2 seems to match the initial point better than sol1:
Example:
[x;y;z]=[8.14723686393179; 9.05791937075619; 1.26986816293506]
sol1=[-7.00438926888561; -8.19496886633831; -0.539859738815100; -0.0119678076141614; -0.00865879956012216]
sol2=[8.08793647354255; 8.99025490081366; 1.25180027090850; 0.00876215856687812; 0.00657248141296997]
The two methods from above are two possible ways to solve the problem using two different MATLAB solvers.
In my completely random example from above, the second method yielded the better fit (0.1887 < 0.4078). But the computed values for the parameters from the first method are more moderate than those of the second method.
I think you will have to test a little. In the end, all will most probably depend on your initial guess vector x0.

Sign in to comment.

More Answers (0)

Products

Release

R2023a

Community Treasure Hunt

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

Start Hunting!