Taylor series in matlab

조회 수: 6 (최근 30일)
Brendan Clark
Brendan Clark 2021년 4월 25일
댓글: Walter Roberson 2021년 4월 25일
I'm working on trying to approximate a taylor series for log(1+x) in matlab. However, I wasn't given an equation, and I haven't finished calculus yet and I'm struggling to get the right equation for the proximation. I can't use the built in functions in matlab to accomplish this sadly. Right now my code is.
x = linspace(0,pi, 100);
N=10;
logApprox = zeros(1,length(x));
for i = 1:length(x)
for j = 1:N
Number = (j - 1);
logApprox(i) = logApprox(i) + (-1)^(j + 1)*((x(i)^N)/N)^(Number)/factorial(Number);
end
end
disp(logApprox)
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 4월 25일
That particular function does not need the factorial, and you are raising to the wrong power
Order 7 for example:
- x^6/6 + x^5/5 - x^4/4 + x^3/3 - x^2/2 + x
Brendan Clark
Brendan Clark 2021년 4월 25일
I understand how the equation is laid out in that format, I just don't have a deep enough grasp of calculus to turn that into a usable matlab equation.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 4월 25일
- x^6/6 + x^5/5 - x^4/4 + x^3/3 - x^2/2 + x
Look at that more carefully. Suppose you have a loop index, K, then you have a term X^K/K*(-1)^(K+1)
After that what you need to know is to use the .^ operator instead of the ^ operator -- x.^K/K*(-1)^(K+1)
You do not need to loop over the x values; you can use vectorized calculations.
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 4월 25일
With a fairly small amount of extra work, you can eliminate the loop, but that requires understanding implicit expansion.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by