假设我将单元格数组 P 中的 Legendre 多项式作为函数句柄。现在我使用线性变换 x = 2/3*t-1。现在我想得到一个具有转换函数句柄的元胞数组 Q。所以 P = [1, @(x) x, 1/2*(3*x^2-1),...] 到 Q = [1,@(t) 2/3*t-1,.. .]
谢谢!
假设我将单元格数组 P 中的 Legendre 多项式作为函数句柄。现在我使用线性变换 x = 2/3*t-1。现在我想得到一个具有转换函数句柄的元胞数组 Q。所以 P = [1, @(x) x, 1/2*(3*x^2-1),...] 到 Q = [1,@(t) 2/3*t-1,.. .]
谢谢!
假设你有符号工具箱,你可以这样做:
subs
。这会产生符号对象作为输出。matlabFunction
:代码:
P = {@(x) 1, @(x) x, @(x) 1/2*(3*x^2-1)}; %// data
f = cellfun(@func2str, P, 'uniformoutput', 0); %// step 1
Q = arrayfun(@(k) matlabFunction(subs(f{k}(5:end), 'x', '2/3*t-1')), 1:numel(P),...
'uniformoutput', 0); %// steps 2 and 3.
%// Note that the "(5:end)" part is used for removing the initial "@(x)"
%// from the string obtained from the function
此示例中的结果:
Q{1} =
@()1.0
Q{2} =
@(t)t.*(2.0./3.0)-1.0
Q{3} =
@(t)(t.*(2.0./3.0)-1.0).^2.*(3.0./2.0)-1.0./2.0
它也可以在基本的 MATLAB 中完成:您只需将多项式的匿名函数与转换函数组合起来。在编写解决方案之前,我想指出您的帖子不一致:您谈论函数句柄的单元数组,但您使用矩阵表示法进行定义。
编码:
%// The original polynomials
P = {@(x) 1, @(x) x, @(x) 1/2*(3*x^2-1)};
%// The transformation function
x = @(t)2/3*t-1;
%// The composition
Q = cellfun(@(f) @(t)f(x(t)), P, 'UniformOutput', false );
结果将是一个单元格数组,这些函数将执行以下操作:
x == 1 --> t == 3
P{2}(1) --> 1
Q{2}(3) --> 1