Solving for all missing terms at once with "solve" doesn't work

1 vue (au cours des 30 derniers jours)
Valeriy
Valeriy le 31 Mai 2023
Modifié(e) : Valeriy le 1 Juin 2023
Hello,
I am trying to write a function to automate solving the following problem:
  • Given 3D unit vectors N, O, and A with at least 3 knowns (the rest could be symbols)
  • Given N is orthogonal to both O and A
  • Find the missing vector components
The equations that relate N, O, and A to each other are:
  • cross(O, A) == N
  • norm(N) == 1
  • norm(O) == 1
  • norm(A) == 1
Simple example done manually:
syms nx ny nz oz
A = [-0.9100; -0.4146; 0];
O = [0.3362; -0.7379; oz];
N = [nx; ny; nz]
% Solve for z component in the O vector
solve(norm(O) == 1, oz)
% oz: (5^(1/2)*6849463^(1/2))/10000
% Solve for all three components in N vector
solve(cross(O, A) == N, nx, ny, nz)
% nx: (2073*oz)/5000
% ny: -(91*oz)/100
% nz: -10135969/12500000
OK, so clearly that works. Next I would like to write a function to do this:
function S = noa(N, O, A)
S = solve([
cross(O, A) == N,
norm(N) == 1,
norm(O) == 1,
norm(A) == 1
]);
end
When I execute it using the values for N, O and I in example above, I get no solutions
noa(N, O, A)
% nx: [0x1 sym]
% ny: [0x1 sym]
% nz: [0x1 sym]
% oz: [0x1 sym]
I would also like to generate C code:
ccode(noa(N, O, A))
...but it's complaining about an incorrect number of inputs/outputs even though I provided the correct number
Incorrect number or types of inputs or outputs for function 'ccode'.
Does anyone know what might be wrong here?
Thank you!

Réponse acceptée

Nathan Hardenberg
Nathan Hardenberg le 31 Mai 2023
The reason, your solve does not work in the function is most likely that there are more equations than variables. This is no problem under normal conditions, but your equations are not correct. The equations are only a good numerical approximation:
format long
norm([-0.9100; -0.4146; 0])
ans =
0.999996579994152
That's why they can't be solved.
Just write you function like your example. The solution is then most likely correct enough.
function S = noa(N, O, A)
S = solve([cross(O, A) == N, norm(O) == 1])
end
Regarding you C-Code generation:
I think the output type is the incorrect one! S ia a struct. Code generation most likely does not support this data type (but I'm no expert on this). You can try to get all solutions and return them as a matrix. This could work.
  1 commentaire
Valeriy
Valeriy le 1 Juin 2023
Modifié(e) : Valeriy le 1 Juin 2023
Had a chance to review your response in detail - thanks for taking the time!
I found that several other issues I had using Matlab were rooted in being loosy-goosey with numeric precision. When I equate terms to other terms that were also calculated by Matlab all the equations have solutions.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by