taylor series using forward method

2 ビュー (過去 30 日間)
Ronald Aono
Ronald Aono 2019 年 10 月 21 日
回答済み: Yuan Li 2019 年 10 月 21 日

% question 3 part b
clc
clear all
real_val=14301/400;
h=[10^-4 10^-3 10^-2 10^-1 10^0 10^1];%step sizes
%defining the function f(x)
n=length(h);
x = 2.1;
f=@(x) x^5-2*x^4+3*x^2-1;
df=@(x) 3*x^4 - 8*x^3 + 6*x;
df=cell(1:n);
error=[];
for i=1:n
f(i)=(f(x + h(i)) - f(x))/ (h(i));% evaluating the derivative at h(i)
error(i)=abs((df(i)-real_val)/(real_val)); % evaluating the absolute error at h(i)
end

% creating a log log plot of the error against
figure(1)
loglog(h,error)
grid on
xlabel('step size')

keep getting this error
Conversion to function_handle from double is not possible.

Error in Tay_ser (line 25)
f(i)=(f(x + h(i)) - f(x))/ (h(i));% evaluating the derivative at h(i)

採用された回答

Yuan Li
Yuan Li 2019 年 10 月 21 日
I can't figure out the relationship between the following codes:
f(i)=(f(x + h(i)) - f(x))/ (h(i));% evaluating the derivative at h(i)
error(i)=abs((df(i)-real_val)/(real_val)); % evaluating the absolute error at h(i)
how about reviewing this way?
temp(i)=(f(x + h(i)) - f(x))/ (h(i));% evaluating the derivative at h(i)
error(i)=abs((temp(i)-real_val)/(real_val)); % evaluating the absolute error at h(i)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLog Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by