MATLAB Answers

Write a MATLAB code to estimate the exponential function. Inputs should be x and n. The outputs must include approximate value, true value, error, absolute error, and relative error.

15 ビュー (過去 30 日間)
Tony Gillard
Tony Gillard 2019 年 9 月 21 日
Commented: James Tursa 2019 年 9 月 21 日
This is what I have but it's not working.
function e_to_x = exp_taylor(x,n)
e_to_x = 0;
for k = 1:n
e_to_x = e_to_x + (x^(k-1))/factorial(k-1);
approx =exp_taylor(1,3);
fprintf("Approximate value %.8f\n",approx);
fprintf("True value %.8f\n",exp(1));
truevalue = exp(1);
relerror = abs(truevalue - approx)/truevalue;
fprintf("Relative error is: ");
aberror = abs(truevalue - approx)/approx;
fprintf("Absolute error is: ");

  1 件のコメント

James Tursa
James Tursa 2019 年 9 月 21 日
Please be specific about what "not working" means. E.g., when I run your code I get:
>> exp(1)
ans =
>> exp_taylor(1,3)
ans =
>> exp_taylor(1,10)
ans =
>> exp_taylor(1,20)
ans =
which looks good to me ...

サインイン to comment.

件の回答 (1)

James Tursa
James Tursa 2019 年 9 月 21 日
Put your function code for e_to_x() in a separate file called e_to_x.m somewhere on the MATLAB path. Then put your other code that calls e_to_x() in a separate file and run that separate file.

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

Translated by