MATLAB Help for expansion of cos(x) using a Taylor Series

Click For Summary
SUMMARY

This discussion focuses on implementing the Taylor series expansion of cos(x) in MATLAB. Users shared code snippets to visualize the convergence of the Taylor series for various orders of n. Key corrections include using MATLAB's built-in factorial function instead of the product function for calculating factorials, and the necessity of summing terms in a loop to accurately compute the series. A function should be created to accept n and x as arguments to return the approximated cosine value.

PREREQUISITES
  • Understanding of Taylor series and its mathematical formulation.
  • Familiarity with MATLAB programming, particularly for-loops and plotting.
  • Knowledge of MATLAB's built-in functions, including factorial.
  • Basic concepts of numerical approximation and convergence.
NEXT STEPS
  • Learn how to implement MATLAB functions to encapsulate logic for Taylor series calculations.
  • Explore MATLAB plotting techniques to visualize mathematical functions effectively.
  • Study the convergence properties of Taylor series for different functions.
  • Investigate error analysis in numerical methods to understand approximation accuracy.
USEFUL FOR

Students, educators, and professionals in mathematics, engineering, and computer science who are interested in numerical methods and MATLAB programming for function approximation.

Tuttle917
Messages
1
Reaction score
0
I was hoping somebody would be able to help me as I am pretty new to Matlab. I am trying to create a for-loop to describe the taylor series expansion of cos(x)= (-1)^n*x^2n/(2n)! and to see how it converges towards cos(x). Below is the code that I have used to plot the different orders of n, but I was wondering if there was a way to make this work for any value of n?

n=0;
for x = -6:0.25:6
n=n+1;
y(n)=x;
F(n)=cos(x);
F0(n)=1;
F2(n)=1-0.5*x^2;
F4(n)=1-0.5*x^2+(1/24)*x^4;
end
plot(y,F,y,F0,y,F2,y,F4)
axis([-6 6 -1.5 1.5])

I have tried

n=0;
for x=(-2:.25:2)
n=n+1;
Y(n)=x;
F(n)=cos(x);
G(n)=1+(-1)^n*x^(2*n)/prod(1:2*n);
end
plot(y,G)

but the plot for G does not come close to cos(x) as the values of n are not constant and the plot goes to zero. Any help would be greatly appreciated.
 
Physics news on Phys.org
The solution is to make a function that will take in as arguments n and x and will return the approximated cosine.

Tuttle917 said:
G(n)=1+(-1)^n*x^(2*n)/prod(1:2*n);
This is not correct, as it is the sum of the 0th term and the nth term. There should be a loop in order to sum over n.

Also, MATLAB has a factorial function, so factorial(2*n) should be used instead of prod(1:2*n).
 
MATLAB CODE FOR CALCULATION TAYLOR SERIES a*cos(bx)^2

clear all
clc

a=input('enter an a value ');
b=input('enter an b value ');
x1=input('enter an x1 value ');
x2=input('enter an x2 value ');
f(x1)=a*cos(b*x1)^2;
f1(x1)=-2*a*b*cos(b*x1)*sin(b*x1); %diff(f(x1))
f2(x1)=2*a*b^2*sin(b*x1)^2 - 2*a*b^2*cos(b*x1)^2; %diff(f1(x1))
f3(x1)=8*a*b^3*cos(b*x1)*sin(b*x1);%diff(f2(x1))


y=f(x1)+f1(x1)*(x2-x1)+(f2(x1)*(x2-x1)^2)+(f3(x1)*(x2-x1)^3)/6 f(x2)=a*cos(b*x2)^2;
f1(x2)=-2*a*b*cos(b*x2)*sin(b*x2); %diff(f(x2))
f2(x2)=2*a*b^2*sin(b*x2)^2 - 2*a*b^2*cos(b*x2)^2; %diff(f1(x2))
f3(x2)=8*a*b^3*cos(b*x2)*sin(b*x2); %diff(f2(x2))

z=f(x2)+f1(x2)*(x2-x1)+(f2(x2)*(x2-x1)^2)+(f3(x2)*(x2-x1)^3)/6

error=((y-z)/y)*100
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
4
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K