How can I define square of a handle function inside an integral?

17 ビュー (過去 30 日間)
Ashkan Rigi
Ashkan Rigi 2021 年 11 月 1 日
コメント済み: Steven Lord 2021 年 11 月 1 日
Hello every body. This is my code and I get the error " Undefined function 'sqrt' for input arguments of type 'function_handle'."
a=1;b=2;c=3;s=10;tt=12;
kkk=@ (t) (sqrt((3.*a(1).*(t-tt(1)).^2+2.*b(1).*(t-tt(1))+c(1)).^2)).^3;
kk=@(t) (18*a(1).^2*(t-tt(1)).^2+2.*b(1).*6.*a(1).*6.*a(1).*b(1).*(t-tt(1)).^2 ...
+2.*b(1).^2.*(t-tt(1))+6.*a(1).*c(1).*(t-tt(1))).^2;
kk=@(t)sqrt(kk);
k=kk/kkk;
bs=integral(k^2,0,s(1))

採用された回答

John D'Errico
John D'Errico 2021 年 11 月 1 日
編集済み: John D'Errico 2021 年 11 月 1 日
kk is a function handle, as is kkk. (Really creative names there. Note that using better, more descriptive names will greatly improve your code in the future, when you need to debug that code. As well, don't reuse variables like that, where you create kk as a functino handle, and then immmediately try to create a new function handle with the same name. That will only bring you down into the depths of programming hell when you try to debud code like that.)
Anyway, you CANNOT do operations like this:
kk=@(t) sqrt(kk);
or this:
k=kk/kkk;
or, this:
bs=integral(k^2,0,s(1))
Instead, you need to create a new function handle derived from it. For example:
kkfun = @(t) sqrt(kk(t));
kfun = @(t) kkfun(t)./kkk(t);
bs=integral(@(t) kfun(t).^2,0,s(1))
The point is, you do not want to square the function handle itself, but to square what it does to the operand (t). You do not want to divide two function handles, one by the other, but to divide the result of those function handles.
  1 件のコメント
Steven Lord
Steven Lord 2021 年 11 月 1 日
To summarize John's last paragraph, you can't do arithmetic on function handles. You can do arithmetic on the values returned by evaluating function handles.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall C++ from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by