Matlab help User defined function

In summary: But you are passing in a function of three variables (t, V, Theta). That's why it's complaining about the input argument "V" being undefined.To fix this, you need to pass in a function that only takes one variable. You can do this by creating an anonymous function that calls your Height function with the correct values for t, V, and Theta.In summary, the problem is with the way the function fminbnd() is being used. It expects a function with only one variable, but the user-defined function Height() has three variables. This can be fixed by creating an anonymous function that calls Height() with the correct values for t, V, and Theta.
  • #1
qiyan31
3
0
I tried to make a user-defined function.
function Ht=Height(t,V,Theta);
Ht=V*t*sin(Theta)-4.9*t.^2;

but then i kept on getting Input argument "V" is undefined...

can someone help me please
 
Physics news on Phys.org
  • #2
qiyan31 said:
I tried to make a user-defined function.
function Ht=Height(t,V,Theta);
Ht=V*t*sin(Theta)-4.9*t.^2;

but then i kept on getting Input argument "V" is undefined...

can someone help me please
What you show above seems reasonable for the function definition. Show us how you are calling this function - that might be what's causing your error.
 
  • #3
Mark44 said:
What you show above seems reasonable for the function definition. Show us how you are calling this function - that might be what's causing your error.

Here's the function.

Ht=fminbnd(@Height,0,5000,V,Theta);
 
  • #4
qiyan31 said:
Here's the function.

Ht=fminbnd(@Height,0,5000,V,Theta);
This doesn't make any sense to me. Use the function you wrote (the Height function) to calculate height, and then pass the value returned by that function as the first parameter in your fminbnd function.
 
  • #5
You get that error when you try to run the function without giving it values for the arguments that you are passing into the function.

If you go to matlab's command window type the function's name and then the values that you want to use. For example:

Height(3, 4, 60)
t = 3, V = 4, theta = 60
 
  • #6
The problem is not the function, but the way you are trying to use it.

The function fminbnd() expects to be passed a function of a single variable.
 

1. How do I create a user-defined function in Matlab?

To create a user-defined function in Matlab, you can use the "function" keyword followed by the function name and input arguments. The function code should be enclosed in a pair of parentheses and end with the keyword "end". For example:
function [output] = myFunction(input)
% function code goes here
end

2. What is the purpose of using a user-defined function in Matlab?

A user-defined function allows you to create a custom set of code that can be reused multiple times within a program. This can help to simplify complex tasks and make code more organized and modular. It also allows for easier debugging and maintenance of code.

3. How do I call a user-defined function in Matlab?

To call a user-defined function in Matlab, you simply use the function name followed by the input arguments within parentheses. For example:
output = myFunction(input)

4. Can a user-defined function have multiple output variables?

Yes, a user-defined function in Matlab can have multiple output variables. These can be specified within square brackets after the function name, separated by commas. For example:
function [output1, output2] = myFunction(input)
% function code goes here
end

5. How can I pass a variable number of input arguments to a user-defined function in Matlab?

You can use the "varargin" keyword within the input arguments of a user-defined function to allow for a variable number of inputs. The function code can then use the "nargin" keyword to determine the number of input arguments and handle them accordingly. For example:
function output = myFunction(varargin)
numInputs = nargin; % number of input arguments
% function code goes here
end

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
805
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
815
  • Engineering and Comp Sci Homework Help
Replies
2
Views
818
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
891
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
874
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
Back
Top