Hello, im wondering if there any way i could perform calculation using values up to 4 decimal places only.
For example,i write this in command windows:
x=4.21;
h=0.001;
f=@(x) x^3 + 8*x^2 + 23*x +3;
f2_prime = (f(x+h)-2*f(x)+f(x-h))/(h^2);
how can i make the value of f(x), f(x+h) and f(x-h) to be only up to 4 decimal places when calculating for f2_prime?

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 12 月 1 日

0 投票

You can use round()
x=4.21;
h=0.001;
f=@(x) x^3 + 8*x^2 + 23*x +3;
f2_prime = (round(f(x+h),4)-2*f(x)+round(f(x-h),4))/(h^2);

4 件のコメント

fikri osman
fikri osman 2020 年 12 月 1 日
thank you, i will try and see if it works
fikri osman
fikri osman 2020 年 12 月 1 日
f2_prime = (round(f(x+h),4)-2*f(x)+round(f(x-h),4))/(h^2);
does this round off the f(x) too?
fikri osman
fikri osman 2020 年 12 月 1 日
f2_prime = (round(f(x+h),4)-(2*round(f(x),4)+round(f(x-h),4)/(h^2);
i use this and got error
fikri osman
fikri osman 2020 年 12 月 1 日
f2_prime = (round(f(x+h),4)-2*round(f(x),4)+round(f(x-h),4))/(h^2);
i used this and got the answer i wanted, thank you sir

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 12 月 1 日

0 投票

how can i make the value of f(x), f(x+h) and f(x-h) to be only up to 4 decimal places when calculating for f2_prime?
You would need to use fixed-point arithemetic, such as from the Fixed Point Toolbox.
A close second would be to use the Symbolic Toolbox with digits(4), but it turns out that the Symbolic Toolbox uses "guard digits" ... I think I read once that it uses 5 guard digits -- and the minimum permitted is digits(2)

カテゴリ

ヘルプ センター および File ExchangeMATLAB Compiler についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by