Writing the Sum when Plotting Taylor Series
Afficher commentaires plus anciens
We want to plot f(x) and Taylor polynomials P0(x), P1(x),P2(x), P3(x) of f(x)=cot(x) ; about base point x=a=5*pi/8. step size is 0.01
The problem is we can't make our Taylor Series sum( T1) work. It gives this error:
Error using diff
Difference order N must be a positive integer scalar.
Error in matlab_hw1_q3 (line 11)
T1=sum( (diff(f(a),n(1,i))) ./ factorial(n(1,i)) .* ((x-a).^n(1,i)) , n(1,i))
Our code:
clc
clear all
close all
f=@(x) cot(x)
x= pi/8:0.01:7*pi/8 ;
a=5*pi/8 ;
n=[0 1 2 3]
for i=1:1:4
T1=sum( (diff(f(a),n(1,i))) ./ factorial(n(1,i)) .* ((x-a).^n(1,i)) , n(1,i))
end
Réponse acceptée
Plus de réponses (1)
Sena Ece Uzungil
le 31 Mar 2019
2 commentaires
Walter Roberson
le 31 Mar 2019
At the point you first call diff, you have not assigned a value to i, so i will have its default value. The default value of the variables i and j are both sqrt(-1)
for i = 0:1:4
the_diff = diff(f, X, n(1,i) );
value_at_diff = subs(the_diff, X, a);
T1 = sum( value_at_diff ./ .....)
end
I suggest you rethink this, though. The vector in your assignment to T1 is x, so when you sum() the expression, you are summing over all of the x values. You do not want to do that: you want to sum over all of i values.
T1 = 0;
for i=0:1:4
T1 = T1 + expression_in_x;
end
Sena Ece Uzungil
le 1 Avr 2019
Catégories
En savoir plus sur Calculus dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!