求和函数在matlab中处理 [英] summing functions handles in matlab

查看:135
本文介绍了求和函数在matlab中处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi

我想总结两个函数句柄,但它不起作用。
例如:

blockquote


y1 = @(x)(x * x); >
y2 = @(x)(x * x + 3 * x);

y3 = y1 + y2


我收到的错误是???未定义函数或方法'加',用于'function_handle'类型的输入参数。



这只是一个小例子,实际上我实际上需要反复累加大约500个相互依赖的函数。



编辑

Clement J.的解决方案确实有效,但我无法将其推广到一个循环中并遇到问题。我有函数 s = @(x,y,z)((1-exp(-x * y)-z)* exp(-x * y)); 我有一个矢量v包含536个数据点,另一个矢量w也包含536个数据点。我的目标是总结s(v(i),y,w(i)),因为i = 1 ... 536因此在变量y中得到一个函数,它是536个函数的和。我尝试这样做的语法是:

  sum = @(y)(s(v(1),y ,Z2(1))); 
for i = 2:536
sum = @(y)(sum + s(v(i),y,z2(i)))
end


解决方案

Fyodor Soikin提出的解决方案可行。

 >> (x)(y1(x)+ y2(x))
y3 =
@(x)

如果你想在多个函数上使用它,你可以使用中间变量:

 >> f1 = y1; 
>> f2 = y2;
>> y3 = @(x)(f1(x)+ f2(x))


我不确定要理解这个问题。你可以在函数之外定义你的向量v和w:

  v = [5 4]; %你的536数据
w = [4 5];
y = 8;
s = @(y)((1-exp(-v * y)-w)。* exp(-v * y))
s_sum = sum(s(y))

请注意乘法中的点,以元素为单位。


Hi
I am trying to sum two function handles, but it doesn't work. for example:

y1=@(x)(x*x);
y2=@(x)(x*x+3*x);
y3=y1+y2

The error I receive is "??? Undefined function or method 'plus' for input arguments of type 'function_handle'."

This is just a small example, in reality I actually need to iteratively sum about 500 functions that are dependent on each other.

EDIT
The solution by Clement J. indeed works but I couldn't manage to generalize this into a loop and ran into a problem. I have the function s=@(x,y,z)((1-exp(-x*y)-z)*exp(-x*y)); And I have a vector v that contains 536 data points and another vector w that also contains 536 data points. My goal is to sum up s(v(i),y,w(i)) for i=1...536 Thus getting one function in the variable y which is the sum of 536 functions. The syntax I tried in order to do this is:

sum=@(y)(s(v(1),y,z2(1))); 
for i=2:536 
  sum=@(y)(sum+s(v(i),y,z2(i))) 
end

解决方案

The solution proposed by Fyodor Soikin works.

>> y3=@(x)(y1(x) + y2(x))
y3 =
@(x) (y1 (x) + y2 (x))

If you want to do it on multiple functions you can use intermediate variables :

>> f1 = y1;
>> f2 = y2;
>> y3=@(x)(f1(x) + f2(x))

EDIT after the comment: I'm not sure to understand the problem. Can you define your vectors v and w like that outside the function :

v = [5 4]; % your 536 data
w = [4 5];
y = 8;
s=@(y)((1-exp(-v*y)-w).*exp(-v*y))
s_sum = sum(s(y))

Note the dot in the multiplication to do it element-wise.

这篇关于求和函数在matlab中处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆