Solving for all missing terms at once with "solve" doesn't work
1 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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])
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Assumptions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!