フィルターのクリア

Backward Finite-Div​ided-Diffe​rence Coding

2 ビュー (過去 30 日間)
Adrian
Adrian 2023 年 6 月 12 日
回答済み: Nivedita 2023 年 11 月 7 日
g = input('Enter the function: e.g @(x) sin(x): ','s');
f = str2func(g);
h = input('Enter step size, H: ');
x = input('At which point you want to approximate the derivative: ');
Bd2 = (f(x)-f(x-h))/h;
fprintf('Backward FDD: %.4f',Bd2)
How do I make my coding to compute this function @(x) -0.3757*x+5
  2 件のコメント
Ashutosh
Ashutosh 2023 年 6 月 14 日
It works as expected for me, what is the error message you are getting?
Kim
Kim 2023 年 10 月 28 日
Error using str2func Argument must be a text scalar. Error in BDD (line 9) f = str2func(g);

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

回答 (1 件)

Nivedita
Nivedita 2023 年 11 月 7 日
Hello Adrian,
I understand that you want to compute the function @(x) -0.3757*x+5 with the provided code.
I executed the provided code and it worked as expected from the definition of the "Bd2" variable providing a result of 0.3757.
If you are facing any difficulty with the "str2func" function and obtaining the same error as mentioned by Kim, you can take care of the following pointers:
  1. Ensure that there are no extra characters or spaces in the function expression stored in "g".
  2. Verify that the function expression in "g" follows the correct syntax as you have mentioned in your question.
For more information on the "str2func" function, please refer to the following documentation link:
I hope it helped!
Regards,
Nivedita.

カテゴリ

Help Center および File ExchangeScope Variables and Generate Names についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by