Converting anonymous function to a matrix
33 次查看(过去 30 天)
显示 更早的评论
I have a function of the form:
,
which I want to evaluate. I could do this old school with a double loop but that would take time, so I thought I would try and use an anonymous function. So I did the following:
f_1=@(x,y) (erf(x+y)-erf(x-y));
f_2=@(x,y) (exp(x*y+2*x));
f=@(x,y) (f_1*f_2);
Now when I do individual values, like f_1(1,1), it works as it should. However when I compute say f(1,1) I get the following error:
Undefined operator '*' for input arguments of type 'function_handle'.
Error in @(x,y)(f_1*f_2)
When I do the function in one fell swoop:
f=@(x,y) (exp(x*y+2*x)*(erf(x+y)-erf(x-y)));
It works unsurpringly.
Now, I also want to be able to do say
x=linspace(-2,2,10);y==linspace(-2,2,10);
and get a nice matrix out of it, but when I then do:
a=f(x,y);
I don't get the nice matrix I wanted. So there are two questions I want to ask:
- How can I build up anonymous functions from other anonymous functions
- How can I get a matrix from inputting two vectors?
0 个评论
回答(1 个)
Stephen23
2019-12-14
编辑:Stephen23
2019-12-14
"How can I build up anonymous functions from other anonymous functions"
Just call the functions, exactly like you would any other type of function:
f_1 = @(x,y) erf(x+y)-erf(x-y);
f_2 = @(x,y) exp(x*y+2*x);
f = @(x,y) f_1(x,y)*f_2(x,y); % you need to call them!
"How can I get a matrix from inputting two vectors?"
That depends on which version of MATLAB you are using and what operations your code contains.
Method one: vectorize: If you are using a version >=R2016b then make sure that you vectorize the operations, e.g. using element-wise array operations instead of matrix operations:
f_2 = @(x,y) exp(x.*y+2*x);
% ^^ array operation!
and then call the function with one row vector and one column vector. For earlier versions you will instead need to use bsxfun for any operation that involves different sized arrays.
Method two: arrayfun: Otherwise you can generate the required matrices using ndgrid and use a loop or arrayfun:
>> f_1=@(x,y) (erf(x+y)-erf(x-y));
>> f_2=@(x,y) (exp(x*y+2*x));
>> f =@(x,y) (f_1(x,y)*f_2(x,y));
>> x = linspace(-2,2,10);
>> y = linspace(-2,2,10);
>> [xM,yM] = ndgrid(x,y);
>> zM = arrayfun(f,xM,yM)
zM =
-1 -0.21775 -0.035275 -0.0041123 -0.00029302 0.00012046 0.00028574 0.00041426 0.00043219 0.00033546
-1.4703 -0.50089 -0.13285 -0.026021 -0.0029847 0.001495 0.0032701 0.0041888 0.0039625 0.0029184
-1.7913 -0.89723 -0.37183 -0.11768 -0.020722 0.012646 0.026748 0.031478 0.028291 0.021036
-1.9405 -1.3307 -0.80635 -0.38671 -0.098104 0.072947 0.15898 0.18328 0.16723 0.13483
-1.9864 -1.7473 -1.4215 -0.9381 -0.31685 0.28705 0.69754 0.86751 0.87523 0.81663
-1.9864 -2.1289 -2.1102 -1.6967 -0.69822 0.7707 2.2819 3.4577 4.2503 4.8318
-1.9405 -2.4068 -2.6378 -2.2881 -1.0498 1.4119 5.5656 11.605 19.151 27.927
-1.7913 -2.409 -2.6804 -2.2777 -1.0768 1.7645 10.021 31.662 76.402 152.53
-1.4703 -1.9964 -2.1104 -1.6475 -0.75323 1.5038 13.11 66.931 252.36 740.79
-1 -1.2883 -1.2349 -0.85177 -0.35909 0.87347 12.259 105.15 649.09 2981
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!