Matlab function

  • #1
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

[email protected](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?
 

Answers and Replies

  • #2
166
0
Here you go.
Code:
f = @(x) x.*(0<=x & x<1) + (1-x).*(1<=x & x<=2);
 
  • #3
Thanks alot matonski. This is really :cool:
 
Last edited:

Related Threads on Matlab function

  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
2
Views
6K
  • Last Post
Replies
2
Views
5K
  • Last Post
Replies
0
Views
12K
  • Last Post
Replies
3
Views
3K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
9
Views
916
  • Last Post
Replies
3
Views
11K
  • Last Post
Replies
3
Views
2K
Top