Writing a taylor series function for e^x
14 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Walter Roberson
2017 年 2 月 27 日
Vectorized in x:
for i=0:n
y(:, i+1) = x.^(i+1) ./ factorial(i+1);
end
回答 (2 件)
James Tursa
2017 年 2 月 27 日
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 件のコメント
M.SEETHA LAKSHMI
2022 年 9 月 19 日
(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 件のコメント
Sam Chak
2022 年 9 月 19 日
John D'Errico
2022 年 9 月 19 日
編集済み: John D'Errico
2022 年 9 月 19 日
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!