Creating a general function handle

In summary, To create a MATLAB function handle that represents the sum of x raised to the power of each number in a given N-by-1 vector, use the syntax f = @(x) sum(x.^v), where v is the input vector. This allows for a general function handle, even when the input vector is not explicitly known, as N is variable.
  • #1
mikeph
1,235
18
I'm writing a MATLAB function m-file which has input argument of some N-by-1 vector.

Say this vector is [1,2,3].

I want to create a function handle representing a sum of x taken to the power of each number that appears in the vector, so in this case it is:

f = @(x) [x^1 + x^2 + x^3]; (x scalar)

The point is, I don't know how to define f when I don't explicitly know what the input vector is. Can anyone help me generate a general function handle? The trick is because N is variable.
 
Physics news on Phys.org
  • #2
v=[1,2,3]

f = @(x) sum(x.^v)
 
  • #3
I like it! Thanks very much
 

1. What is a general function handle?

A general function handle is a type of variable in programming that stores a reference to a function. This allows the function to be called or executed at a later time, using the handle as a shortcut instead of typing out the entire function name and parameters.

2. How do you create a general function handle?

To create a general function handle, you can use the "@" symbol followed by the name of the function. For example, if you have a function named "calculateArea", the general function handle would be "@calculateArea". You can also use anonymous functions, where you define the function directly within the handle itself.

3. Can a general function handle store any type of function?

Yes, a general function handle can store any type of function, including built-in functions, user-defined functions, and anonymous functions. However, the function must be defined before it can be used with a handle.

4. How do you use a general function handle to call a function?

To use a general function handle, you can simply use the handle name followed by parentheses and any necessary arguments. For example, if you have a function handle named "myFunction", you can call it by typing "myFunction(arg1,arg2)".

5. What are the advantages of using general function handles?

General function handles offer several advantages, including improved code readability, increased flexibility, and the ability to pass functions as arguments to other functions. They also allow for more efficient code reuse, as the same function can be called multiple times using different handles.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
17
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
928
Back
Top