MATLAB Answers

How to Solve matrix equations in Matlab

14 views (last 30 days)
I am trying to solve the following set of matrix equations
The values of r1, r2, r3 and ψare known. The values of θ and ϕ are to be found by solving this equation in matlab. i attempt to do this using the fsolve function. However, it is not able to arrive to a solution. Is there a better way to solve this?
function F = root2d(Ficksang)
rfinal = [0,-0.101233861621737,0.365119069777688];
theta_f = Ficksang(1);
phi_f = Ficksang(2);
psi_f = 0;
r1 = rfinal(1);
r2 = rfinal(2);
r3 = rfinal(3);
F(1) = r1 - ( ( tan(psi_f/2) - (tan(theta_f/2) * tan(phi_f/2)) ) / ...
( 1 + (tan(theta_f/2) * tan(phi_f/2) * tan(psi_f/2)) ) );
F(2) = r2 - ( ( tan(phi_f/2) + (tan(theta_f/2) * tan(psi_f/2)) ) / ...
( 1 + (tan(theta_f/2) * tan(phi_f/2) * tan(psi_f/2)) ) );
F(3) = r3 - ( ( tan(theta_f/2) - (tan(phi_f/2) * tan(psi_f/2)) ) / ...
( 1 + (tan(theta_f/2) * tan(phi_f/2) * tan(psi_f/2)) ) );
fun = @root2d;
x0 = [0,0];
x = fsolve(fun,x0)

Accepted Answer

Matt J
Matt J on 20 Sep 2021
Edited: Matt J on 20 Sep 2021
With an over-determined system (3 equations and only 2 unknowns), you can't expect an exact solution. However, the solution that fsolve does find does seem to be valid as a least squares solution, judging from the surface plot below.
fun = @root2d;
x0 = [0,0];
[x,f] = fsolve(fun,x0);
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead.
No solution found. fsolve stopped because the last step was ineffective. However, the vector of function values is not near zero, as measured by the value of the function tolerance.
x
x = 1×2
0.6950 -0.1785
[Theta,Phi]=ndgrid( linspace(-pi/2,pi/2,300));
fun=@(x,y)norm(root2d([x,y]));
F= arrayfun(fun,Theta,Phi);
surf(Theta,Phi,F,'EdgeColor','none')
xlabel 'Theta', ylabel 'Phi'
view(60,65)

More Answers (2)

Sargondjani
Sargondjani on 20 Sep 2021
Your method seems valid. But of course fsolve will only attempt to find a local solution, and it might get stuck in a place where there is locally no solution.
Did you try with other starting values? This might work in general if you know where your solutions should approximately be.
  1 Comment
Julian Blackthorne
Julian Blackthorne on 20 Sep 2021
I did try with a couple of other initial values but fsolve seems to still have ineffective steps leading to no solution at all.

Sign in to comment.


Walter Roberson
Walter Roberson on 20 Sep 2021
Optimal solutions, in the sense of smallest least-squared.
Are there other solutions? Yes: you can ask to solve eqn2 with return conditions set, and the answer will be parameterized . One of the variables of parameterization, k will add multiples of pi .
(The other one is a nuisance variable -- the expression inside the root() is pulled out into a parameterized variable and the "conditions" are that the fifth degree polynomial = 0. And then to express F2 you have to extract the expression from the conditions and wrap it with a root() yourself... Do-able, but a nuisance.)
format long g
syms F [1 2]
fun = root2d(F);
residue = sum(fun.^2)
residue = 
bestF1 = solve(diff(residue, F(1)),F(1))
bestF1 = 
eqn2 = subs(residue, F(1), bestF1)
eqn2 = 
sol2 = solve(diff(eqn2, F(2)),F(2));
F2 = sol2;
F1 = subs(bestF1, F(2), F2);
F1
F1 = 
F2
F2 = 
F1n = double(F1)
F1n = 0.6950
F2n = double(F2)
F2n = -0.1785
function F = root2d(Ficksang)
rfinal = [0,-0.101233861621737,0.365119069777688];
theta_f = Ficksang(1);
phi_f = Ficksang(2);
psi_f = 0;
r1 = rfinal(1);
r2 = rfinal(2);
r3 = rfinal(3);
F(1) = r1 - ( ( tan(psi_f/2) - (tan(theta_f/2) * tan(phi_f/2)) ) / ...
( 1 + (tan(theta_f/2) * tan(phi_f/2) * tan(psi_f/2)) ) );
F(2) = r2 - ( ( tan(phi_f/2) + (tan(theta_f/2) * tan(psi_f/2)) ) / ...
( 1 + (tan(theta_f/2) * tan(phi_f/2) * tan(psi_f/2)) ) );
F(3) = r3 - ( ( tan(theta_f/2) - (tan(phi_f/2) * tan(psi_f/2)) ) / ...
( 1 + (tan(theta_f/2) * tan(phi_f/2) * tan(psi_f/2)) ) );
end
  3 Comments
Julian Blackthorne
Julian Blackthorne on 21 Sep 2021
@Walter Roberson Thank you so much for taking your time to explain this.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!