How to Solve matrix equations in Matlab
Ältere Kommentare anzeigen
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)
Akzeptierte Antwort
Weitere Antworten (3)
Sargondjani
am 20 Sep. 2021
1 Stimme
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 Kommentar
Julian Blackthorne
am 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 Kommentare
Julian Blackthorne
am 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
Julian Blackthorne
am 21 Sep. 2021
DEENA DHAYALAN
am 12 Feb. 2025
Bearbeitet: Walter Roberson
am 12 Feb. 2025
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
Kategorien
Mehr zu Linear Algebra finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








