Writing a Taylor series function in matlab

36 ビュー (過去 30 日間)
Ash A
Ash A 2018 年 5 月 8 日
回答済み: Jonah Schmidt 2021 年 5 月 12 日
I want to write a MATLAB function that accepts three inputs (FUN, a, N), where FUN is an annonymous function, a is the point the taylor series is centered around and N is the order of the taylor series. I want the function to output the Nth order Taylor series for the function about a. I am new to matlab so still trying to understand its functionality
This is what I have so far:
function [TS] = tylorSeries(Fun,a.N)
  4 件のコメント
Torsten
Torsten 2018 年 5 月 9 日
So you are not allowed to use MATLAB's "taylor" ?
Ash A
Ash A 2018 年 5 月 9 日
編集済み: James Tursa 2018 年 5 月 11 日
No cant use taylor.. This is what I have:
function [TS_Approx] = taylorSeries(Fun,a,N)
syms x;
TS_Approx = Fun(a)
for n = 1:N
derivative = diff(Fun(x));
TS_Approx = Fun(a)*(x-a)*n/factorial(n)
end
end
I am trying to run it using: taylorSeries(exp(x),0,10)
the aim if the code is to solve f(x) = e^x , a = 0

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

回答 (2 件)

James Tursa
James Tursa 2018 年 5 月 11 日
You're close, but you need to pass in a function handle and then fix up a few other things in your code. So call the code like this:
taylorSeries(@(x)exp(x),0,10) <-- pass in a function handle as the first argument
Then you need to fix up these things:
The (x-a)*n should be a power, not a multiply, so it should be this instead:
(x-a)^n
You aren't summing up the terms. It should be accumulating, e.g.,
TS_Approx = TS_Approx + etc.
You aren't calculating or using the function derivatives properly. You should be calculating the derivative, and then evaluating that derivative at the point "a". E.g.,
derivative = Fun(x);
for n = 1:N
derivative = diff(derivative);
TS_Approx = TS_Approx + subs(derivative,a) * etc.
You need to fill in the etc part. Give it a shot and let us know if you still have problems.

Jonah Schmidt
Jonah Schmidt 2021 年 5 月 12 日
This is the CLOSEST I've ever come to a working Taylor script: still having difficulties geting the correct answer, defeating the purpose of the script.
%Taylor Series Script%
clc;
clear variables;
close all;
disp('Clearing workspace, and closing tabs.')
format short;
disp('Now using Taylor Series expansion')
syms x;
%User Inputed Function%
z = input('Enter f(x) = ','s');
if isempty(z)
error('No function entered.')
end
f = inline(z);
%Order Input%
n = input('Enter the order of this Taylor series: ');
if isempty(n)
disp('No order entered, going to 7th order Taylor Expansion.')
n = 7;
end
%Step Size Input%
h = input('Enter the step size of this Taylor series (Usually 1): ');
if isempty(h)
error('No value entered.')
end
%Point Input%
p = input('Enter the point of which this Taylor series approximates: ');
if isempty(p)
error('No point entered.')
end
%Tag Line%
fprintf('\nOrder \t\t\t Value \t\t\t Approximate Error \n')
%xr = vpa(f(p)/factorial(n)*h^(n));%
%xr + (p.^n)./factorial(n)%
%Loop%
xr = p;
for iter = 1:n
xr = xr + vpa(f(p)/factorial(n)*h^(n));
v = func1(xr,n,p);
iter = iter + 1;
n = n + 1;
err = ((v-xr)/v)*100;
fprintf(' %d \t\t\t\t %f \t\t %f \n',iter-1,v,err)
end
%Line Break%
fprintf('\n')
%Function xr%
function a = func1(xr,n,p)
a = xr + (p.^n)./factorial(n);
end

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by