函数的导数在数值上已知的MATLAB微分 [英] MATLAB differentiation when the derivative of a function is numerically known

查看:142
本文介绍了函数的导数在数值上已知的MATLAB微分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MATLAB中区分以下函数wrt: T( e(x(t),t) p(t))

我的问题是我从数字上知道x的导数(我在一种odefun中). 我想使用diff使我的代码可推广用于高阶导数,但是x的导数现在是常数.我还希望所有这些都在一个匿名函数中,在其中可以进行微分,并相应地替换所需的x的时间和导数,这样我就不必为系统的每个状态编写多个函数.

我的代码如下:

syms q x star;
qd=symfun(90*pi/180+30*pi/180*cos(q),[q]);
p=symfun(79*pi/180*exp(-1.25*q)+pi/180,[q]);
T=log(-(1+star)/star);
e=symfun(x-qd,[x,q]);

例如,我想以

的形式编写函数

@(t,y)( d ^ 2 dt ^ 2 T( e(x(t),t) p(t))+ d dt T( e(x(t),t) p(t))+ T( e(x(t),t) p(t)))

解决方案

我不确定实现细节,但总的来说,这是您可以采用的一种方法.它涉及两个步骤.

    T(.)函数中的
  1. exp(t)替换x时,用这种方法进行微分时exp(t)始终停留在此处以便进行高阶导数,并且外部函数将相对于同时.在执行diff之后,您应该收到一个包含exp(t)的表达式(未经测试,希望如此).此时,exp(t)x的时间导数.现在,您只需要在t中评估此表达式.这样做时,您需要用x的派生替换exp(t).我不知道是否可以做到这一点,如果不能,那么可以使用y代替exp(t)并使用约束y=exp(t)来做到这一点,但是您需要自己弄清楚正确的实现.
  2. 在这里您需要在右侧的t处替换x的导数.如果您没有在特定点t处的值,请执行我在注释中建议的操作.预先在许多点上预先计算并在此步骤中进行插值.

这种方法依赖于将x(t)exp(t)交换,如果那不起作用,那么我将按照注释中的建议进行操作.通过已知函数近似x(t),并在代码中使用它代替x.

I want to differentiate the following function wrt in MATLAB: T(e(x(t),t)p(t))

My problem is that I know the derivatives of x numerically (I am inside a kind of odefun). I want to use diff to make my code generalizeable for high order derivatives,but the derivatives of x are now constant. I would also like all this to be in an anonymous function where I can make the differentiation and substitute accordingly the time and the derivative of x needed,so that I don't have to write multiple functions for every state of my system.

My code is as follows:

syms q x star;
qd=symfun(90*pi/180+30*pi/180*cos(q),[q]);
p=symfun(79*pi/180*exp(-1.25*q)+pi/180,[q]);
T=log(-(1+star)/star);
e=symfun(x-qd,[x,q]);

and I want to write for example a function in the form

@(t,y)(d^2dt^2 T(e(x(t),t)p(t))+ddt T(e(x(t),t)p(t))+T(e(x(t),t)p(t)))

解决方案

I am not sure of the implementation details but in general this is one approach that you could take. It involves two steps.

  1. in the T(.) function replace x with exp(t) this way when you do the differentiation exp(t) always stays there for the higher order derivatives to be taken and the outer functions will be differentiated with respect to x at the same time. After you do diff you should receive an expression that contains exp(t) (not tested so hopefully it is the case). At this point exp(t) is your time derivative of x. Now you only need to evaluate this expression in t. When doing so you need to replace exp(t) by the derivative of x. I do not know if this can be done, if not then perhaps using y instead of exp(t) with the constraint y=exp(t) would do it but you need to figure the correct implementation out yourself.
  2. Here you need to substitute the derivative of x at the right t. If you do not have the value at the particular point t then do what I suggested in the comment. Pre-calculate it beforehand in many points and interpolate in this step.

This approach relies on swapping x(t) with exp(t) if that does not work then I would do what I suggested in the comment. Approximate x(t) by a known function and use that instead of x in your code.

这篇关于函数的导数在数值上已知的MATLAB微分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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