【问题标题】:Linear transformation function handle线性变换函数句柄
【发布时间】:2015-06-01 14:22:15
【问题描述】:

假设我将单元格数组 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,.. .]

谢谢!

【问题讨论】:

  • 也许这个网站可能更适合您的问题?:math.stackexchange.com
  • @Kmeixner 我认为这更像是一个编程问题而不是数学问题
  • @user307380 你最好使用符号函数来完成这项任务

标签: matlab anonymous-function cell-array function-handle


【解决方案1】:

假设你有符号工具箱,你可以这样做:

  1. 将匿名函数元胞数组转换为字符串元胞数组
  2. 使用subs 更改变量。这会产生 symbolic objects 作为输出。
  3. 使用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

【讨论】:

    【解决方案2】:

    它也可以在基本的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 2018-10-30
      相关资源
      最近更新 更多