Creating Anonymous Functions for Complex Functions

In summary, the conversation discusses the use of anonymous functions in MATLAB to avoid creating extra m-files for simple functions. An example is given for integrating a function using a simple MATLAB script. The question is posed on how to create an anonymous function for a specific function, and the solution is provided by using logical indexing in the function definition.
  • #1
matematikawan
338
0
I normally create anonymous function in order to avoid creating extra m-file if the function is simple enough. For example if I want to integrate the function f(x)=x2+x, I just write a simple MATLAB script like

myfun=@(x) x.^2 + x;
quad(myfun,0,1)


But how do we create an anonymous function for the following function
[tex]
f(x)=\left\{\begin{array}{ccc}x,&\mbox{ } 0 \leq x <1\\
1-x, & \mbox{ } 1 \leq x <2\\
0 , & \mbox{ elsewhere } \end{array}\right.
[/tex]

The function look simple to me and I know how to write a function m-file for it. But I want to avoid it. Is it possible to create an anonymous function for it?
 
Physics news on Phys.org
  • #2
Here you go.
Code:
f = @(x) x.*(0<=x & x<1) + (1-x).*(1<=x & x<=2);
 
  • #3
Thanks a lot matonski. This is really :cool:
 
Last edited:

1. What is an anonymous function?

An anonymous function, also known as a lambda function, is a function without a name. It is a function that is defined without being bound to an identifier, making it a function value that can be passed around and used as a variable.

2. Why would I need to use anonymous functions in complex functions?

Anonymous functions are useful in complex functions because they allow you to create functions on the fly, without having to define a separate function and give it a name. This can make the code more concise and easier to read, as well as allowing for more flexibility in passing functions as arguments to other functions.

3. How do I create an anonymous function in JavaScript?

To create an anonymous function in JavaScript, you use the function keyword followed by the parameters in parentheses and a set of curly braces containing the function body. For example: var myFunction = function(a, b) { return a + b; };

4. What are some common use cases for anonymous functions?

Anonymous functions are commonly used in event handlers, callbacks, and higher-order functions. They are also useful for creating closures, as they can access variables from the parent function's scope. Additionally, they can be used to create short and concise functions for tasks that do not need to be repeated elsewhere.

5. Are there any downsides to using anonymous functions?

One potential downside to using anonymous functions is that they can make the code harder to debug, as they do not have a name for reference. They also cannot be reused in other parts of the code, as they are not bound to an identifier. However, these downsides can be mitigated by using well-written and well-documented code, and carefully considering when and where to use anonymous functions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
997
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
2
Views
682
Back
Top