# Documentation

### This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

라이선스가 부여된 사용자만 번역 문서를 볼 수 있습니다. 번역 문서를 보려면 로그인하십시오.

## Anonymous Functions

This example shows how to define functions at the command line with anonymous functions.

### Integrating a Function

Consider the function 10*x.

 

If we want to allow any multiplier of x, not just 10, we might create a variable g (where g is initially set to 10), and create a new function

 

Let's do this in MATLAB® by creating a function handle h.

g = 10; h = @(x) g*x;

You can integrate the function by passing its handle to the INTEGRAL function.

integral(h,1,10)
ans = 495.0000 

Consider another function:

 

Create a function handle to this function where alpha = 0.9.

alpha = 0.9; f = @(x) sin(alpha*x);

Plot the function and shade the area under it.

x = 0:pi/100:pi; area(x,f(x)); % You can evaluate f without feval title('f(x) = sin(\alpha x), \alpha =.9');

We can use the INTEGRAL function to calculate the area under the function between a range of values.

integral(f,0,pi)
ans = 2.1678 

### Minimizing a Function

Consider the function:

 

where a = 1, b = -2, and c = 1

Create a function handle for it.

a = 1; b = -2; c = 1; f = @(x)(a*x.^2+b*x+c);

ezplot(f); % Plot the function title('f(x)=ax^2+bx+c, a=1,b=-2,c=1'); hold on; % Find and plot the minimum minimum = fminbnd(f,-2,2); % We can pass the function handle directly % to the minimization routine plot(minimum,f(minimum),'d'); % We can evaluate the function without % using feval grid; hold off;

### 2D Functions

We can create handles to functions of many variables

a = pi; b = 15; f = @(x,y) (a*x+b*y); ezsurf(f); title('f(x,y) = ax+by, a = \pi, b = 15');

### Function Composition

We can also create handles to functions of functions

f = @(x) x.^2; g = @(x) 3*x; h = @(x) g(f(x)); h(3)
ans = 27 

Watch now
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "http://kr.mathworks.com/help/matlab/examples/anonymous-functions.html?s_tid=gn_loc_drop&requestedDomain=kr.mathworks.com&nocookie=true", "fetch_time": 1474953683000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2016-40/segments/1474738660966.51/warc/CC-MAIN-20160924173740-00124-ip-10-143-35-109.ec2.internal.warc.gz", "warc_record_offset": 149340901, "warc_record_length": 12410, "token_count": 585, "char_count": 1949, "score": 4.03125, "int_score": 4, "crawl": "CC-MAIN-2016-40", "snapshot_type": "latest", "language": "en", "language_score": 0.6387170553207397, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00040-of-00064.parquet"}