Writing a taylor series function for e^x
조회 수: 8 (최근 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!