Creating a general function handle

  • Thread starter Thread starter mikeph
  • Start date Start date
  • Tags Tags
    Function General
Click For Summary
SUMMARY

This discussion focuses on creating a general function handle in MATLAB that can dynamically handle an N-by-1 vector input. The user aims to define a function handle that computes the sum of a variable scalar x raised to the powers specified in the input vector. The solution provided is to use the syntax f = @(x) sum(x.^v), where v is the vector containing the powers. This approach effectively allows for the creation of a flexible function handle that can adapt to different vector sizes.

PREREQUISITES
  • Understanding of MATLAB function handles
  • Familiarity with vector operations in MATLAB
  • Knowledge of element-wise operations using the dot operator (.^)
  • Basic understanding of summation functions in MATLAB
NEXT STEPS
  • Explore advanced MATLAB function handle techniques
  • Learn about MATLAB anonymous functions and their applications
  • Investigate vectorization in MATLAB for performance optimization
  • Study the use of cell arrays for dynamic function inputs in MATLAB
USEFUL FOR

This discussion is beneficial for MATLAB programmers, data analysts, and anyone looking to enhance their skills in creating dynamic function handles for mathematical computations.

mikeph
Messages
1,229
Reaction score
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
v=[1,2,3]

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

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K