简体   繁体   English

替换 MATLAB 中的符号导数

[英]Replacing symbolic derivatives in MATLAB

I have a symbolic function that looks like this我有一个看起来像这样的符号函数

syms x y(x) h
fn(x) = y + (h^2*(diff(y(x), x) + 2))/2 + (h^5*diff(y(x), x, x, x, x))/120 + (h^3*diff(y(x), x, x))/6 + (h^4*diff(y(x), x, x, x))/24 + h*(2*x + y(x) - 1)

I'd like to replace all instances of derivatives of y with its first derivative, ie我想用它的一阶导数替换 y 的所有导数实例,即

subs(fn, sym('diff(y(x), x)'), dy)

where dy is already defined as其中dy已定义为

dy(x) = 2*x + y(x) - 1

The result is the following:结果如下:

ans(x) =

y + (h^2*(2*x + y(x) + 1))/2 + (h^5*diff(y(x), x, x, x, x))/120 + (h^3*diff(y(x), x, x))/6 + (h^4*diff(y(x), x, x, x))/24 + h*(2*x + y(x) - 1)

It replaces the first derivative, but not the higher derivatives.它取代了一阶导数,但不是更高的导数。 What I want is for the h^5 term to have (h^5*diff(dy(x), x, x, x) . Is there any way to do that?我想要的是h^5项有(h^5*diff(dy(x), x, x, x) 。有什么办法可以做到吗?

My current method is pretty hackish and involves converting the sym to a string, replacing first derivatives with dy , then converting back to a sym and evaluating to reduce the order of each term of the series by one, but it has to be recursive because at each stage derivatives of dy are then replaced by something containing diff(y, ...) .我目前的方法非常笨拙,涉及将 sym 转换为字符串,用dy替换一阶导数,然后转换回 sym 并评估以将系列的每个项的顺序减一,但它必须是递归的,因为在然后将dy每个阶段导数替换为包含diff(y, ...) I was hoping there would be a cleaner way to deal with this.我希望有一种更清洁的方法来处理这个问题。

You need to keep in mind that Matlab treats things like diff(y,x) and diff(y,x,2) as distinct variables.您需要记住,Matlab 将diff(y,x)diff(y,x,2)视为不同的变量。 It doesn't know how to substitute diff(y,x) into diff(y,x,2) because such a general operation for an abstract function (one with out an explicit definition, eg, y(x) ) is ill-defined.它不知道如何将diff(y,x)替换为diff(y,x,2)因为抽象函数的这种一般操作(没有明确定义的函数,例如y(x) )是病态的-定义。

How about something like this that performs substitution from the opposite end, starting with the highest order derivatives:像这样的事情如何从另一端执行替换,从最高阶导数开始:

syms y(x) h
dy(x) = 2*x + y - 1
fn(x) = y + (h^2*(diff(y, x) + 2))/2 + (h^5*diff(y, x, 4))/120 + (h^3*diff(y, x, 2))/6 + (h^4*diff(y, x, 3))/24 + h*(2*x + y - 1)
fn2 = subs(fn, diff(y, x, 4), diff(dy, x, 3));
fn2 = subs(fn2, diff(y, x, 3), diff(dy, x, 2));
fn2 = subs(fn2, diff(y, x, 2), diff(dy, x));
fn2 = subs(fn2, diff(y, x), dy);

This returns这返回

fn2(x) =

y(x) + (h^2*(2*x + y(x) + 1))/2 + (h^3*(2*x + y(x) + 1))/6 + (h^4*(2*x + y(x) + 1))/24 + (h^5*(2*x + y(x) + 1))/120 + h*(2*x + y(x) - 1)

Or you can leave dy(x) as an abstract symbolic expression initially:或者,您可以将dy(x)最初保留为抽象的符号表达式:

syms y(x) dy(x) h
fn(x) = y + (h^2*(diff(y, x) + 2))/2 + (h^5*diff(y, x, 4))/120 + (h^3*diff(y, x, 2))/6 + (h^4*diff(y, x, 3))/24 + h*(2*x + y - 1)
fn2 = subs(fn, diff(y, x, 4), diff(dy, x, 3));
fn2 = subs(fn2, diff(y, x, 3), diff(dy, x, 2));
fn2 = subs(fn2, diff(y, x, 2), diff(dy, x));
fn2 = subs(fn2, diff(y, x), dy)

which returns返回

fn2(x) =

y(x) + (h^4*diff(dy(x), x, x))/24 + (h^2*(dy(x) + 2))/2 + (h^5*diff(dy(x), x, x, x))/120 + (h^3*diff(dy(x), x))/6 + h*(2*x + y(x) - 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM