How to Solve matrix equations in Matlab
30 views (last 30 days)
Show older comments
Julian Blackthorne
on 20 Sep 2021
Commented: Julian Blackthorne
on 21 Sep 2021
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)
0 Comments
Accepted Answer
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);
x
[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)
0 Comments
More Answers (2)
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.
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)
bestF1 = solve(diff(residue, F(1)),F(1))
eqn2 = subs(residue, F(1), bestF1)
sol2 = solve(diff(eqn2, F(2)),F(2));
F2 = sol2;
F1 = subs(bestF1, F(2), F2);
F1
F2
F1n = double(F1)
F2n = double(F2)
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
Walter Roberson
on 20 Sep 2021
When you use 'returnconditions' then you get forms in which you can substitute different integer values for k (an automatically generated parameter) in order to get different periods (since tan() is periodic)
A more careful analysis would probably involve using returnconditions on bestF1 as well so that the atan() were in the periodic form.
format long g
syms F [1 2]
fun = root2d(F);
residue = sum(fun.^2)
bestF1 = solve(diff(residue, F(1)),F(1))
eqn2 = subs(residue, F(1), bestF1)
sol2 = solve(diff(eqn2, F(2)),F(2), 'returnconditions', true);
sol2.parameters
assume(sol2.parameters(1),'integer')
z_values = vpa(cellfun(@rhs, children(simplify(sol2.conditions))));
F2 = subs(sol2.F2, sol2.parameters(2), z_values(:));
F1 = subs(bestF1, F(2), F2);
F1
F2
kvals = -2:2;
F1n = double(subs(F1, sol2.parameters(1), kvals))
F2n = double(subs(F2, sol2.parameters(1), kvals))
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!