为向量中的每个元素创建函数句柄 (Matlab)
Creating a function handle for each element in a vector (Matlab)
我有以下问题。我正在尝试创建一个函数句柄,它是一个向量。特别是,我有这样的东西
EQ0 = @(W) m1.^2*(exp(W))-m2.^2
其中m1和m2是相同维度的向量。因此,对于每个 m1(i) 和 m2(i),我都希望有一个句柄 W(i)。我需要它以便在下一步中使用 fsolve 在看起来像这样的东西中找到那些 W(i)
n=size(m1)
x0 = zeros(n);
Wbar = fsolve(EQ0,x0)
我尝试使用 arrayfun,但收到以下错误
EQ0 = arrayfun( @(W) m1.^2*(exp(W))-m2.^2, m1=m1e, m2=m2e)
Error: The expression to the left of the equals sign is not a valid target for an assignment.
另一个尝试使用 arrayfun 的结果是这样的(这里我只是直接使用 m1 和 m2 向量,而不是像之前的情况那样作为输入)
EQ0 = arrayfun( @(W) m1.^2*(exp(W))-m2.^2,:)
Undefined variable arrayfun.
我显然遗漏了什么。我查看了 arrayfun 上的一些提要,但看起来我的问题有些不同。
如有任何建议,我们将不胜感激。
So if I understood you right you want to have for each m1(i) or m2(i) a seperate function handle EQ0(i) which can operate with an vector W in the following way EQ0(i) = @(W) m1(i)^2*(exp(W))-m2(i)^2.这个对吗? If so you can create a cell-array of the same dimension as m1 with a function handle in each dimension:
EQ0 = cell(size(m1));
for ii = 1:numel(m1)
EQ0(ii) = {@(W) m1(ii)^2*exp(W)-m2(ii)^2};
end
编辑:
Another option could be:
EQ0 = @(W)arrayfun(@(Wel,m1el,m2el)m1el^2*exp(Wel)-m2el^2,W,m1,m2);
fsolve(EQ0, W_Values)
Here m1, m2 should be defined beforehand. Otherwise you have to add them as arguments of the first anonymous function.
So by calling arrayfun(@(Wel, m1el, m2el)..., W, m1, m2)
you do your elementwise calculations for the entries in W, m1, m2
defined by the anonymous function handle you have passed in the first argument of arrayfun. But since you want to define your W
differently each time, you make an anonymous function of that arrayfun command, which takes W
as an argument.
我有以下问题。我正在尝试创建一个函数句柄,它是一个向量。特别是,我有这样的东西
EQ0 = @(W) m1.^2*(exp(W))-m2.^2
其中m1和m2是相同维度的向量。因此,对于每个 m1(i) 和 m2(i),我都希望有一个句柄 W(i)。我需要它以便在下一步中使用 fsolve 在看起来像这样的东西中找到那些 W(i)
n=size(m1)
x0 = zeros(n);
Wbar = fsolve(EQ0,x0)
我尝试使用 arrayfun,但收到以下错误
EQ0 = arrayfun( @(W) m1.^2*(exp(W))-m2.^2, m1=m1e, m2=m2e)
Error: The expression to the left of the equals sign is not a valid target for an assignment.
另一个尝试使用 arrayfun 的结果是这样的(这里我只是直接使用 m1 和 m2 向量,而不是像之前的情况那样作为输入)
EQ0 = arrayfun( @(W) m1.^2*(exp(W))-m2.^2,:)
Undefined variable arrayfun.
我显然遗漏了什么。我查看了 arrayfun 上的一些提要,但看起来我的问题有些不同。
如有任何建议,我们将不胜感激。
So if I understood you right you want to have for each m1(i) or m2(i) a seperate function handle EQ0(i) which can operate with an vector W in the following way EQ0(i) = @(W) m1(i)^2*(exp(W))-m2(i)^2.这个对吗? If so you can create a cell-array of the same dimension as m1 with a function handle in each dimension:
EQ0 = cell(size(m1));
for ii = 1:numel(m1)
EQ0(ii) = {@(W) m1(ii)^2*exp(W)-m2(ii)^2};
end
编辑: Another option could be:
EQ0 = @(W)arrayfun(@(Wel,m1el,m2el)m1el^2*exp(Wel)-m2el^2,W,m1,m2);
fsolve(EQ0, W_Values)
Here m1, m2 should be defined beforehand. Otherwise you have to add them as arguments of the first anonymous function.
So by calling arrayfun(@(Wel, m1el, m2el)..., W, m1, m2)
you do your elementwise calculations for the entries in W, m1, m2
defined by the anonymous function handle you have passed in the first argument of arrayfun. But since you want to define your W
differently each time, you make an anonymous function of that arrayfun command, which takes W
as an argument.