how to plot forward difference error against h values ?

% Here is the code which I wrote but I'm not sure about. I think there are errors and I dont understand the last part which says use a function thanks for helping.
a=1;
h=10.^[-1:-1:-10];
x=a;
tval=(1/x)-1
%Forward
x=a+h;
f1=2-x+log(x);
x=a;
f2=2-x+log(x);
f3=(f1-f2)./h
Eabs=abs(tval-f3)
plot(Eabs,h)

回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2022 年 5 月 2 日

0 投票

So when you save what you have written in a file, that would be saved as a script file. You have to manually update different values for different results and run them each time.
So, for a function file you have to define the syntax, as mentioned in the web page above.
function output = function_name(input)
your_code...
end
and then you can get outputs by calling the file without running it manually.

カテゴリ

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

製品

リリース

R2021b

質問済み:

2022 年 5 月 1 日

回答済み:

2022 年 5 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by