Writing a taylor series function for e^x
5 views (last 30 days)
Show older comments
I need to write a function that takes two input arguments- x and n (where n is the number of terms) and one output argument- the value of exp(x) for the Taylor series of e^x. This is the code I have right now
function [ ts ] = tayser( x,n )
%TAYLOR Finds the value to Taylor series
% finds the value of the Taylor series given an x and "n" number of terms
for i=0:n
y(i+1) = x^(i+1)/factorial(i+1);
end
ts=sum(y);
end
I tried some random numbers like x=4, for n= 5 terms. But I got an error. Can someone help? please and thank you!
3 Comments
Walter Roberson
on 27 Feb 2017
Vectorized in x:
for i=0:n
y(:, i+1) = x.^(i+1) ./ factorial(i+1);
end
Answers (2)
James Tursa
on 27 Feb 2017
You are missing the first term in the series (i.e., the 1). You could rectify this by modifying the sum line. E.g.,
ts = 1 + sum(y);
0 Comments
M.SEETHA LAKSHMI
on 19 Sep 2022
(1-e^(-B*t))
how to solve this e power value in matlab?
is there is any code to solve this kindly send the coding for e power value
2 Comments
Sam Chak
on 19 Sep 2022
John D'Errico
on 19 Sep 2022
Edited: John D'Errico
on 19 Sep 2022
Please don't answer a question, with a completely new question as you did here. If you have a valid question, then ask a question. Start a NEW question.
Anyway, your question is itself highly confusing. Are you perhaps asking how to evaluate that expression for some value(s) of B and t? Are you perhaps asking how to accurately evaluate that expression when B or t are near zero? In that case, the direct evaluation using exp will be inaccurate, however there is a solution in MATLAB, in the form of expm1.
help expm1
Or perhaps you wish to know how to solve that fragment for the value of B, or t, or something. We don't know, and your question is far to confusing to get an intelligent answer.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!