Integrate a function after having differenciated it

8 ビュー (過去 30 日間)
Adrian Anton Alvarez
Adrian Anton Alvarez 2020 年 12 月 15 日
コメント済み: Adrian Anton Alvarez 2020 年 12 月 16 日
I would like to differenciate a function (2 variables) and after that integrate it, but its giving me errors all the time. You can see here an extract of the code. Does anybody now how can I do this?
u = @(x,y) sqrt(1-x).*y;
duxx = @(x,y) diff(u(x,y),x,2);
k = (1/2)*integral2(duxx,0,1,0,1);

採用された回答

Walter Roberson
Walter Roberson 2020 年 12 月 16 日
編集済み: Walter Roberson 2020 年 12 月 16 日
integral2 is a numeric integration routine. It will pass the function handle two arrays of values, and must return something the same size.
With your function handle, u(x, y) will be invoked, passing in the arrays. Your u is vectorized so it will return a numeric array.
The numeric array will then be passed as the first parameter to diff(), and the numeric x will be passed as the second parameter, and 2 will be passed as the third.
https://www.mathworks.com/help/matlab/ref/diff.html shows that three parameters are possible for diff() but only when the second is a positive integer representing the difference number and the third is the dimension number. The array of x values is not a dimension number.
You have gotten confused between the numeric diff() function and the symbolic diff() function which is calculus. The calculus diff does not work on function handles, just symbolic expressions and symbolic functions.
sym x y
duxx = matlabFunction(diff(u(x,y), x, 2), 'vars', [x, y])
integral2(duxx, etc)
  1 件のコメント
Adrian Anton Alvarez
Adrian Anton Alvarez 2020 年 12 月 16 日
Thank you, that solved my problem.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by