필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Valeriy
Valeriy 2023년 5월 31일
편집: Valeriy 2023년 6월 1일
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!

채택된 답변

Nathan Hardenberg
Nathan Hardenberg 2023년 5월 31일
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
Valeriy
Valeriy 2023년 6월 1일
편집: Valeriy 2023년 6월 1일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by