Can't get my Maclaurin Series function to use symsum properly?

3 ビュー (過去 30 日間)
Cole Butler
Cole Butler 2017 年 3 月 30 日
コメント済み: Walter Roberson 2017 年 3 月 30 日
Here is the code I'm working with currently:
function [ output_args ] = MaclaurinSeries( f,a,tol )
syms x k;
y = subs(f,a)
disp(f)
i=1
error = 1;
while error > tol
f1 = symsum(diff(f,x,k) * (x^k)/(factorial(k)),k,0,i)
error = y - subs(f1,a)
q = subs(f1,a)
i=i+1
end
Every time I run it, no matter how many times I tweak it, I manage to get f1 = 0 every time (which just isn't true). It calculates the derivative properly and everything, but I just don't know why it's returning zero. Any help?

採用された回答

Walter Roberson
Walter Roberson 2017 年 3 月 30 日
編集済み: Walter Roberson 2017 年 3 月 30 日
symsum(diff(f,x,k) * (x^k)/(factorial(k)),k,0,i)
is processed as
temp = diff(f,x,k) * (x^k)/(factorial(k));
symsum(temp, k, 0, i)
That is, the expression is evaluated first (as is the case for all MATLAB function arguments) and the result of the expression has the symsum applied to it.
But diff(f, x, k) with symbolic k is 0 . I do not know why they made that choice.
With it coming out as 0, you are doing symsum(0, k, 0, i) which is going to be 0.
I would advise not using symsum for this. Instead, keep a running total, adding one more term at each point.
  3 件のコメント
Torsten
Torsten 2017 年 3 月 30 日
You are not allowed to use MATLAB's "taylor" ?
Best wishes
Torsten.
Walter Roberson
Walter Roberson 2017 年 3 月 30 日
f1 = sym(0);
while ...
new_term = ...
f1 = f1 + new_term;
...
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by