User defined function wont work, syntax issue?
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
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 ./
0 个评论
Tyler
2012-11-5
3 个评论
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.
0 个评论
Tyler
2012-11-6
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!