how can i change x to (x+h)?

2 ビュー (過去 30 日間)
Bishwam Pattnaik
Bishwam Pattnaik 2019 年 3 月 12 日
コメント済み: Steven Lord 2019 年 3 月 12 日
This is my function, f(x)=x^2 - 4 and f(x+h)=(x+h)^2 - 4
f=@(x) x.^2- 4;
f=@(x+h) x.^2- 4 (this gives me error)
How can i write f(x+h) ?

採用された回答

Star Strider
Star Strider 2019 年 3 月 12 日
I am not certain what problem you are having. You have to define all the values you pass to your function as arguments before calling your function.
Example —
f = @(x) x.^2- 4;
h = 1E-8;
dfdx = @(f,x,h) (f(x+h) - f(x))./h;
x = 4;
fval = f(x)
derv = dfdx(f,x,h)

その他の回答 (1 件)

Adam
Adam 2019 年 3 月 12 日
編集済み: Adam 2019 年 3 月 12 日
It's the same function definition, you just pass in
x + h
instead of
x
assuming you have x and h defined at the point you call the function.
Or you could define
f = @(x,h) ( x + h )^2 - 4
if you prefer.
  1 件のコメント
Steven Lord
Steven Lord 2019 年 3 月 12 日
Or you could reuse f in defining the next function.
f = @(x) x.^2- 4;
g = @(x, h) f(x+h);
Check that they give the same result on some sample data.
x = 1:10;
h = 1;
y1 = f(x+h)
y2 = g(x, h)

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by