How to Solve matrix equations in Matlab

30 次查看(过去 30 天)
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)

采纳的回答

Matt J
Matt J 2021-9-20
编辑:Matt J 2021-9-20
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)

更多回答(2 个)

Sargondjani
Sargondjani 2021-9-20
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 个评论
Julian Blackthorne
Julian Blackthorne 2021-9-20
I did try with a couple of other initial values but fsolve seems to still have ineffective steps leading to no solution at all.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2021-9-20
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 个评论
Walter Roberson
Walter Roberson 2021-9-20
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)
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), 'returnconditions', true);
sol2.parameters
ans = 
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
F1 = 
F2
F2 = 
kvals = -2:2;
F1n = double(subs(F1, sol2.parameters(1), kvals))
F1n =
0.695035633270614 + 0i 0.695035633270614 + 0i 0.695035633270614 + 0i 0.695035633270614 + 0i 0.695035633270614 + 0i 1.44113625911814 - 3.69313313087171i 1.44113625911814 - 3.69313313087171i 1.44113625911814 - 3.69313313087171i 1.44113625911814 - 3.69313313087171i 1.44113625911814 - 3.69313313087171i 1.44113625911814 + 3.69313313087171i 1.44113625911814 + 3.69313313087171i 1.44113625911814 + 3.69313313087171i 1.44113625911814 + 3.69313313087171i 1.44113625911814 + 3.69313313087171i -1.78865407575345 + 3.72460215957041i -1.78865407575345 + 3.72460215957041i -1.78865407575345 + 3.72460215957041i -1.78865407575345 + 3.72460215957041i -1.78865407575345 + 3.72460215957041i -1.78865407575345 - 3.72460215957041i -1.78865407575345 - 3.72460215957041i -1.78865407575345 - 3.72460215957041i -1.78865407575345 - 3.72460215957041i -1.78865407575345 - 3.72460215957041i
F2n = double(subs(F2, sol2.parameters(1), kvals))
F1n =
-12.7448796695526 + 0i -6.46169436237299 + 0i -0.178509055193401 + 0i 6.10467625198619 + 0i 12.3878615591658 + 0i -14.2687797481358 - 2.40307293360451i -7.98559444095626 - 2.40307293360451i -1.70240913377667 - 2.40307293360451i 4.58077617340291 - 2.40307293360451i 10.8639614805825 - 2.40307293360451i -14.2687797481358 + 2.40307293360451i -7.98559444095626 + 2.40307293360451i -1.70240913377667 + 2.40307293360451i 4.58077617340291 + 2.40307293360451i 10.8639614805825 + 2.40307293360451i -10.7747069529858 - 2.43542709294936i -4.49152164580621 - 2.43542709294936i 1.79166366137337 - 2.43542709294936i 8.07484896855296 - 2.43542709294936i 14.3580342757325 - 2.43542709294936i -10.7747069529858 + 2.43542709294936i -4.49152164580621 + 2.43542709294936i 1.79166366137337 + 2.43542709294936i 8.07484896855296 + 2.43542709294936i 14.3580342757325 + 2.43542709294936i
F2n = 
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by