User defined function wont work, syntax issue?

조회 수: 11 (최근 30일)
Tyler
Tyler 2012년 11월 5일
function [Max_height] = Nheight(v0, la, g)
Max_height = -(v0^2*sin(la)^2)/(2*g);
not really sure what to do to fix it. Cant get it to work.
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 5일
what is the error message
Jan
Jan 2012년 11월 5일
@Tyler: Please provide the error message and the inputs, which cause the error. Otherwise an answer required guessing, and this is always a bad strategy.

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

답변 (5개)

Tyler
Tyler 2012년 11월 5일
it is

Walter Roberson
Walter Roberson 2012년 11월 5일
Your code would have a problem if any of your parameters are vectors, and could have a problem if they are matrices. You should change your * operations to .* and you should change ^ to .^ and should change / to ./

Tyler
Tyler 2012년 11월 5일
these are the errors im getting still with the "." also i dont understand why it says undefined function or variable for v0 when there is a defined v0 in the workspace
Undefined function or variable 'v0'.
Error in Max_Height (line 2) Max_height = -((v0).^2.*sin(la).^2)/(2.*g); Error in fminbnd (line 216) x= xf; fx = funfcn(x,varargin{:});
Error in main (line 19) h = fminbnd(@Max_Height, 0, 6500); fprintf('The max height reached was %d meters\n', h)
  댓글 수: 3
Tyler
Tyler 2012년 11월 5일
i changed it were it isnt, and the error nor the undefined v0 went away.
Walter Roberson
Walter Roberson 2012년 11월 5일
I am confused about whether the problem is solved or not? If it is not please show your current function and show your main routine where you are calling it.
You do realize that with the code you showed earlier, your "la" and "g" would be undefined? fminbnd() only passes in one parameter. See the fminbnd() documentation.

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


Jan
Jan 2012년 11월 5일
How do you call the function? Perhaps like this:
v0 = 123;
la = 234;
g = 567;
Max_height = Nheight
You have to call the function in this way:
Max_height = Nheight(v0, la, g)
The existence of v0 in the workspace is not enough for functions in opposite to scripts. You have to provide the inputs explicitly.

Tyler
Tyler 2012년 11월 6일
here is the User defined:
function [Max_h] = Max_Height(v0, la, g)
Max_h= -((v0.^2).*(sin(la)).^2)/(2.*g);
here is the line it is in:
h = fminbnd(@Max_Height,0,6500);
im getting these errors
Error using Max_Height (line 2) Not enough input arguments.
Error in fminbnd (line 216) x= xf; fx = funfcn(x,varargin{:});
Error in main (line 19) h = fminbnd(@Max_Height,0,6500); fprintf('The max height reached was %d meters\n', h);
  댓글 수: 6
Tyler
Tyler 2012년 11월 6일
ok. All things aside, when i run my userdefined functions (forget the fminbnd for now), i get not enough input arguments.
everything is in the same folder, variables are in the workspace. do i need to somehow put the variables in the UDF or what?
Walter Roberson
Walter Roberson 2012년 11월 6일
What is your current version of the code?
Having variables in the workspace is not good enough: you need to pass them in to the function if they appear in the function heading.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by