【问题标题】:Multiplying two function handle and applying ode45 to the result将两个函数句柄相乘并将 ode45 应用于结果
【发布时间】:2014-10-16 11:12:13
【问题描述】:

我有以下离散形式的函数(这意味着它们是数组):

p1_1 of dim(200x1)
p1_2 of dim(200x1)
p1_3 of dim(200x1)
p2_1 of dim(200x1)
p2_2 of dim(200x1)
p2_3 of dim(200x1) 

函数p1_1, p1_2, p1_3 正在评估x1 = 0:(10/199):10 的点,函数p2_1, p2_2, p2_3 在点x2 = 0:(10/199):10

由于我有函数值和函数被评估的点,我可以执行以下操作:

fun1_of_p1 = @(xq1) interp1(x1,p1_1,xq1);
fun1_of_p2 = @(xq1) interp1(x1,p1_2,xq1);
fun1_of_p3 = @(xq1) interp1(x1,p1_3,xq1);

fun2_of_p1 = @(xq2) interp1(x2,p2_1,xq2);
fun2_of_p2 = @(xq2) interp1(x2,p2_2,xq2);
fun2_of_p3 = @(xq2) interp1(x2,p2_3,xq2);

然后我需要能够做到以下几点:

new_fun1 = @(xq1,xq2) fun1_of_p1.*fun2_of_p1;
new_fun2 = @(xq1,xq2) fun1_of_p1.*fun2_of_p2;
new_fun3 = @(xq1,xq2) fun1_of_p1.*fun2_of_p3;
new_fun4 = @(xq1,xq2) fun1_of_p2.*fun2_of_p1;
new_fun5 = @(xq1,xq2) fun1_of_p2.*fun2_of_p2;
new_fun6 = @(xq1,xq2) fun1_of_p2.*fun2_of_p3;
new_fun7 = @(xq1,xq2) fun1_of_p3.*fun2_of_p1;
new_fun8 = @(xq1,xq2) fun1_of_p3.*fun2_of_p2;
new_fun9 = @(xq1,xq2) fun1_of_p3.*fun2_of_p3;

终于

 last_fun = @(xq1,xq2) gamma1*new_fun1 + gamma2*new_fun2 +...
 gamma3*new_fun3 + gamma4*new_fun4 + gamma5*new_fun4 +...
 gamma6*new_fun6 + gamma7*new_fun7 + gamma8*new_fun8 +...
 gamma9*new_fun9;

gamma-values 只是常量(实数值常量)。在我定义了last_fun之后,我需要对其应用ode45,但我不知道该怎么做,我试过了:

([T,V] = ode45(@(xq1,xq2)last_fun(xq1,xq2),tspan,x0)

但它不起作用。其实我不知道我所做的一切是否正确,所以非常感谢一些反馈!

【问题讨论】:

    标签: matlab ode function-handle


    【解决方案1】:

    将函数的句柄视为标识该函数的标签(或地址、ID 等)。这些标签是数字好吧(计算机中的一切都是数字),但它们不代表函数可能采用的值。要获取这些值,您必须提供函数的 ID 函数的参数。

    现在,当您说函数乘法时,您的意思是返回函数的相乘的函数,而不是函数的标签。这导致了正确的定义:

    new_fun1 = @(xq1,xq2) fun1_of_p1(xq1).*fun2_of_p1(xq2);
    new_fun2 = @(xq1,xq2) fun1_of_p1(xq1).*fun2_of_p2(xq2);
    new_fun3 = @(xq1,xq2) fun1_of_p1(xq1).*fun2_of_p3(xq2);
    new_fun4 = @(xq1,xq2) fun1_of_p2(xq1).*fun2_of_p1(xq2);
    new_fun5 = @(xq1,xq2) fun1_of_p2(xq1).*fun2_of_p2(xq2);
    new_fun6 = @(xq1,xq2) fun1_of_p2(xq1).*fun2_of_p3(xq2);
    new_fun7 = @(xq1,xq2) fun1_of_p3(xq1).*fun2_of_p1(xq2);
    new_fun8 = @(xq1,xq2) fun1_of_p3(xq1).*fun2_of_p2(xq2);
    new_fun9 = @(xq1,xq2) fun1_of_p3(xq1).*fun2_of_p3(xq2);
    

    请以同样的方式更正last_fun 表达式:

    last_fun = @(xq1,xq2) ...
         gamma1*new_fun1(xq1,xq2) ...
       + gamma2*new_fun2(xq1,xq2) ...
       + gamma3*new_fun3(xq1,xq2) ...
       + gamma4*new_fun4(xq1,xq2) ...
       + gamma5*new_fun4(xq1,xq2) ...
       + gamma6*new_fun6(xq1,xq2) ...
       + gamma7*new_fun7(xq1,xq2) ...
       + gamma8*new_fun8(xq1,xq2) ...
       + gamma9*new_fun9(xq1,xq2);
    

    ode45 的正确调用是:

    [T,V] = ode45(last_fun,tspan,x0);
    

    前提是tspanx0是向量,tspan是升序排列的。

    【讨论】:

    • 感谢四位您的回答!我现在将更正功能!顺便说一句,您解释函数句柄是什么的方式令人惊叹!我现在可以申请ode45 吗?如何制定应用 ode45 的代码的正确方法是什么?
    • 正确的方式是[T,V] = ode45(last_fun,tspan,x0),前提是tspanx0具有相同的大小并且是向量。
    • 再次感谢!我会检查你的建议! :)
    • @SergioHaram 很高兴我能提供帮助。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2014-01-11
    • 2013-09-18
    • 1970-01-01
    相关资源
    最近更新 更多