Functions in Matlab, problem

I have a vector A = (A1, A2, A3) which is orthogonal to another unit vector, v = (v1,v2,v3), whose first coordinate is 0. I have come up with the following:
v2 = [0 A(2) -A(3)];
v3 = [0 -A(2) A(3)];
v2 = v2/norm(v2)
v3 = v3/norm(v3)
But if I want to write/code it as a function instead where A is the input value, how do I do that?
I understand that functions can be written as function[v2,v3] = Name(A3,A2) but how do I do it on above if the function is not given?
I have tried with below but all I get is errors, and furthurmore I do not get the coordinates of the vector:
function[v2,v3] = function1(A2,A3)
v2 = +/-A3/sqrt((A2)^2)+(A3)^2);
v3 = +/-A2/sqrt((A2)^2)+(A3)^2);

4 件のコメント

Adam
Adam 2019 年 11 月 20 日
function [v2, v3] = functionName( A )
v2 = [0 A(2) -A(3)];
v3 = [0 -A(2) A(3)];
v2 = v2/norm(v2)
v3 = v3/norm(v3)
seems fine. I can't work out what on earth you are trying to do in your example at the bottom. The code itself doesn't need to change at all to be inside a function and
+/-w3/
is not valid syntax.
Jenny Andersen
Jenny Andersen 2019 年 11 月 20 日
Hi Adam, thanks for your reply. If i run the function I am asked to enter a vector for A, but should I not be able to just write the follwing in the Command Window (and enter a random value for A):
[v2, v3] = functionName( 1 )
Adam
Adam 2019 年 11 月 20 日
You expect A to have 3 elements, so you have to pass in a 3-element array, even though you don't use the first one at all. Alternatively you could pass in two arguments as A2 and A3 and change the code accordingly. But clearly a scalar input won't work. How would you expect the equation to calculate V2 and V3?
Image Analyst
Image Analyst 2019 年 11 月 24 日
Can you supply some actual input numbers and the actual ine of code, and what you'd like as output numbers?

サインインしてコメントする。

回答 (0 件)

カテゴリ

タグ

質問済み:

2019 年 11 月 20 日

コメント済み:

2019 年 11 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by