Matlab:为什么在ode45中传递其他参数时,我还需要传递`(t,y)`? [英] Matlab: Why in passing additional arguments in ode45 i need to pass `(t,y)` as well?

查看:326
本文介绍了Matlab:为什么在ode45中传递其他参数时,我还需要传递`(t,y)`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> MATLAB:如何传递参数功能?

据说如果我想传递参数u,则需要使用匿名函数:

it is said that if i want to pass the parameter u, i need to use anonymous function:

u = 1.2;
[t y] = ode45(@(t, y) ypdiff(t, y, u), [to tf], yo);

最初,未传递参数u的情况下,颂歌行显示为:

Originally, without passing parameter u, the ode line reads:

[t y] = ode45(@ypdiff, [to tf], yo);,其中@ypdiff只是创建一个函数句柄.

[t y] = ode45(@ypdiff, [to tf], yo);, where @ypdiff just creates a function handle.

为什么只想传递u,我们还需要在创建匿名函数@(t, y) ypdiff(t, y, u)时包含ty,而不是像@ypdiff(u)这样的东西?

Why if we want to pass u only, we also need to include t and y in the creation of anonymous function @(t, y) ypdiff(t, y, u), but not something like @ypdiff(u)?

推荐答案

只需将@附加到函数的前面即可创建函数句柄 not 匿名函数.该函数句柄将所有输入参数隐式转发到该函数上.

Simply appending @ to the front of a function creates a function handle not an anonymous function. This function handle implicitly forwards all input arguments onto the function.

您需要的是匿名函数的函数句柄(因为它接受输入并执行操作或调用另一个函数).在这种情况下,它不会隐式传递输入,因此您需要显式接收输入参数,然后在匿名函数中使用(或不使用)它们.

What you need is a function handle to an anonymous function (since it accepts inputs and performs an action or calls another function). In this case, it does not implicitly pass inputs therefore you need to explicitly receive input arguments and then use them (or not) within the anonymous function.

@(t, y)ypdiff(t, y, u)

此规则的唯一例外是某些图形对象将接受单元格数组来代替回调函数,后者将函数句柄作为第一个元素,并将任何其他参数作为第二个元素,但是对于ode45.

The only exception to this rule is that some graphics objects will accept a cell array in place of a callback function which accept a function handle as the first element and any additional parameters as the second, but this is not the case for ode45.

{@ypdiff, u}

这篇关于Matlab:为什么在ode45中传递其他参数时,我还需要传递`(t,y)`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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