User defined function wont work, syntax issue?
古いコメントを表示
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 件のコメント
Tyler
2012 年 11 月 5 日
Azzi Abdelmalek
2012 年 11 月 5 日
what is the error message
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 件)
Walter Roberson
2012 年 11 月 5 日
0 投票
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
2012 年 11 月 5 日
0 投票
3 件のコメント
Walter Roberson
2012 年 11 月 5 日
Your routine is named Nheight but you are invoking Max_Height.
Note: do not name the routine the same thing as a variable in the routine.
Tyler
2012 年 11 月 5 日
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
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
2012 年 11 月 6 日
0 投票
6 件のコメント
Walter Roberson
2012 年 11 月 6 日
"Not enough input arguments" does not mean that v0 is undefined. In your case it indicates that la and g are undefined. fminbnd() only passes in one parameter. See the fminbnd() documentation.
Arguments
fun is the function to be minimized. fun accepts a scalar x and returns a scalar f, the objective function evaluated at x.
and
Parameterizing Functions in the MATLAB Mathematics documentation, explains how to pass additional parameters to your objective function fun.
Tyler
2012 年 11 月 6 日
Walter Roberson
2012 年 11 月 6 日
Read that documentation about parameterizing functions!
What value are you expecting la and g to get? You do not define them anywhere obvious, and fminbnd() is for minimizing along a SINGLE variable. If you are expecting la and g to -also- be varied between the bound 0 to 6500 then you have the logical problem that you divide by g so you would get a discontinuity at your bounds but the minimizers require continuous functions.
Tyler
2012 年 11 月 6 日
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.
カテゴリ
ヘルプ センター および File Exchange で MATLAB Coder についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!