How to convert expression to function?

Hi, I am trying to convert my expression to a function, but when I remove the informative text I can not call/run the function. How can I fix this?
w=input('Please enter a vector B=[B1 B2 B3]: ');
c2 = [0 B(2) -B(3)];
c3 = [0 -B(2) B(3)];
c2 = B2/norm(B2)
c3 = B3/norm(B3)
disp(' ');
c2=1*B2;
c3=1*-B2;
disp('The following vectors fulfill the demands above:')
disp(' ');
disp(['c2=[' num2str(c2(1)) ',' num2str(c2(2)) ',' num2str(c2(3)) ']'])
disp(['c3=[' num2str(c3(1)) ',' num2str(c3(2)) ',' num2str(c3(3)) ']'])
disp(' ');
I have tried to add the function expression, as follows:
function(c2,c3) = Test(B)
w=input('Please enter a vector B=[B1 B2 B3]: ');
c2 = [0 B(2) -B(3)];
c3 = [0 -B(2) B(3)];
c2 = B2/norm(B2)
c3 = B3/norm(B3)
disp(' ');
c2=1*B2;
c3=1*-B2;
disp('The following vectors fulfill the demands above:')
disp(' ');
disp(['c2=[' num2str(c2(1)) ',' num2str(c2(2)) ',' num2str(c2(3)) ']'])
disp(['c3=[' num2str(c3(1)) ',' num2str(c3(2)) ',' num2str(c3(3)) ']'])
disp(' ');

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 24일
편집: KALYAN ACHARJYA 2019년 11월 24일

0 개 추천

function [c2,c3]=test()
w=input('Please enter a vector B=[B1 B2 B3]: ');
c2 = [0 B(2) -B(3)];
c3 = [0 -B(2) B(3)];
c2 = B2/norm(B2)
c3 = B3/norm(B3)
disp(' ');
c2=1*B2;
c3=1*-B2;
disp('The following vectors fulfill the demands above:')
disp(' ');
disp(['c2=[' num2str(c2(1)) ',' num2str(c2(2)) ',' num2str(c2(3)) ']'])
disp(['c3=[' num2str(c3(1)) ',' num2str(c3(2)) ',' num2str(c3(3)) ']'])
disp(' ');
end
here C2 and C3 as output arduments, if not required, you can avoid them too to just display
1w.png

댓글 수: 8

Jenny Andersen
Jenny Andersen 2019년 11월 24일
Thanks, but I am tryigng to get rid of the informative texts, such as ('Please enter a vector B=[B1 B2 B3]: ') and ('The following vectors fulfill the demands above:') and still be able to run the funktion.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 24일
편집: KALYAN ACHARJYA 2019년 11월 24일
Is the B1,B2, and B3 are scalars or vector?
Is B2 or B(2) you considered as same?
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 24일
편집: KALYAN ACHARJYA 2019년 11월 24일
Is this, You didnot answered my question, hence I have to guess
Is this
function [c2,c3]=test(B2,B3)
c2=[0 B2 -B3];
c3=[0 -B2 B3];
c2=B2/norm(B2);
c3=B3/norm(B3);
c2=1*B2;
c3=1*-B2;
disp('The following vectors fulfill the demands above:')
disp(' ');
fprintf('c2=%f',c2)
fprintf('c3=%f',c3)
end
Please note that you have replace the c2 and c3 vectors by scalar in later section of the code, also I have no clue what you trying to do.
13.png
Jenny Andersen
Jenny Andersen 2019년 11월 24일
B1 and B2 are vectors and B2 is the same as B(2).
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 24일
편집: KALYAN ACHARJYA 2019년 11월 24일
This is not same in MATLAB, B2 just a variable name, whereas B(2) accessing the second index element of variable B / array B
Jenny Andersen
Jenny Andersen 2019년 11월 24일
Ok
But if I try to write as follows, I can not call the function or run it since the informative text is removed. I want to be able to run the function and put in values for B=B1,B2,B3 without the informative text in the code.
function [c2,c3]=test(B)
c2 = [0 B(2) -B(3)];
c3 = [0 -B(2) B(3)];
c2 = B2/norm(B2)
c3 = B3/norm(B3)
disp(' ');
c2=1*B2;
c3=1*-B2;
disp(' ');
disp(['c2=[' num2str(c2(1)) ',' num2str(c2(2)) ',' num2str(c2(3)) ']'])
disp(['c3=[' num2str(c3(1)) ',' num2str(c3(2)) ',' num2str(c3(3)) ']'])
disp(' ');
end
Walter Roberson
Walter Roberson 2019년 11월 24일
... Delete the disp() calls?

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

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2019년 11월 24일

댓글:

2019년 11월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by