Inexplicable Symbolic Function problem

1 回表示 (過去 30 日間)
Emiliano Rosso
Emiliano Rosso 2022 年 12 月 8 日
コメント済み: Walter Roberson 2022 年 12 月 8 日
When I type :
syms xi
syms yi
syms lami
f(xi,yi,lami) = 1/2.*sum(abs((yi-xi).^2)) + lami.*sum(abs(diff(diff(xi))));
I obtain in the workspace the function " f 1x1 symfun "
but when I type in the command line " f ENTER" :
f(xi, yi, lami) =
abs(xi - yi)^2/2
What is this function I never typed and why does it not return an error?
Instead running the default example it works:
syms x y z
f(x,y,z) = 2*y*z*sin(x) + 3*x*sin(z)*cos(y);
>> f
f(x, y, z) =
2*y*z*sin(x) + 3*x*cos(y)*sin(z)
Where's my error ?
Thanks!

採用された回答

Walter Roberson
Walter Roberson 2022 年 12 月 8 日
編集済み: Walter Roberson 2022 年 12 月 8 日
xi is not a function. diff(xi) is going to search the expression xi looking for symbolic variables, find xi, and take the derivative with respect to that variable getting 1. diff(1) is 0. So the second term becomes 0.
xi and yi are scalar so the sum() only has the single term so the sum() wrapper can be removed.
The overall calculation becomes what you see.
When you create a symbolic function with that kind of assignment, then internal mechanism is to fully evaluate the right hand side, and then to use symfun() to create a symbolic function with the parameters of the left side.
Defining a symbolic function is not like defining an anonymous function with @. The names on the left do not just act as placeholders for passing values positional. Doing the diff(xi) on the right side immediately evalutes diff(xi) that takes the derivative of scalar xi. It does not just record the sequence of steps and apply the diff to whatever gets passed in later
  2 件のコメント
Emiliano Rosso
Emiliano Rosso 2022 年 12 月 8 日
Thanks!
You say "Doing the diff(xi) on the right side immediately evalutes diff(xi) that takes the derivative of scalar xi. It does not just record the sequence of steps and apply the diff to whatever gets passed in later".
I only partly understand the reasons but how can I just record the sequence of steps and apply the diff to whatever gets passed in later?
Walter Roberson
Walter Roberson 2022 年 12 月 8 日
use @() anonymous function definition.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by